struts-基础内容-6-数据的自动封装和类型转换
来源:互联网 发布:大数据的价值体现在 编辑:程序博客网 时间:2024/05/17 19:56
请求数据的自动封装
实现的原理-参数处理拦截器
<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
方式1.jsp表单数据填充到action中的属性
<%-- Created by IntelliJ IDEA. User: cxspace Date: 16-7-9 Time: 下午11:39 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body> <form action="${pageContext.request.contextPath}/user_register.action" method="post"> 用户名:<input type="text" name="name"> <br> 密码:<input type="password" name="pwd"> <br> 年龄:<input type="text" name="age"> <br> 生日: <input type="text" name="birth"> <br> <input type="submit" value="注册"> </form></body></html>
import java.util.Date;/** * * struts核心业务,请求数据自动封装及类型转换 * Created by cxspace on 16-7-10. */public class UserAction { //拿到请求数据 private String name; private String pwd; private int age; private Date birth; //必须要给set方法get可以不用给 public void setName(String name) { this.name = name; } public void setPwd(String pwd) { this.pwd = pwd; } public void setAge(int age) { this.age = age; } public void setBirth(Date birth) { this.birth = birth; } //处理注册请求 public String register(){ System.out.println(name); System.out.println(pwd); System.out.println(age); System.out.println(birth); return "success"; }}
方式2.jsp表单数据填充到action中的对象中的属性-对象类型一定要给get方法
User
package com.cx.type;import java.util.Date;/** * Created by cxspace on 16-7-10. */public class User { //拿到请求数据 private String name; private String pwd; private int age; private Date birth; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; }}
Action
package com.cx.type;/** * * struts核心业务,请求数据自动封装及类型转换 * Created by cxspace on 16-7-10. */public class UserAction { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } //必须要给set方法get可以不用给 //处理注册请求 public String register(){ System.out.println(user.getName()); System.out.println(user.getPwd()); System.out.println(user.getAge()); System.out.println(user.getBirth()); return "success"; }}
jsp
<%-- Created by IntelliJ IDEA. User: cxspace Date: 16-7-9 Time: 下午11:39 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body> <form action="${pageContext.request.contextPath}/user_register.action" method="post"> 用户名:<input type="text" name="user.name"> <br> 密码:<input type="password" name="user.pwd"> <br> 年龄:<input type="text" name="user.age"> <br> 生日: <input type="text" name="user.birth"> <br> <input type="submit" value="注册"> </form></body></html>
类型转换-转换器的使用
Strus中 jsp提交的数据,struts会自动转换为action中属性的类型
对于基本数据类型以及日期类型会自动转换
日期类型只支持yyyy-MM-dd格式
需要自定义类型转换器
局部类型转换器
全局类型转换器
Struts转换器api
|----TypeConverter 转换器接口
|----默认类型转换器类
|----StrutsTypeConverter 转换器类
把19900318 ==> 1990-03-18
一.局部类型转换
1.写转换器类
package com.cx.type;import org.apache.struts2.util.StrutsTypeConverter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Map;/** * Created by cxspace on 16-7-10. */public class MyConverter extends StrutsTypeConverter { /* * * 把String 转换到特定类型 * * * context当期上下问环境 * * values表单中字符串值 * * * */ @Override public Object convertFromString(Map context, String[] values, Class aClass) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); try { return sdf.parse(values[0]); } catch (ParseException e) { throw new RuntimeException(e); } } @Override public String convertToString(Map map, Object o) { return null; }}
2.配置转换器类
在同包的action目录下新建属性配置文件
该文件命名规则:ActionClassName-conversion.properties
3.转换内容
配置文件内容-包含要转换的属性和对应的处理类
user.birth=com.cx.type.MyConverter
user.birth=转换器类全路径(com.cx.type.MyConverter)
4.这个转换器不能给其他的action用
二.全局转换器
写一个转换器给所有action用
-->在src目录下建xwork-conversion.properties
-->内容
转换的类型=转换器全路径
package com.cx.type;import org.apache.struts2.components.Date;import org.apache.struts2.util.StrutsTypeConverter;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Map;/** * Created by cxspace on 16-7-10. */public class MyConverter extends StrutsTypeConverter { /* * * 把String 转换到特定类型 * * * context当期上下问环境 * * values表单中字符串值 * * * */ //如果项目中要支持的类型很多 yyyy-MM-dd/yyyyMMdd/yyyy年MM月dd日 DateFormat [] df = { new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyy年MM月dd日") }; //定义项目中支持的转换格式 @Override public Object convertFromString(Map context, String[] values, Class aClass) { //判断内容不能为空 if (values==null||values.length==0) { return null; } //判断类型必须为Date if(Date.class!=aClass) { return null; } //转换失败下一次循环 for(int i=0 ; i < df.length ; i++) { try { return df[i].parse(values[0]); } catch (ParseException e) { continue; } } return null; } @Override public String convertToString(Map map, Object o) { return null; }}
0 0
- struts-基础内容-6-数据的自动封装和类型转换
- Struts请求数据的自动封装以及类型转换
- 关于Struts请求数据自动封装以及类型转换(封装异常)
- struts封装请求、类型转换、数据验证
- struts2请求数据自动封装和类型转换
- 基本数据的自动类型转换和强制类型转换
- struts类型自动转换
- struts 数据自动封装表单的三种方式
- SSH框架之Struts的请求数据自动封装
- Struts核心业务之请求数据的自动封装
- 第二十一篇 Java 数据类型的定义 ,以及基础类型的自动转换和强制转换
- Struts2请求数据自动封装和数据类型转换
- Struts2的知识整理总结(三)—— 数据封装和类型转换
- 类的自动类型转换和强制类型转换
- 类的自动类型转换和强制类型转换
- java的自动类型转换和强制类型转换
- 类的自动类型转换和强制类型转换
- Java:自动类型转换和强制类型转换的规则
- struts基础内容-2-配置详解
- struts-基础内容-3-action开发,路径,常量
- struts-基础内容-4-全局配置
- canvas动画之一 -- 百分比进度加载
- struts-基础内容-5-数据处理,数据回显,模型驱动
- struts-基础内容-6-数据的自动封装和类型转换
- struts-基础内容-7-文件上传
- struts-基础内容-8-文件下载
- struts-核心内容-9-拦截器
- javaweb-c3p0连接池
- struts-核心内容-10-Ognl表达式语言
- Android系列之Service组件的应用
- struts-10-主要标签
- struts-11-表单数据校验