當前位置:編程學習大全網 - 源碼下載 - spring boot怎麽訪問jsp頁面

spring boot怎麽訪問jsp頁面

舉例說明:

Project Structure

└─main

├─java

│ └─com

│ └─henry

│ └─jsp

│ SampleWebJspController.java

└─resources

│ application.properties

└─META-INF

└─resources

└─WEB-INF

└─jsp

welcome.jsp

pom file

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<version>${spring.boot.version}</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<version>8.0.28</version>

</dependency>

java code

@Controller

@EnableAutoConfiguration

public class SampleWebJspController extends SpringBootServletInitializer {

@Value("${application.message:Hello World}")

private String message = "Hello World";

@RequestMapping("/")

public String welcome(Map<String, Object> model) {

model.put("time", new Date());

model.put("message", this.message);

return "welcome";

}

public static void main(String[] args) throws Exception {

SpringApplication.run(SampleWebJspController.class, args);

}

}

welcome.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

${message}

<div>${time}</div>

</body>

</html>

Start the project with main method and access localhost:8080 you can see the page.

Notice

At first I make a directory in src/main like webapp/WEB-INF/jsp and put all jsp in it. When I try to access root path with main method I got 404 page. I found there is no jsp file in jar. I changed the pom file like following

<packaging>war</packaging>

<dependencies>

<!-- dependency here -->

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<version>${spring.boot.version}</version>

</plugin>

</plugins>

</build>

  • 上一篇:在Java中那個開源日誌怎麽使用
  • 下一篇:Linux教程第4版
  • copyright 2024編程學習大全網