Spring Boot - 映射JSP和REST URI以在控制器中处理的正确方法?

时间:2017-10-02 08:24:07

标签: java spring jsp spring-mvc spring-boot

我有一个Spring boot 1.5.2的问题,我还添加了JSP的所有依赖项,因此它可以从web应用程序(war)文件返回。

application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

带有JSP依赖关系的pom.xml

<!-- JSTL for JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP,
    tomcat-embed-jasper version is not working, no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>       

我有一个像这样的简单控制器

@Controller
public class ClassController {
    @RequestMapping("/**")
    public String handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // This one can handle any rest URIs, e.g: localhost:8080/application/abc, localhost:8080/application/abc/12/asdasd/3123,...
    }
}

但是,我无法从localhost:8080 / application / index.jsp返回index.jsp,因为它不会调用方法句柄,而是返回

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Oct 02 10:20:39 CEST 2017
There was an unexpected error (type=Not Found, status=404).

什么是正确的配置,所以我可以将.jsp(例如:http://localhost:8080/application/index.jsp)的请求指向jsp文件(WEB-INF / jsp / index.jsp)和REST uri(http://localhost:8080/application/132/21321312/324 )处理方法?

2 个答案:

答案 0 :(得分:1)

我使用servlet过滤器将包含.jsp的请求转发到/ WEB-INF / jsp中的相应文件,从而解决了这个问题。其他请求将转到控制器内的handle()方法。

 @WebFilter(urlPatterns = "/*")
public class SecoreFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        if (!request.getRequestURI().endsWith(".jsp")) {
            chain.doFilter(req, res);
        } else {
            String jspFile = new File(request.getRequestURI()).getName();
            // e.g: /application/3223/234234/2342323/browse.jsp
            String uri = request.getRequestURI();
            // extract the jsp file from uri
            uri = uri.substring(0, uri.length() - jspFile.length());
            RequestDispatcher dispatcher = req.getServletContext()
                    .getRequestDispatcher("/WEB-INF/jsp/" + jspFile);
            req.setAttribute("uri", uri);
            dispatcher.forward(req, response);
            // chain.doFilter(req, res);
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}

答案 1 :(得分:0)

我根据您的描述重建了您的项目,发现了两个问题。

  • tomcat-embed-jasper 需要是编译时依赖项,您不需要Eclipse编译器
  • 在控制器( ClassController )中,您必须返回视图的名称,即索引

您可以在此处找到一个有效的示例:https://github.com/springuni/springuni-stackoverflow/tree/master/question-46521912

它的作用是将任何给定的请求(/ application / 132/21321312/324)重定向到 index.jsp 。在JSP中,请求的路径被写入输出,以证明重定向实际上来自控制器。

让我知道它是否适合你!

干杯, 拉斯洛