第一个spring boot web程序

Spring Boot简介

Spring boot是由pivotal团队提供的全新框架,其设计目的是用来简化spring应用的创建、运行、调试、部署等。

Spring Boot解决的问题

Spring boot的出现带来了以下优点:

1.使编码变得更简单:推荐使用注解。

2.使配置变得简单:自动配置、快速构建项目、快速集成新技术的能力。

3.使部署变得简单:内嵌Tomcat、Jetty等web容器。

4.使监控变得简单:自带项目监控。

示例:第一个spring boot web程序(基于Thymeleaf模板引擎)

项目结构:

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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

程序清单:/springboot2/src/main/resources/templates/login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}">
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}"/>
<script type="text/javascript" th:src="@{js/jquery-1.11.0.min.js}"></script>
<script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script>
</head>
<body>
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h3 class="panel-title">第一个spring boot web程序</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/resources/templates/main.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>主页</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}">
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}"/>
<script type="text/javascript" th:src="@{js/jquery-1.11.0.min.js}"></script>
<script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script>
</head>
<body>
	<div class="panel panel-primary"> 
		<div class="panel-heading">
			<h3 class="panel-title">商品信息列表&nbsp;&nbsp;&nbsp;&nbsp;当前用户:<span th:text="${session.userName}"></span></h3>
		</div>
	</div>
	<div class="container">
		<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、付费专栏及课程。

余额充值