1、配置Web.xml文件,将请求交给SpringMVC处理
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- SpringMVC -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
<init-param>
<span style="white-space:pre"> </span><param-name>contextConfigLocation</param-name>
<span style="white-space:pre"> </span><param-value>classpath:spring-servlet.xml</param-value>
</init-param
<span style="white-space:pre"> </span> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
2、编写User.java
public class User implements Serializable{
private static final long serialVersionUID = 1736050259982800329L;
private String userName;
private String password;
private String realName;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
3、编写处理请求的控制器——
UserController
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/register")
public ModelAndView createUser(User user){
ModelAndView mav = new ModelAndView();
mav.setViewName("user/createSuccess");
mav.addObject("user",user);
return mav;
}
@RequestMapping("/registerUI")
public String register(){
return "user/register";
}
}
4、编写视图对象(WEB-INF/user/)——register.jsp
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册用户</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/register.html" method="post">
<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 type="text" name="realName"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
createSuccess.jsp
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户创建成功</title>
</head>
<body>
<span style="white-space:pre"> </span>恭喜,${user.realName}注册成功,你的用户名是${user.userName},密码是${user.password},请牢记!!!
</body>
</html>
5、编写springmvc-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.zsm.learn.spring.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
注:springmvc配置文件默认