【问题标题】:Spring Boot - where to place the jsp filesSpring Boot - 放置 jsp 文件的位置
【发布时间】:2017-10-23 05:13:49
【问题描述】:

我正在尝试使用 MVC 作为将现有 Spring MVC 应用程序迁移到 Spring boot 的第一步来开发一个新的 Spring Boot 应用程序。

但是,我遇到了 jsp 文件映射的问题。

无法解析名称为“dispatcherServlet”的 servlet 中名称为“hello”的视图

我已经在 SO 中准备了许多答案,但似乎没有一个可以解决我的问题 - 我不打算使用任何模板引擎,因为我将有很多 jsps 需要考虑 - 一旦设置了 spring boot 可能是一个计划。

我的项目结构如下:

MyFirstApp
  --src/main/java
    --uk.co.company
      --MainApplication.java
      --ServletInitializer.java
    --uk.co.company.web
      --HelloController.java
  --src/main/resources
    --static
    --templates
    --application.properties
 --src
   --main
     --webapp
       --WEB-INF
         --jsp
           --hello.jsp
  --pom.xml

代码如下:

MyFirstAppApplication.java

 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, 
 HibernateJpaAutoConfiguration.class })
 @EnableWebMvc
 public class MyFirstAppApplication extends SpringBootServletInitializer {
     public static void main(String[] args) {
    SpringApplication.run(MyFirstAppApplication.class, args);
  }
}

ServletInitializer.java

  public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
    return application.sources(MyFirstAppApplication.class);
}
}

HelloController.java

@Controller
public class HelloController {  
@RequestMapping("/hello")
public String sayHello() {      
    return "hello";
}   
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
hellooo
</body>
</html>

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>

<groupId>uk.co.company</groupId>
<artifactId>MyFirstApp</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>MyFirstApp</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties

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

【问题讨论】:

  • jsp的标准路径是WEB-INF目录
  • 能否添加jstl依赖&lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;jstl&lt;/artifactId&gt; &lt;/dependency&gt;
  • 加了前缀和后缀的application.properties,还是不行
  • jstl 依赖也不行
  • 你是在创建jar还是war?

标签: java maven jsp spring-mvc spring-boot


【解决方案1】:

我创建了一个渲染jsp的演示项目

Git 网址:https://github.com/rksharma1401/spring-boot-war

然后结帐 mvn包 java -jar 目标\simple-web-app-tomcat-0.0.1-SNAPSHOT.war 网址:http://localhost:8081/w

【讨论】:

  • 我已经更新了 pom.xml,它指出了我的问题。我很欣赏它帮助我找出这个问题的根本原因的解决方案。我已经发布了解决方案。
  • 你是怎么做到的,我下载了你的项目,它工作得很好。我创建了一个 Web Maven 项目并为 Spring Boot 应用程序创建了一个控制器和一个主类,我将您的确切依赖项复制到了 pom 中,但是当我运行该项目时,我仍然没有得到我创建的 Jsp 页面,并且web.xml 是在我运行应用程序时自动创建的,你能发布一下你是如何做到的吗?
  • 你有 war 或 jar .... JSP 只在战争中工作
【解决方案2】:

我走了完全一样的路。将一个jsp、基于xml的spring应用升级到spring boot 2。 迁移时需要考虑的事项很少:

首先删除@EnableWebMvc。

其次,您需要在 MyFirstAppApplication 类之上使用 @ComponentScan。

尝试阅读这篇文章,它对我有很大帮助 https://htr3n.github.io/2018/12/jsp-spring-boot/

第三,你还需要这个依赖项和 embed-jasper:

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.22</version>
</dependency>

最后但并非最不重要的是,这是在 java 中创建视图处理程序的快捷方式

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

据我所知,它仅适用于 Spring Boot 2。 您可以改用 Java 实现并对其进行调试以查看它是否达到了目标。

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");       
    registry.viewResolver(resolver);
}

因为以我为例,我什至不需要特定或默认的解析器。 我必须在 xml/bean 中定义并注入每个类中的每个控制器都有不同的解析器。

【讨论】:

    【解决方案3】:

    从 Spring Boot 类中删除 @EnableWebMvc 注释。 默认情况下,@SpringBootApplication 使您的应用程序成为 Web 应用程序。显式声明 @EnableWebMvc 将禁用自动配置,并且自动设置 DispatcherServlet 将被覆盖,这会导致您出现问题。

    【讨论】:

      【解决方案4】:

      问题在于 jar spring-boot-starter-parent 的版本。由于某种原因,这不适用于版本1.5.3 RELEASE。它工作到版本1.5.2 RELEASE

      我已经更新了pom.xml的父标签如下:

      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>1.5.2.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
      </parent>
      

      【讨论】:

        【解决方案5】:

        如果您想通过 jar 包进行部署,不鼓励使用目录 src/main/webapp/WEB-INF/jsp/..。

        “如果您的应用程序将被打包为 jar,请不要使用 src/main/webapp 文件夹。虽然这个文件夹是一个通用标准,但它只适用于 war 打包,如果你生成一个 jar,它会被大多数构建工具默默地忽略。”

        参考:https://docs.spring.io/spring-boot/docs/1.1.4.RELEASE/reference/htmlsingle/#using-boot-structuring-your-code

        【讨论】:

          【解决方案6】:

          需要包含一个依赖项,因为 Spring boot 不知道如何将 JSP 转换为 Servlet。 所以,

          1. 检查您的 tomcat-embed-core-.jar 的版本。

          2. 转到相应的版本发布 https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper

          3. 复制依赖,它看起来像-

          <dependency>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat-jasper</artifactId>
                  <version>9.0.37</version> 
          </dependency> 
          

          这里版本需要根据你的tomcat-embed-core jar 版本更新

          1. 更新 Maven 项目,以便它可以从 repo 下载 jar。

          现在你可以开始你的项目了。

          谢谢。

          【讨论】:

            【解决方案7】:

            为什么有两个独立的 java 类(MyFirstAppApplication,ServletInitializer)扩展 SpringBootServletInitializer 类?

            删除 ServletInitializer.java 并将配置方法从 ServletInitializer 移动到 MyFirstAppApplication。

            【讨论】:

              【解决方案8】:

              我面临同样的问题,我通过以下更改解决了这个问题:

              @SpringBootApplication
              public class MyFirstAppApplication extends SpringBootServletInitializer {
                  public static void main(String[] args) {
                      SpringApplication.run(MyFirstAppApplication.class, args);
                  }
              }
              
              protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                  return application.sources(MyFirstAppApplication.class);
              }
              

              无需创建ServletInitializer 新课程。

              【讨论】:

                【解决方案9】:

                以下步骤为我解决了问题。

                1. pom中的包装改为war:

                  &lt;packaging&gt;war&lt;/packaging&gt;

                2. application.properties中,添加:

                  spring.mvc.view.prefix=/WEB-INF/views/

                  spring.mvc.view.suffix=.jsp

                  spring.main.web-application-type=SERVLET

                (或者视图解析器的 java 配置也适用于我)

                1. pom 中,添加这些额外的依赖项:

                  <dependency>
                       <groupId>org.apache.tomcat.embed</groupId>
                       <artifactId>tomcat-embed-jasper</artifactId>
                       <scope>runtime</scope>
                  </dependency>
                   <dependency>
                       <groupId>javax.servlet</groupId>
                       <artifactId>jstl</artifactId>
                       <version>1.2</version>
                   </dependency>
                  
                2. war 运行为 uber jar:

                  java -jar warfilename.war

                【讨论】:

                  猜你喜欢
                  • 2014-12-21
                  • 2012-12-10
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-07-06
                  • 1970-01-01
                  • 2011-11-30
                  • 1970-01-01
                  相关资源
                  最近更新 更多