Spring Boot - where to place the jsp files

亡梦爱人 提交于 2020-01-10 04:59:05

问题


I am trying to develop a new Spring boot application using MVC as a first step to move my existing Spring MVC application to Spring boot.

However, I am facing an issue with the mapping of jsp files.

Could not resolve view with name 'hello' in servlet with name 'dispatcherServlet'

I have ready many answers in SO, but none seem to solve my issue - I am not planning to use any template engines as I will have a lot of jsps to consider - might be a plan once spring boot is set up.

I have a project structure as below:

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

Placing the code below:

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

回答1:


I have created a demo project which is rendering jsp

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

take checkout then mvn package java -jar target\simple-web-app-tomcat-0.0.1-SNAPSHOT.war URL : http://localhost:8081/w




回答2:


Remove @EnableWebMvc annotation from your Spring Boot class. By default with @SpringBootApplication enables your application as web application. Declaring @EnableWebMvc explicitly will disable the auto configuration and Auto setting up DispatcherServlet will be overridden which is causing you the issue.




回答3:


The issue was with the version of jar spring-boot-starter-parent. For some reason this doesn't work with the version 1.5.3 RELEASE. It works until version 1.5.2 RELEASE.

I have updated the pom.xml's parent tag as below:

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



回答4:


Why do have two separate java classes(MyFirstAppApplication,ServletInitializer) extending SpringBootServletInitializer class ?

Remove ServletInitializer.java & move configure method from ServletInitializer to MyFirstAppApplication.




回答5:


I am facing The Same issue and I remove this problem by following changes:

@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);
}

No need to make ServletInitializer new class.




回答6:


I have gone exactly the same path. Upgrading a jsp, xml-based spring application to spring boot 2. There are few things you need to consider when migrating:

First remove @EnableWebMvc.

Second you need @ComponentScan on top of MyFirstAppApplication class.

Try to read this article, it helped me alot https://htr3n.github.io/2018/12/jsp-spring-boot/

Third you also need this dependeny together with embed-jasper:

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

Last but not least this is a shortcut for creating a view handler in java

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

and as far as I know it only works in spring boot 2. You can have Java implementation instead and debug it to see if it ever hit that.

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

Because in my case for example I didn't even need a specific or default resolver. I had different resolver for each Controller that I had to define in xml/bean and inject in each class.



来源:https://stackoverflow.com/questions/44131612/spring-boot-where-to-place-the-jsp-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!