- 浏览: 118664 次
- 性别:
- 来自: 北京
最新评论
-
coyoc:
楼主封装得不错,不过部分方法是不是没实现完整,不像生产在用的吧 ...
spring data jpa的动态查询封装 -
lee1177:
hyf_0528 写道我单元测试 ...
spring data jpa的动态查询封装 -
zhuyuqu840123:
能给代码吗?
quartz+spring-集群版任务动态载入及修改 -
hyf_0528:
我单元测试了下,如果solveTeam是Event实体类多对多 ...
spring data jpa的动态查询封装 -
hanyi_:
...
quartz+spring-集群版任务动态载入及修改
spring mvc 几种数据绑定
收集spring mvc 几种数据绑定,记录备用
org.springframework.beans.PropertyEditorRegistrySupport.java
[java] view plaincopy
- /**
- * Actually register the default editors for this registry instance.
- */
- private void doRegisterDefaultEditors() {
- this.defaultEditors = new HashMap<Class, PropertyEditor>(64);
- // Simple editors, without parameterization capabilities.
- // The JDK does not contain a default editor for any of these target types.
- this.defaultEditors.put(Charset.class, new CharsetEditor());
- this.defaultEditors.put(Class.class, new ClassEditor());
- this.defaultEditors.put(Class[].class, new ClassArrayEditor());
- this.defaultEditors.put(Currency.class, new CurrencyEditor());
- this.defaultEditors.put(File.class, new FileEditor());
- this.defaultEditors.put(InputStream.class, new InputStreamEditor());
- this.defaultEditors.put(Locale.class, new LocaleEditor());
- this.defaultEditors.put(Pattern.class, new PatternEditor());
- this.defaultEditors.put(Properties.class, new PropertiesEditor());
- this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
- this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());
- this.defaultEditors.put(URI.class, new URIEditor());
- this.defaultEditors.put(URL.class, new URLEditor());
- // Default instances of collection editors.
- // Can be overridden by registering custom instances of those as custom editors.
- this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
- this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
- this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
- this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
- this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
- // Default editors for primitive arrays.
- this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
- this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());
- // The JDK does not contain a default editor for char!
- this.defaultEditors.put(char.class, new CharacterEditor(false));
- this.defaultEditors.put(Character.class, new CharacterEditor(true));
- // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
- this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
- this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));
- // The JDK does not contain default editors for number wrapper types!
- // Override JDK primitive number editors with our own CustomNumberEditor.
- this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
- this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
- this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
- this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
- this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
- this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
- this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
- this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
- this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
- this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
- this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
- this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
- this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
- this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
- // Only register config value editors if explicitly requested.
- if (this.configValueEditorsActive) {
- StringArrayPropertyEditor sae = new StringArrayPropertyEditor();
- this.defaultEditors.put(String[].class, sae);
- this.defaultEditors.put(short[].class, sae);
- this.defaultEditors.put(int[].class, sae);
- this.defaultEditors.put(long[].class, sae);
- }
- }
1.基本数据类型
很简单,该怎么写就怎么写.
controller代码
[java] view plaincopy
- @RequestMapping
- public void test1(String name, Integer age, Double income, Boolean isMarried, String[] interests)
- {
- System.out.println("简单数据类型绑定=========");
- System.out.println("名字:" + name);
- System.out.println("年龄:" + age);
- System.out.println("收入:" + income);
- System.out.println("已结婚:" + isMarried);
- System.out.println("兴趣:");
- for (String interest : interests)
- {
- System.out.println(interest);
- }
- System.out.println("====================");
- }
jsp代码
[xhtml] view plaincopy
- <form action="test1.do" method="post">
- <div class="f_left">姓名:</div>
- <div class="f_left"><input name="name" value="张三"/></div>
- <div class="clear"></div>
- <div class="f_left">年龄:</div>
- <div class="f_left"><input name="age" value="20"/></div>
- <div class="clear"></div>
- <div class="f_left">收入:</div>
- <div class="f_left"><input name="income" value="100000"/></div>
- <div class="clear"></div>
- <div class="f_left">结婚:</div>
- <div class="f_left">
- <input type="radio" name="isMarried" value="true" checked="checked"/>是
- <input type="radio" name="isMarried" value="false"/>否</div>
- <div class="clear"></div>
- <div class="f_left">兴趣:</div>
- <div class="f_left">
- <input type="checkbox" name="interests" value="听歌" checked="checked"/>听歌
- <input type="checkbox" name="interests" value="书法" checked="checked"/>书法
- <input type="checkbox" name="interests" value="看电影" checked="checked"/>看电影
- </div>
- <div class="clear"></div>
- <div><input type="submit" value="提交表单"/></div>
- </form>
2.简单对象类型
与基本类型相拟,只不过绑定到对象上更加简洁.(类拟struts的ActionForm)
controller代码
[java] view plaincopy
- @RequestMapping
- public void test2(User user)
- {
- System.out.println("简单对象类型绑定=========");
- System.out.println("名字:" + user.getName());
- System.out.println("年龄:" + user.getAge());
- System.out.println("收入:" + user.getIncome());
- System.out.println("已结婚:" + user.getIsMarried());
- System.out.println("========================");
- }
user代码
[java] view plaincopy
- public class User {
- private String name;
- private Integer age;
- private Boolean isMarried;
- private Double income;
- private String[] interests;
- 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;
- }
- public Boolean getIsMarried() {
- return isMarried;
- }
- public void setIsMarried(Boolean isMarried) {
- this.isMarried = isMarried;
- }
- public Double getIncome() {
- return income;
- }
- public void setIncome(Double income) {
- this.income = income;
- }
- public String[] getInterests() {
- return interests;
- }
- public void setInterests(String[] interests) {
- this.interests = interests;
- }
- }
jsp代码
[xhtml] view plaincopy
- <form action="test2.do" method="post">
- <div class="f_left">姓名:</div>
- <div class="f_left"><input name="name" value="lxf"/></div>
- <div class="clear"></div>
- <div class="f_left">年龄:</div>
- <div class="f_left"><input name="age" value="20"/></div>
- <div class="clear"></div>
- <div class="f_left">收入:</div>
- <div class="f_left"><input name="income" value="100000"/></div>
- <div class="clear"></div>
- <div class="f_left">结婚:</div>
- <div class="f_left">
- <input type="radio" name="isMarried" value="true" checked="checked"/>是
- <input type="radio" name="isMarried" value="false"/>否</div>
- <div class="clear"></div>
- <div class="f_left">兴趣:</div>
- <div class="f_left">
- <input type="checkbox" name="interests" value="听歌" checked="checked"/>听歌
- <input type="checkbox" name="interests" value="书法" checked="checked"/>书法
- <input type="checkbox" name="interests" value="看电影" checked="checked"/>看电影
- </div>
- <div class="clear"></div>
- <div><input type="submit" value="提交表单"/></div>
- </form>
3.List类型绑定
网上很多人都给不出答案,关键在于,List需要绑定在对象(ActionForm),直接写在request-mapping函数的参数是不行的,更重要的一点是要创建对象(ArrayList).
controller代码
[java] view plaincopy
- @RequestMapping
- public void test3(Club club)
- {
- System.out.println("list类型绑定=========");
- System.out.println("会员:");
- for (User user : club.getUsers())
- {
- System.out.println(user.getName());
- }
- System.out.println("========================");
- }
Club代码
[java] view plaincopy
- public class Club {
- private List<User> users = new ArrayList<User>();
- public List<User> getUsers() {
- return users;
- }
- public void setUsers(List<User> users) {
- this.users = users;
- }
- }
jsp代码
[xhtml] view plaincopy
- <form action="test3.do" method="post">
- <div class="f_left">会员1号:</div>
- <div class="f_left">
- <input name="users[0].name" value="张三"/>
- </div>
- <div class="clear"></div>
- <div class="f_left">会员2号:</div>
- <div class="f_left">
- <input name="users[1].name" value="李四"/>
- </div>
- <div class="clear"></div>
- <div class="f_left">会员3号:</div>
- <div class="f_left">
- <input name="users[2].name" value="王五"/>
- </div>
- <div class="clear"></div>
- <div><input type="submit" value="提交表单"/></div>
- </form>
4.Set类型绑定
与List类型相似,经测试表明,绑定Set数据时,必须先在Set对象中Add相应的数量的模型对象.
controller代码
[java] view plaincopy
- @RequestMapping
- public void test4(Club2 club)
- {
- System.out.println("set类型绑定=========");
- System.out.println("会员:");
- for (User user : club.getUsers())
- {
- System.out.println(user.getName());
- }
- System.out.println("========================");
- }
Club2代码
[java] view plaincopy
- public class Club2 {
- private Set<User> users = new LinkedHashSet<User>();
- public Club2() {
- users.add(new User());
- users.add(new User());
- users.add(new User());
- }
- public Set<User> getUsers() {
- return users;
- }
- public void setUsers(Set<User> users) {
- this.users = users;
- }
- }
jsp代码
[xhtml] view plaincopy
- <form action="test4.do" method="post">
- <div class="f_left">会员1号:</div>
- <div class="f_left">
- <input name="users[0].name" value="张三"/>
- </div>
- <div class="clear"></div>
- <div class="f_left">会员2号:</div>
- <div class="f_left">
- <input name="users[1].name" value="李四"/>
- </div>
- <div class="clear"></div>
- <div class="f_left">会员3号:</div>
- <div class="f_left">
- <input name="users[2].name" value="王五"/>
- </div>
- <div class="clear"></div>
- <div><input type="submit" value="提交表单"/></div>
- </form>
5.Map类型绑定
最灵活的一种方式,可无限绑定未定义的参数,注意必须绑定在对象下(ActionForm).
controller代码
[java] view plaincopy
- @RequestMapping
- public void test5(UserForm userForm)
- {
- System.out.println("Map类型绑定=========");
- System.out.println("名字:" + userForm.getUser().get("name"));
- System.out.println("年龄:" + userForm.getUser().get("age"));
- System.out.println("收入:" + userForm.getUser().get("income"));
- System.out.println("已结婚:" + userForm.getUser().get("isMarried"));
- System.out.println("========================");
- }
UserForm.java代码
[java] view plaincopy
- public class UserForm {
- private Map<String, String> userMap = new LinkedHashMap<String, String>();
- private User user = new User();
- public Map<String, String> getUserMap() {
- return userMap;
- }
- public void setUserMap(Map<String, String> userMap) {
- this.userMap = userMap;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- }
jsp代码
[xhtml] view plaincopy
- <form action="test5.do" method="post">
- <div class="f_left">姓名:</div>
- <div class="f_left"><input name="user['name']" value="张三"/></div>
- <div class="clear"></div>
- <div class="f_left">年龄:</div>
- <div class="f_left"><input name="user['age']" value="20"/></div>
- <div class="clear"></div>
- <div class="f_left">收入:</div>
- <div class="f_left"><input name="user['income']" value="100000"/></div>
- <div class="clear"></div>
- <div class="f_left">结婚:</div>
- <div class="f_left">
- <input type="radio" name="user['isMarried']" value="true" checked="checked"/>是
- <input type="radio" name="user['isMarried']" value="false"/>否</div>
- <div class="clear"></div>
- <div><input type="submit" value="提交表单"/></div>
- </form>
6.复合类型绑定
controller代码
[java] view plaincopy
- @RequestMapping
- public void test6(UserForm userForm)
- {
- System.out.println("复合类型绑定=========");
- System.out.println("名字:" + userForm.getUser().getName());
- System.out.println("年龄:" + userForm.getUser().getAge());
- System.out.println("收入:" + userForm.getUser().getIncome());
- System.out.println("已结婚:" + userForm.getUser().getIsMarried());
- System.out.println("========================");
- }
UserForm代码
[java] view plaincopy
- public class UserForm {
- private Map<String, String> userMap = new LinkedHashMap<String, String>();
- private User user = new User();
- public Map<String, String> getUserMap() {
- return userMap;
- }
- public void setUserMap(Map<String, String> userMap) {
- this.userMap = userMap;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- }
jsp代码
[xhtml] view plaincopy
- <form action="test6.do" method="post">
- <div class="f_left">姓名:</div>
- <div class="f_left"><input name="user.name" value="张三"/></div>
- <div class="clear"></div>
- <div class="f_left">年龄:</div>
- <div class="f_left"><input name="user.age" value="20"/></div>
- <div class="clear"></div>
- <div class="f_left">收入:</div>
- <div class="f_left"><input name="user.income" value="100000"/></div>
- <div class="clear"></div>
- <div class="f_left">结婚:</div>
- <div class="f_left">
- <input type="radio" name="user.isMarried" value="true" checked="checked"/>是
- <input type="radio" name="user.isMarried" value="false"/>否</div>
- <div class="clear"></div>
- <div><input type="submit" value="提交表单"/></div>
- </form>
相关推荐
这是一个基于Spring MVC、Mybatis和Spring框架实现的个人博客系统,涵盖了Web开发中的后端架构设计、数据库管理和前端展示等多个方面。以下将详细介绍这个系统的关键知识点: **1. Spring MVC** Spring MVC是Spring...
7. **Jackson或Gson**:用于JSON数据的序列化和反序列化,Spring MVC默认支持这两种库,便于前后端数据交换。 8. **Hibernate或MyBatis**:如果应用需要持久化数据,这些ORM(对象关系映射)框架可以与Spring MVC...
Spring MVC 的核心处理流程主要包括以下几个步骤: 1. **请求接收**:客户端发送 HTTP 请求到服务器。 2. **前端控制器**:DispatcherServlet 接收到请求并调用 HandlerMapping。 3. **处理映射**:HandlerMapping ...
Spring MVC还支持数据绑定、验证和本地化等特性,提高了开发效率和代码可维护性。 Spring框架是Java企业级应用的核心,它提供了依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming...
Spring MVC 内置了几种 HandlerMapping 实现,如基于注解的 HandlerMapping,可以根据 @RequestMapping 注解来匹配请求。 **4. Controller** Controller 是处理用户请求的组件,通常由 Java 类实现,其中的方法称为...
SPRING MVC 请求参数获取的几种方法 SPRING MVC 框架中,获取请求参数是非常重要的一步,下面将介绍 SPRING MVC 中获取请求参数的几种方法。 1. 使用 @PathVariable 注解获取路径中传递参数 在 SPRING MVC 中,...
- **可配置性**:Spring MVC提供了丰富的配置选项,包括但不限于拦截器、消息转换器和数据绑定等。 #### 二、Spring MVC的优势 Spring MVC之所以受到广大开发者的青睐,主要是因为它具备以下几个显著优势: 1. **...
Spring MVC 提供了几种内置的 HandlerMapping 实现,例如,基于注解的 HandlerMapping 可以根据方法上的 @RequestMapping 注解进行映射。 5. **HandlerAdapter** HandlerAdapter 适配器模式的体现,它使得 ...
Spring MVC提供了一种优雅的方式来组织和分离Web应用的各个部分,MyBatis简化了数据库操作,而ZVM则帮助监控整个系统的运行状态。这些文档和手册将为学习者提供详细的步骤和最佳实践,以帮助他们更好地理解和应用...
此外,Spring MVC还支持输入验证和数据绑定,异常处理和国际化支持等功能,大大提高了Web应用的开发效率。 在数据持久层方面,Spring MVC提供了对持久层技术的支持,包括JDBC、Hibernate以及ibatis等流行的持久层...
获取请求数据通常涉及以下几种方式: - `HttpServletRequest`对象:在拦截器中,可以通过`HttpServletRequest`对象来获取请求参数、请求头、请求URL等信息。例如,`request.getParameter("key")`可以获取指定参数名...
Spring MVC提供了一种模型-视图-控制器(MVC)架构,帮助开发者分离业务逻辑、数据处理和用户界面展示。 【描述】"自己搭建的mybits + StringMVC框架,除了日志外其他没问题" 暗示开发者已经成功地整合了mybits和...
它是Spring生态体系中的重要组成部分,提供了一种模型-视图-控制器(MVC)的架构模式,帮助开发者将业务逻辑、数据处理和用户界面进行解耦。 在"spring mvc 入门Demo"中,我们可以学习到以下几个核心概念和步骤: ...
`<mvc:annotation-driven>`:这是 Spring MVC 的一个重要注解,它启用了 Spring MVC 对注解的支持,例如 `@RequestMapping`, `@RequestParam`, `@PathVariable` 等,使得我们可以使用注解来处理请求映射和参数绑定。...
MyBatis允许开发者编写SQL语句,通过XML或注解的方式将SQL与Java代码绑定,从而实现灵活的数据查询和更新。在这个项目中,MyBatis被用来连接Oracle数据库,执行增删改查操作。 3. **Maven**:Maven是Java项目的构建...
总之,Spring MVC 提供了一种高效、灵活的方式来构建 Web 应用,它的注解驱动和组件扫描使得代码更简洁,可维护性更强。通过不断的实践和学习,你可以掌握更多高级特性和最佳实践,进一步提升开发效率。
8. 输入验证与数据绑定:展示了如何在Spring MVC中对用户输入进行验证和将数据绑定到模型。 9. 异常处理:介绍了在Spring MVC中如何处理异常。 10. 国际化支持:讲解了Spring MVC如何支持多语言环境下的应用开发。...
3. **类型安全**:相比于Struts2的ActionForm,Spring MVC的模型绑定更加类型安全,减少了运行时错误。 4. **更强大的依赖注入**:Spring框架提供了全面的依赖注入功能,可以方便地管理对象的生命周期和依赖关系。 5...