【发布时间】:2018-03-13 07:29:19
【问题描述】:
我在使用 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)定向到句柄方法?
【问题讨论】:
-
控制器方法中返回什么?要查看页面,您应该只返回字符串“index”。还要粘贴您的 servlet 配置,因为不清楚您为 DispatcherServlet 定义了什么映射(上下文路径/servlet 路径)
-
我只是在 handle() 方法中返回 "" 。我没有 servlet 配置?因为我正在使用 Spring Boot。
-
尝试返回“application/index”并在浏览器中使用
http://localhost:8080/application/index.jsp -
我在handle()方法中添加了return并尝试了uri到jsp文件,但仍然返回404 not found。
-
好的,我几个小时后回来,我现在在上班
标签: java spring jsp spring-mvc spring-boot