我构建了一个多模块Spring
应用程序。一切正常,但从视图返回的JSP文件将浏览器显示为纯文本。
一些代码:
Spring boot Init:
@SpringBootApplication
@ComponentScan(basePackages={"hu.me.zelena.rr"})
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
}
}
配置:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Dispatcher config:
@Configuration
public class DispatcherConfiguration {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
registration.addUrlMappings("/");
registration.setLoadOnStartup(1);
return registration;
}
}
控制器:
@Controller
public class MainController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView welcome() {
ModelAndView model = new ModelAndView("hello");
model.addObject("title", "TEST");
model.addObject("message", "Test msg");
return model;
}
}
JSP就像这样简单:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<body>
<h1>Title : ${title}</h1>
<h1>Message : ${message}</h1>
</body>
</html>
结果与jsp代码完全一样。 它出什么问题了?我找不到解决方案。