我正在编写一个部署在Tomcat上的Spring MVC应用程序.请参阅以下最小,完整且可验证的示例
public class Application extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { };
}
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { SpringServletConfig.class };
}
protected String[] getServletMappings() {
return new String[] { "/*" };
}
}
Run Code Online (Sandbox Code Playgroud)
哪里SpringServletConfig是
@Configuration
@ComponentScan("com.example.controllers")
@EnableWebMvc
public class SpringServletConfig {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setPrefix("/WEB-INF/jsps/");
vr.setSuffix(".jsp");
return vr;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我有一个@Controller包com.example.controllers
@Controller
public class ExampleController {
@RequestMapping(path = "/home", …Run Code Online (Sandbox Code Playgroud) 我使用嵌入式Tomcat(默认)启动并运行Spring Boot Web应用程序.当它提供JSP文件作为渲染我在控制器中指定的视图的一部分时,JSP不会这样呈现,而是打印出内容.例如:
的index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>Test</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在浏览器中呈现视图时,将显示上面的内容,而不是预期的内容:
Test
Run Code Online (Sandbox Code Playgroud)