[英]Spring MVC Java based config - JSP not resolving
I am developing a little demo project with Spring MVC on a Tomcat with hibernate and JSPs . 我正在使用带有休眠和JSP的Tomcat在Spring MVC上开发一个小演示项目。 The application was working with thymeleaf as a view resolver but I need to rewrite my thymeleaf template as a .JSP. 该应用程序正在使用百里香叶作为视图解析器,但是我需要将百里香叶模板重写为.JSP。 So I needed to reconfig everything. 所以我需要重新配置所有内容。 I am also trying to only use annotation configuration files without any xml. 我也试图仅使用不带任何xml的注释配置文件。
The problem is that I am seeing the source code of my JSP-File without resolving. 问题是我无法解析我的JSP文件的源代码。
The Request-Mapping and the program logic already worked with thymeleaf + spring . 请求映射和程序逻辑已与thymeleaf + spring一起使用。 I am getting no error messages in the log files. 我在日志文件中没有收到任何错误消息。 I tried a lot of changes so my config files may be inconsistent at the moment. 我尝试了很多更改,因此目前我的配置文件可能不一致。 Any pointers or advice is appreciated. 任何指针或建议表示赞赏。
My config files are below: 我的配置文件如下:
MyWebAppInitializer: MyWebAppInitializer:
public class MyWebAppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "MvcConfiguration";
private static final String MAPPING_URL = "/";@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(MAPPING_URL);
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
return context;
}
}
MvcConfiguration: MvcConfiguration:
class MvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
BenutzerController: BenutzerController:
@Controller
public class BenutzerController {
@Autowired
BenutzerRepository repository;
@ModelAttribute("Benutzer")
public Benutzer loadEmptyModelBean() {
return new Benutzer();
}
@ModelAttribute("userList")
public List < Benutzer > userList() {
return toList(repository.findAll());
}
@RequestMapping("/verwaltung")
public String verwaltung(@RequestParam(value = "message", required = false, defaultValue = "Defaultvalue") String message, ModelMap model) {
model.addAttribute("message", message);
return "verwaltung";
}
pom.xml: pom.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>benutzerverwaltung</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
EDIT: SOLUTION 编辑:解决方案
editing the pom.xml: 编辑pom.xml:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.0.29</version>
</dependency>
Are you using Spring Boot's embedded Tomcat? 您正在使用Spring Boot的嵌入式Tomcat吗? According to the answer to this question , you may need to add a dependency: compile("org.apache.tomcat.embed:tomcat-embed-jasper")
. 根据此问题的答案,您可能需要添加一个依赖项: compile("org.apache.tomcat.embed:tomcat-embed-jasper")
。 However, I've never got JSPs to work properly with an embedded tomcat - you can create a war file instead, as it says in the docs . 但是,我从来没有让JSP与嵌入式tomcat一起正常工作-您可以改为创建war文件,如docs中所述 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.