spring-boot使用JSP的Demo

注意要点:
1  使用idea的时候,Pom引入tomcat-embed-jasper的时候不能有<scope>provided</scope>
2  @RestController是Controller和ResponseBody的结合,不会跳转JSP会返回字符串
3  启动类Application要在根目录(参考项目结构图)

项目结构

spring-boot使用JSP的Demo


*******************Controller*******************

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;

@Controller
public class ExampleController {
@RequestMapping(value = "/index")
public String index() {
return "index";
}
@RequestMapping("/name")
public String helloJsp(String username,Map map){
map.put("name", username);
return "name";
}
}


*******************Application 启动类*******************

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

}


*******************application.properties配置文件*******************

#jsp 支持
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

server.port=8082


*******************index.jsp放在main/webapp/WEB-INF/jsp/目录下*******************

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="name" method="post">
用户名:<input type="text" name="username"/><br>
<input type="submit" value="提交"/><br>

</form>
</form>

</body>
</html>


*******************name.jsp放在main/webapp/WEB-INF/jsp/目录下*******************

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello ${name} !!!
</body>
</html>


*******************POM文件*******************

<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>

<groupId>com.hao</groupId>
<artifactId>SpringBootJsp5</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.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>

<!-- jstl支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- tomcat 的支持.-->
<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> idea必须删除-->
</dependency>

</dependencies>

</project>


运行main方法后,在浏览器输入http://localhost:8082/index

spring-boot使用JSP的Demo

输入一个用户名,跳转name

spring-boot使用JSP的Demo