Spring Boot和JSP一起使用
在这个以JSP作为视图的Spring Boot例子中,我们将看到如何将JSP作为Spring Boot的视图使用,以及需要做些什么额外的配置。
首先要做的是在创建Maven项目时将包装选择为" war"。
如果我们正在使用STS,则在创建新的Spring入门项目时,请在" New Spring Starter Project"中选择打包作为war。
如果我们使用的是eclipse,则在创建Maven项目时在原型选择中选择一个webapp项目。
在这里查看使用STS创建Spring Boot应用程序的示例使用Spring Tool Suite(STS)的Spring Boot示例
需要的Starter依赖项:
- spring-boot-starter-web
- spring-boot-starter-tomcat
项目结构
此Spring Boot JSP示例的项目结构应如下所示。
Maven – pom.xml
pom.xml应该具有给定的依赖性。编译JSP文件需要以下依赖关系。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootwebdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootWebDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Spring Boot MVC – JSP文件
在示例中,这些是3个JSP文件。
- home.jsp –登陆页面,提供了开始用户注册过程的链接。
- userregister.jsp –具有输入框的JSP,用于输入绑定到用户对象的用户数据。
- user.jsp –使用上一个" userregister"页面中绑定的用户对象显示用户数据。
home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring BOOT MVC Example - Home JSP</title>
</head>
<body>
<body>
<div>${message}</div>
<a href="/registerUser">Register User</a>
</body>
</body>
</html>
userregister.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>User Registration</title>
</head>
<body>
<!-- Values entered here are bound to the properties of
user object assigned here to modelAttribute -->
<form:form action="/showUser" modelAttribute="user" method="post">
<table>
<tr>
<td>
<form:label path="firstName">First Name</form:label>
</td>
<td>
<form:input path="firstName" id="firstname" />
</td>
</tr>
<tr>
<td>
<form:label path="lastName">Last Name</form:label>
</td>
<td>
<form:input path="lastName" id="lastname" />
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
user.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>User Data</title>
</head>
<body>
<table>
<tr>
<td>First Name: ${User.firstName}</td>
</tr>
<tr>
<td>Last Name: ${User.lastName}</td>
</tr>
</table>
</body>
</html>
Spring Boot MVC –控制器类
UserController.java文件,它指定到方法的URL映射。
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.model.User;
@Controller
@RequestMapping(value = "/")
public class UserController {
@GetMapping("/register")
public String showHome(Model model) {
model.addAttribute("message", "Spring Boot MVC Example");
return "home";
}
@RequestMapping(value = "/registerUser", method = RequestMethod.GET)
public String registerUser(Model model) {
// Add new user to model to be bound with view (JSP)
model.addAttribute(new User());
return "userregister";
}
@RequestMapping(value = "/showUser", method = RequestMethod.POST)
public String showUser(@ModelAttribute("user") User user, Model model) {
model.addAttribute("User", user);
return "user";
}
}
Spring Boot应用程序类
具有main方法的应用程序类扩展了SpringBootServletInitializer类,并覆盖了它的configure方法。
SpringBootServletInitializer是一个自以为是的WebApplicationInitializer,用于从传统的WAR部署中运行SpringApplication。它将Servlet,Filter和ServletContextInitializer Bean从应用程序上下文绑定到服务器。
要配置Web应用程序,我们需要重写configure(SpringApplicationBuilder)方法。
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 SpringBootWebDemoApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebDemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootWebDemoApplication.class, args);
}
}
配置视图解析器
要将解析器视图显示为JSP文件,可以在application.properties中配置InternalResourceViewResolver,如下所示。
application.properties
spring.mvc.view.prefix: /WEB-INF/JSP/ spring.mvc.view.suffix: .jsp
运行应用程序
我们可以通过执行具有主要方法的SpringBootWebDemoApplication类来运行该应用程序。
成功启动应用程序后,我们可以根据控制器映射使用URL访问应用程序。
http:// localhost:8080 / register
http:// localhost:8080 / registerUser
http:// localhost:8080 / showUser

