我有一个包含以下代码的项目:
简化版:
@SpringBootApplication
public class Starter extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Starter.class);
}
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
}
控制器:
@Controller
public class GreetingController {
@RequestMapping(value = "greeting", method = RequestMethod.GET, produces = "text/html; charset=UTF-8")
public ModelAndView greeting() {
ModelAndView modelAndView = new ModelAndView("Greeting");
return modelAndView;
}
}
配置:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"hello"})
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(org.springframework.web.servlet.view.JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
JSP(Greeting.jsp):
<%@ page session="false" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1>Spring MVC Hello World Annotation Example</h1>
<h2>HELLO !</h2>
</body>
</html>
POM:对springboot和jstl的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
当我向http://localhost:8080/greeting发送请求时,我在webbrowser中收到一个文件Greeting.jsp作为文本文件(文件未执行)。当我从Greeting.jsp中删除以下行:
<%@ page session="false" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
请求处理正确 - 浏览器仅显示标题标记中的文本(“Hello”和“Spring MVC ...”)。
问题是:JSTL视图配置有什么问题?