先建maven war建立完然后在
pom.xml 文件
补充:
右点击项目选择properties----maven--project facts----java 1.7
targeted runtime----apatche tomcatev7.0打钩 ---apply
Deploy右点击选择奶瓶点击生成web.xml
然后在web.xml
后补充
然后在java Resource-----src/main/resources----下放spring-mvc.xml
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">
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
login.html
error.html
reg_success.html
login_success.html
run as-----http://localhost:8080/springmvc4/login.do---