Spring Boot对jsp的支持

     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>&nbsp;登录
					</button>
				</div>
				<div class="btn-group">
					<button type="submit" class="btn btn-success" id="loginbtn">
						<span class="glyphicon glyphicon-log-in"></span>&nbsp;注册
					</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

输入用户名、密码点击【登录】按钮。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云淡风轻58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值