标签:springmvc
一、创建工程,搭建springmvc
1. 配置wem.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 配置 DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 --> <!-- 实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的. 默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml --> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--乱码处理 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2. 创建springmvc-servlet.xml配置文件,并存放WEB-INF下
<?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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定扫描的包 --> <context:component-scan base-package="com"></context:component-scan> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置视图 BeanNameViewResolver 解析器: 使用视图的名字来解析视图 --> <!-- 通过 order 属性来定义视图解析器的优先级, order 值越小优先级越高 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="100"></property> </bean> <!-- 配置国际化资源文件 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n"></property> </bean> <!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
二、基本类型和包装类型绑定
1、基本类型绑定
@RequestMapping("baseType.do") @ResponseBody public String baseType(int age){ return "age:"+age; }
2、包装类型绑定
@RequestMapping("baseType2.do") @ResponseBody public String baseType2(Integer age){ return "age:"+age; }
3、JSP页面
<form action="baseType.do"> <h1>基本数据类型</h1> 年龄:<input type="text" name="age"> <input type="submit" value="提交"> </form> <hr/> <form action="baseType2.do"> <h1>包装数据类型</h1> 年龄:<input type="text" name="age"> <input type="submit" value="提交"> </form>
4、基本数据类型和包装类绑定区别?
基本类型不能为空,如果为空,出现404错误
包装类型可以为空
三、对象的数据绑定
1、简单对象和多层级对象
创建实体类UserInfo和Department
UserInfo类
package com.liu; public class UserInfo { private Integer id; private String name; private Integer age; private Department depart; public Department getDepart() { return depart; } public void setDepart(Department depart) { this.depart = depart; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "UserInfo [id=" + id + ", name=" + name + ", age=" + age + ", depart=" + depart + "]"; } }
Department类
package com.liu; public class Department { private Integer deId; private String deName; public Integer getDeId() { return deId; } public void setDeId(Integer deId) { this.deId = deId; } public String getDeName() { return deName; } public void setDeName(String deName) { this.deName = deName; } @Override public String toString() { return "Department [deId=" + deId + ", deName=" + deName + "]"; } }
2、绑定代码:
@RequestMapping("object.do") @ResponseBody public String object(UserInfo user){ return "UserInfo:"+user; }
3、JSP页面
<form action="object.do"> <h1>对象数据绑定</h1> 编号:<input type="text" name="id"/><br/> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"><br/> 部门编号:<input type="text" name="depart.deId"><br/> 部门名称:<input type="text" name="depart.deName"><br/> <input type="submit" value="提交"> </form>
4. 同属性多对象绑定
@RequestMapping("object2.do") @ResponseBody public String object2(UserInfo user,Admin admin){ return user+" "+admin; } @InitBinder("userInfo") public void initUser(WebDataBinder binder){ binder.setFieldDefaultPrefix("user."); } @InitBinder("admin") public void initAdmin(WebDataBinder binder){ binder.setFieldDefaultPrefix("admin."); }
页面内容
<form action="object.do"> <h1>同属性多对象</h1> user编号:<input type="text" name="user.id"/><br/> user姓名:<input type="text" name="user.name"/><br/> user年龄:<input type="text" name="user.age"><br/> admin编号:<input type="text" name="admin.id"/><br/> admin姓名:<input type="text" name="admin.name"/><br/> admin年龄:<input type="text" name="admin.age"><br/> <input type="submit" value="提交"> </form>
三、数组、list、set、map数据绑定
1、数组
代码:
@RequestMapping("array.do") @ResponseBody public String array(String[] name){ StringBuffer sb = new StringBuffer(); for(String s:name){ sb.append(s).append(","); } return sb.toString(); }
JSP页面:
<form action="array.do"> <h1>数组</h1> 姓名:<input type="text" name="name"/><br/> 姓名:<input type="text" name="name"/><br/> 姓名:<input type="text" name="name"/><br/> 姓名:<input type="text" name="name"/><br/> <input type="submit" value="提交"> </form>
2、list绑定
创建类UserListForm:
package com.liu; import java.util.List; public class UserListForm { private List<UserInfo> user; public List<UserInfo> getUser() { return user; } public void setUser(List<UserInfo> user) { this.user = user; } @Override public String toString() { return "UserListForm [user=" + user + "]"; } }
Controller类:
@RequestMapping("list.do") @ResponseBody public String list(UserListForm userListForm){ return userListForm.toString(); }
JSP页面信息:
<form action="list.do"> <h1>List</h1> 编号:<input type="text" name="user[0].id"/><br/> 姓名:<input type="text" name="user[0].name"/><br/> 年龄:<input type="text" name="user[0].age"><br/> 编号:<input type="text" name="user[1].id"/><br/> 姓名:<input type="text" name="user[1].name"/><br/> 年龄:<input type="text" name="user[1].age"><br/> <input type="submit" value="提交"> </form>
3、Set绑定
创建UserSetForm:
package com.liu; import java.util.HashSet; import java.util.Set; public class UserSetForm { private Set<UserInfo> user; public UserSetForm() { user = new HashSet(); user.add(new UserInfo()); user.add(new UserInfo()); } public Set<UserInfo> getUser() { return user; } public void setUser(Set<UserInfo> user) { this.user = user; } @Override public String toString() { return "UserSetForm [user=" + user + "]"; } }
Controller类
@RequestMapping("set.do") @ResponseBody public String set(UserSetForm userSetForm){ return userSetForm.toString(); }
JSP页面:
<form action="set.do"> <h1>Set</h1> 编号:<input type="text" name="user[0].id"/><br/> 姓名:<input type="text" name="user[0].name"/><br/> 年龄:<input type="text" name="user[0].age"><br/> 编号:<input type="text" name="user[1].id"/><br/> 姓名:<input type="text" name="user[1].name"/><br/> 年龄:<input type="text" name="user[1].age"><br/> <input type="submit" value="提交"> </form>
4、Map绑定
创建UserMapForm
package com.liu; import java.util.Map; public class UserMapForm { public Map<String,UserInfo> users; public Map<String, UserInfo> getUsers() { return users; } public void setUsers(Map<String, UserInfo> users) { this.users = users; } @Override public String toString() { return "UserMapForm [users=" + users + "]"; } }
Controller类:
@RequestMapping("map.do") @ResponseBody public String map(UserMapForm userMapForm){ return userMapForm.toString(); }
JSP页面
<form action="map.do"> <h1>Map</h1> 编号:<input type="text" name=‘users["x"].id‘/><br/> 姓名:<input type="text" name=‘users["x"].name‘/><br/> 年龄:<input type="text" name=‘users["x"].age‘><br/> 编号:<input type="text" name=‘users["y"].id‘/><br/> 姓名:<input type="text" name=‘users["y"].name‘/><br/> 年龄:<input type="text" name=‘users["y"].age‘><br/> <input type="submit" value="提交"> </form>
本文出自 “老牛Java” 博客,谢绝转载!
标签:springmvc
原文地址:http://liuyj.blog.51cto.com/2340749/1846914