JSP页面无法在spring boot embedded下显示

时间:2017-11-24 09:26:35

标签: spring maven jsp tomcat undertow

我想在spring boot appliaction中显示JSP页面。我能够在tomcat中做到这一点但是当我改变嵌入式服务器时它会抛出错误。我无法弄清楚为什么它不能在Undertow中工作我已经改变并从tomcat中删除了依赖关系,我认为可能存在依赖性缺失。有人能说出我在这里犯的错误吗?当我在其中运行时它会抛出错误,

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers.

我唯一的区别就是在pom.xml文件中。

Tomcat的pom.xml

<!-- Web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Web with Tomcat + Embed -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

<!-- Need this to compile JSP -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Undertow的pom.xml

<!-- DEPLOY TEST -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/io.undertow/undertow-jsp -->
<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-jsp</artifactId>
    <version>1.0.0.Alpha21</version>
</dependency>

的index.jsp

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html lang="en">
<body>
<div class="container">
    <h1>Branch As a Service</h1>
    <form method="post" action="save">
        Name: <br/>
        <input type="text" name="name"><br>
        <p></p>
        Version: <br/>
        <input type="text" name="version"><br>
        <p></p>
        <input type="submit" value="Save"/>
    </form>
</div>
</body>
</html>

application.properties

spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp

ServiceController.java

@RequestMapping("index")
  public ModelAndView viewemp(){
    return new ModelAndView("index");
  }

1 个答案:

答案 0 :(得分:0)

基于Spring Boot documentation

  

Undertow不支持JSP。

JSP非常陈旧且有限(至少在Spring Boot中),您应该考虑使用像Thymeleaf,FreeMarker等现代支持模板库。