Spring Boot建议使用HTML来完成动态页面,但是由于大多数Java Web项目之前的页面都是使用jsp来完成,所以Spring Boot同时也支持使用jsp。
程序清单:/springboot2/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>com.dwx</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot2</name>
<description>hello project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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>
<!-- 添加 web 依赖. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加 servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加 JSTL(JSP Standard Tag Library,JSP标准标签库) -->
<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>
<!-- Jasper是tomcat中使用的JSP引擎,运用tomcat-embed-jasper可以将项目与tomcat分开 -->
<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>
</project>
程序清单:/springboot2/src/main/resources/application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
程序清单:/springboot2/src/main/webapp/WEB-INF/jsp/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css"/>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">spring boot对jsp的支持</h3>
</div>
</div>
<div class="container">
<div class="row">
<form action="login" method="post">
<div class="form-group">
<div class="input-group col-md-4">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input class="form-control" placeholder="用户名" type="text" name="userName">
</div>
</div>
<div class="form-group">
<div class="input-group col-md-4">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input class="form-control" placeholder="密码" type="password" name="passWord">
</div>
</div>
<div class="btn-group">
<button type="submit" class="btn btn-success" id="loginbtn">
<span class="glyphicon glyphicon-log-in"></span> 登录
</button>
</div>
<div class="btn-group">
<button type="submit" class="btn btn-success" id="loginbtn">
<span class="glyphicon glyphicon-log-in"></span> 注册
</button>
</div>
</form>
</div>
</div>
</body>
</html>
程序清单:/springboot2/src/main/webapp/WEB-INF/jsp/main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主页</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css"/>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">商品信息列表</h3>
</div>
</div>
<div class="container">
<div class="row"><span>欢迎【${sessionScope.userName }】</span></div>
<div class="row">
<div class="col-md-4">
<img alt="" src="img/Apple iPad.jpg" height="200">
<h4>Apple ipad</h4>
<p>3999.00</p>
</div>
<div class="col-md-4">
<img alt="" src="img/iPhone X.jpg" height="200">
<h4>iPhone X</h4>
<p>10999.00</p>
</div>
<div class="col-md-4">
<img alt="" src="img/vivo_nex.jpg" height="200">
<h4>vivo nex</h4>
<p>2999.00</p>
</div>
</div>
</div>
</body>
</html>
程序清单:/springboot2/src/main/java/com/dwx/hello/loginController.java
package com.dwx.hello;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class loginController {
@RequestMapping("/loginForm")
public String loginForm() {
return "login";
}
@RequestMapping("/mainForm")
public String main() {
return "main";
}
@RequestMapping("/login")
public String login(@RequestParam("userName")String userName,
@RequestParam("passWord")String passWord,HttpSession session) {
System.out.println(userName);
session.setAttribute("userName", userName);
return "redirect:mainForm";
}
}
Spring Boot项目启动后,在浏览器中输入URL测试应用。
http://localhost:8080/loginForm
输入用户名、密码点击【登录】按钮。