先建maven war建立完然后在
pom.xml 文件
补充:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.25.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
</dependencies>
右点击项目选择properties----maven--project facts----java 1.7
targeted runtime----apatche tomcatev7.0打钩 ---apply
Deploy右点击选择奶瓶点击生成web.xml
然后在web.xml
</welcome-file-list>后补充
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
然后在java Resource-----src/main/resources----下放spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 开启组件扫描功能:Spring会递归扫描cn.tedu.beans包中的全部类,查找相关注解,如果找到就将注解标注的类,创建为java Bean对象 -->
<context:component-scan base-package="cn.tedu.spring"></context:component-scan>
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="prefix" value="/templates/"/>
<property name="suffix" value=".html"/>
<property name="characterEncoding" value="utf-8"/>
<property name="templateMode" value="HTML"/>
<property name="cacheable" value="false"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="utf-8"/>
</bean>
</beans>
java Resource---src/main/java--cn.tedu.spring---实例类User.java
package cn.tedu.spring;
public class User {
private String username;
private String password;
private Integer age;
private String phone;
private String email;
public User() {
// TODO Auto-generated constructor stub
super();
System.out.println("创建user类的对象");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + ", phone=" + phone
+ ", email=" + email + "]";
}
}
然后写UserController.java
package cn.tedu.spring;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("reg.do")
public String showReg() {
System.out.println("UserController.showReg");
return "reg";
}
@RequestMapping("login.do")
public String showLogin() {
System.out.println("UserController.showLogin");
return "login";
}
@RequestMapping("handle_reg.do")
public String handleReg(User user) {
System.out.println("UserController.showReg");
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
System.out.println("age:"+user.getAge());
System.out.println("phone:"+user.getPhone());
System.out.println("email:"+user.getEmail());
System.out.println(user.toString());
return "reg_success";
}
@RequestMapping("handle_login.do")
public String handleLogin(String username,String password,HttpServletRequest request) {
System.out.println("UserController.handleLogin");
if("root".equals(username)) {
if("123".equals(password)) {
return "login_success";
}
else {
request.setAttribute("errorMessage","登录失败,密碼错误");
return "error";
}
}else {
request.setAttribute("errorMessage","登录失败,用戶名错误");
return "error";
}
}
}
src/main/resources---templates----reg.html/login.html/error.html/login_success.html/reg_success.html
reg.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>
<h1>用户注册</h1>
<form action="handle_reg.do">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>年龄</td>
<td><input name="age"></td>
</tr>
<tr>
<td>手机号码</td>
<td><input name="phone"></td>
</tr>
<td>电子邮箱</td>
<td><input name="email"></td>
</tr>
<tr>
<td colspan="2"> <input type="submit"> </td>
</tr>
</table>
</form>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录</h1>
<form action="handle_login.do" method="post">
<div>
<label>用户</label>
<input type="text" name="username">
</div>
<div>
<label>密码</label>
<input type="password" name="password">
</div>
<input type="submit" value="登录">
</form>
</body>
</html>
error.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录失败</h1>
<h3 th:text=${errorMessage}></h3>
</body>
</html>
reg_success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>注册成功</h1>
</body>
</html>
login_success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
run as-----http://localhost:8080/springmvc4/login.do---