Spring Download - where to place jsp files

I'm 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 ran into a problem matching jsp files.

Failed to allow browsing with the name "hello" to the servlet named "dispatcherServlet"

I have many answers in SO, but no one seems to solve my problem - I do not plan to use any template mechanisms, since I will have a lot of jsps to consider - maybe the plan after installing w760> is up.

I have a project structure as shown 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

Code placement 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
+5
6

, jsp

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

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

+3

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>
+1

@EnableWebMvc Spring Boot. @SpringBootApplication -. @EnableWebMvc , DispatcherServlet , .

+1

Java (MyFirstAppApplication, ServletInitializer), SpringBootServletInitializer?

ServletInitializer.java configure ServletInitializer MyFirstAppApplication.

0

, , :

@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 .

0

If you use Jetty or Tomcat as the built-in servlet container, just change the packaging from jar to war and run it with java -jar ...

0
source

Source: https://habr.com/ru/post/1677753/


All Articles