向浏览器写验证码却直接去找jsp页面,还需要配置什么东西吗?

img

控制层代码如下:

@RequestMapping("/code")
    public void generatorCode() throws Exception{

        // 获取 宽和高
        int width = Integer.parseInt(ContextUtils.getRequest().getParameter("w")) ;
        int height = Integer.parseInt(ContextUtils.getRequest().getParameter("h")) ;

        AbstractCaptcha captcha = CodeUtils.createCaptcha(width, height);

        // 获取验证码 ,存储到 Session中
        String code = captcha.getCode();

        ContextUtils.getSession().setAttribute(AppConst.CODE , captcha);

        // 将验证码通过流的技术,写入到浏览器中
        ServletOutputStream outputStream = ContextUtils.getResponse().getOutputStream();

        captcha.write(outputStream);

    }

根据你提供的代码,向浏览器输出验证码应该是没有问题的,不需要再配置什么东西了。

如果你访问 /code 时确实跳转到了 JSP 页面,那么可能是你的请求路径配置有问题。你可以检查一下你的控制层代码所在的类上是否添加了 @Controller 注解,同时该类所在的包是否被正确地扫描到了。如果还有其他的控制层类,也需要检查是否存在相同的问题。如果路径配置没有问题,可能需要检查一下浏览器端的请求是否正确。