[英]Spring Boot - Correct way to map both JSP and REST URI to be handled in controller?
I have a problem with Spring boot 1.5.2, I also added all the dependencies for JSP so it could return from web application (war) file.我对 Spring boot 1.5.2 有问题,我还添加了 JSP 的所有依赖项,以便它可以从 Web 应用程序 (war) 文件返回。
application.properties应用程序属性
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
pom.xml with JSP dependencies带有 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>
And I have a simple controller like this我有一个像这样的简单控制器
@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,...
}
}
However, I could not return index.jsp from localhost:8080/application/index.jsp as it will not invoke the method handle, but return但是,我无法从 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).
What is the proper configuration, so I could direct request with .jsp (eg: http://localhost:8080/application/index.jsp ) to jsp file (WEB-INF/jsp/index.jsp), and REST uri ( http://localhost:8080/application/132/21321312/324 ) to the handle method?什么是正确的配置,所以我可以使用 .jsp(例如: http://localhost:8080/application/index.jsp )直接请求到 jsp 文件(WEB-INF/jsp/index.jsp)和 REST uri( http://localhost:8080/application/132/21321312/324 ) 到句柄方法?
I solved this problem with using a servlet filter to forward requests containing .jsp to the corresponding files inside /WEB-INF/jsp.我通过使用 servlet 过滤器将包含 .jsp 的请求转发到 /WEB-INF/jsp 中的相应文件解决了这个问题。 Other requests will go to the handle() method inside the controller.
其他请求将转到控制器内的 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 {
}
}
I've rebuilt your project based on your description and found two issues.我根据你的描述重建了你的项目,发现了两个问题。
You can find a working example here: https://github.com/springuni/springuni-stackoverflow/tree/master/question-46521912你可以在这里找到一个工作示例: https : //github.com/springuni/springuni-stackoverflow/tree/master/question-46521912
What it does is that it redirects any given request (/application/132/21321312/324) to index.jsp .它的作用是将任何给定的请求 (/application/132/21321312/324) 重定向到index.jsp 。 In the JSP the path of the request is written to the output for demonstrating that the redirection actually came from the controller.
在 JSP 中,请求的路径被写入输出以证明重定向实际上来自控制器。
Let me know if it worked for you!让我知道它是否对你有用!
Cheers, László干杯,拉斯洛
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.