学习参考自易百教程
1、程序目录如下
2、controller:
package com.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.model.User;
@Controller
public class RegisterController {
@RequestMapping(value="/register",method=RequestMethod.GET)
public ModelAndView register(){
User user=new User();
//user.setHoppies(new String[]{"看书","看电影","玩游戏","垃板胡","画画"});
user.setGender(0); //设定默认值
ModelAndView modelAndView=new ModelAndView("register","command",user);//带参重定向
return modelAndView;
}
@RequestMapping(value="getUserInfo",method=RequestMethod.POST)
public String getInfo(@ModelAttribute("SpringWeb")User user,Model model){
model.addAttribute("id",user.getId());
model.addAttribute("userName",user.getUserName());
model.addAttribute("password",user.getPassword());
model.addAttribute("gender",user.getGender());
model.addAttribute("hoppies",user.getHoppies());
model.addAttribute("country",user.getCountry());
model.addAttribute("remark",user.getRemark());
return "show";
}
@ModelAttribute("countriesList") //映射列表属性名,给视图中的列表赋值
public Map<String,String> getCountryList(){
Map<String,String> countriesList=new HashMap<String,String>();
countriesList.put("US","America");
countriesList.put("CN","China");
countriesList.put("JP","Japan");
countriesList.put("HK","HongKong");
return countriesList;
}
@ModelAttribute("hoppiesList")
public List<String> getHoppyList(){
List<String> hoppies=new ArrayList<String>();
hoppies.add("看书");
hoppies.add("电影");
hoppies.add("玩游戏");
hoppies.add("垃板胡");
hoppies.add("画画");
return hoppies;
}
}
3、User
package com.model;
public class User {
private String id;
private String userName;
private String password;
private int gender;
private String[] hoppies;
private String country;
private String remark;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String[] getHoppies() {
return hoppies;
}
public void setHoppies(String[] hoppies) {
this.hoppies = hoppies;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
4、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>springMVC_register</display-name>
<!-- 配置编码过滤器,解决post请求中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5、spring-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6、register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'register.jsp' starting page</title>
</head>
<body>
<form:form action="/springMVC_register/getUserInfo" method="post">
<table>
<caption>User Register</caption>
<tr>
<td><form:hidden path="id" value="1001"/></td>
</tr>
<tr>
<td><form:label path="userName">用户名</form:label></td>
<td><form:input path="userName"/></td>
</tr>
<tr>
<td><form:label path="password">密码</form:label></td>
<td><form:password path="password"/></td>
</tr>
<tr>
<td><form:label path="gender">性别</form:label></td>
<td>
<form:radiobutton path="gender" label="男" value="0"/>
<form:radiobutton path="gender" label="女" value="1"/>
</td>
</tr>
<tr>
<td><form:label path="hoppies">爱好</form:label></td>
<td><form:checkboxes path="hoppies" items="${hoppiesList}"/></td>
</tr>
<tr>
<td><form:label path="country">国籍</form:label></td>
<td>
<form:select path="country">
<form:option label="请选择——" value="none"/>
<form:options items="${countriesList}"/>
</form:select>
</td>
</tr>
<tr>
<td><form:label path="remark">备注</form:label></td>
<td><form:textarea path="remark" cols="30" rows="3"></form:textarea></td>
</tr>
<tr>
<td><input type="reset" value="reset"/></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form:form>
</body>
</html>
7、show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.jsp' starting page</title>
</head>
<body>
<table>
<tr><td>id:</td><td>${id}</td></tr>
<tr><td>userName:</td><td>${userName}</td></tr>
<tr><td>password:</td><td>${password}</td></tr>
<tr><td>gender</td><td>${(gender==0?'男':'女')}</td></tr>
<tr><td>hoppies:</td><td>
<%
String[] hoppies=(String[])request.getAttribute("hoppies");
for(String hoppy: hoppies){
out.println(hoppy);
}
%>
</td></tr>
<tr><td>country:</td><td>${country}</td></tr>
<tr><td>remark:</td><td>${remark}</td></tr>
</table>
</body>
</html>
结果