我有一个基于Spring 4.3.20和Hibernate 3.6构建的现有应用程序,我们在XML配置中使用SimpleUrlHandlerMapping
进行控制器映射,并且运行良好。
我正在尝试使用Spring Boot 1.5.17.RELEASE与当前的Spring版本4.3.20-RELEASE兼容。现在,在解决所有编译和部署问题之后,我看不到现有的控制器与浏览器/ POST / GET映射配合良好。它总是显示404错误。
我在部署时检查了日志,发现映射正确完成,请在日志中找到以下内容:
2019-06-03 12:39:26.333 INFO 15630 --- [nio-8084-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
我还创建了一个带有注释(没有SimpleUrlHandlerMapping
)的单独控制器,并且按照日志正确完成了其映射,并且工作正常
2019-06-03 12:32:10.437 INFO 26049 --- [nio-8084-exec-9] .w.s.m.a.DefaultAnnotationHandlerMapping : Mapped URL path [/testc] onto handler 'testController'
2019-06-03 12:32:10.437 INFO 26049 --- [nio-8084-exec-9] .w.s.m.a.DefaultAnnotationHandlerMapping : Mapped URL path [/testc.*] onto handler 'testController'
2019-06-03 12:32:10.437 INFO 26049 --- [nio-8084-exec-9] .w.s.m.a.DefaultAnnotationHandlerMapping : Mapped URL path [/testc/] onto handler 'testController'
2019-06-03 12:32:10.583 INFO 26049 --- [nio-8084-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/testc],methods=[GET]}" onto public java.lang.String com.krawler.spring.TestController.test()
2019-06-03 12:32:16.898 INFO 26049 --- [nio-8084-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/testc],methods=[GET]}" onto public java.lang.String com.krawler.spring.TestController.test()
2019-06-04 16:55:12.448 INFO 13811 --- [nio-8084-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/AuthHandler/*.do] onto handler 'authHandlercontroller'
我尝试使用带有jsp页面的@Controller,它可以正常工作,但不能用于使用ModelAndView的现有控制器。
控制器类
@Controller
@RequestMapping("testc")
public class TestController {
@GetMapping
public String test(){
return "test";
}
}
/WEB-INF/jsp/test.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
AuthHandlreController
public ModelAndView verifyUserLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException {
JSONObject jobj = new JSONObject();
.....
return new ModelAndView("jsonView_ex", "model", jobj.toString());
}
用于jsonView_ex的bean
<bean name="jsonView_ex" class="com.mycompany.views.JsonView_ex">
<property name="contentType">
<value>text/html; charset=ISO-8859-1</value>
</property>
</bean>
JsonView_ex.java
public class JsonView_ex extends AbstractView {
protected void renderMergedOutputModel(Map map,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String model = (String) map.get("model");
try {
JSONObject jobj = new JSONObject(model);
if (jobj.has("msg")) {
if (jobj.getString("msg").toLowerCase().contains("system failure:")) {
jobj.remove("msg");
jobj.put("msg", "Some problem occurred while performing the operation. Please try again later.");
model = jobj.toString();
}
}
} catch (JSONException je) {
model = (String) map.get("model");
}
// write the XML data to the response
response.getOutputStream().write(model.getBytes());
response.setContentType("text/html; charset=UTF-8");
}
}
TestController
工作正常,而不是AuthHandlerController
问题似乎只在于视角,但不确定如何解决。
答案 0 :(得分:0)
当使用jar
作为包装时,使用jsp作为视图解析器时,Springboot有局限性。对于您的情况,请确保您正在生成war
文件,请在POM中使用以下命令:
pom.xml
<packaging>war</packaging>
将上述标记放在主pom中的<version>
标记下方。还要确保将jsp文件放在文件夹src/main/webapp/WEB-INF/jsp
或基本上在webapps下。
确保在pom中添加了以下依赖项来编译jsp:
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
,也可以在application.properties
或application.yml
中使用put:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp