1.strtus2的自动类型转换:
1)web.xml :
StrutsPrepareAndExecuteFilter org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter *.action *.jsp
2)struts.xml :
/typeConvert.jsp
3)请求页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>Iindex page
4)Action处理类:
package cn.cvu.action;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class ActionTypeConvert extends ActionSupport { //与页面元素同名的属性,并且get/set。struts通过反射机制获取 private String userName; private String passWord; public String typeConvert() { //获取参数的方式一,使用ServletActionContext工具类 HttpServletRequest request = ServletActionContext.getRequest(); String username = request.getParameter("userName"); String password = request.getParameter("passWord"); System.out.println("typeConvert: "+username+","+password+"\n"); //获取参数的方式二,使用struts2自动获取 System.out.println("User's Message: "+userName+","+passWord); return "success"; } 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; }}
5)反馈页面:
typeConvert~~~
6)测试:
2.自定义类型转换:
1)请求页面:
2)Action处理类:
package cn.cvu.action;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class ActionTypeConvert extends ActionSupport { //与页面元素同名的属性,并且get/set。struts通过反射机制获取 private String userName; private String passWord; private Date birthday; public String typeConvert() { //获取参数的方式一,使用ServletActionContext工具类 HttpServletRequest request = ServletActionContext.getRequest(); String username = request.getParameter("userName"); String password = request.getParameter("passWord"); String birthDay = request.getParameter("birthday"); System.out.println("typeConvert: "+username+","+password+","+birthDay+"\n"); //获取参数的方式二,使用struts2自动获取 System.out.println("User's Message: "+userName+","+passWord+","+birthday); return "success"; } public String getUserName() { } public void setUserName(String userName) { } public String getPassWord() { } public void setPassWord(String passWord) { } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; }}
3)类型转换类:
package cn.cvu.action;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;public class DateTypeConvert extends DefaultTypeConverter { //覆盖此方法 //value:源类型,toType:目标类型 public Object convertValue(Object value, Class toType) { System.out.println("value:" + value + "toType:" + toType); //判断输入生日为空 if (value==null || "".equals(value)) { return false; } //判断目标输出类型为空 if (toType==null || "".equals(toType)) { return false; } //判断输出是否为目标类型 if (toType!=java.util.Date.class) { return false; } //确认传入数据位字符串 if (value instanceof java.lang.String[]) { String[] strs = (String[])value; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); try { if (strs != null && strs.length>0) { //按照指定格式返回日期 return sdf.parse(strs[0]); } } catch (ParseException e) { e.printStackTrace(); } } return new Date(); }}
4)属性配置文件:
和Action类统计目录。命名格式:需转换参数所在Action类-conversion.properties 。
birthday=cn.cvu.action.DateTypeConvert
5)测试:
3.错误信息提示:
1)struts.xml配置:
/typeConvert.jsp /errorConvert.jsp
2)错误页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %>Insert title here 类型转换错误!
3)类型转换类:
public class DateTypeConvert extends DefaultTypeConverter { //覆盖此方法 //value:源类型,toType:目标类型 public Object convertValue(Object value, Class toType) { System.out.println("value:" + value + "toType:" + toType); //判断输入生日为空 if (value==null || "".equals(value)) { return false; } //判断目标输出类型为空 if (toType==null || "".equals(toType)) { return false; } //判断输出是否为目标类型 if (toType!=java.util.Date.class) { return false; } //确认传入数据位字符串 if (value instanceof java.lang.String[]) { String[] strs = (String[])value; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); try { if (strs != null && strs.length>0) { //按照指定格式返回日期 return sdf.parse(strs[0]); } } catch (ParseException e) { //如果不抛出异常,struts会认为成功 throw new RuntimeException(e); } } return new Date(); }}
4)测试:
5)中文信息提示:
1》在和Action类同级目录创建自定义名称的properties文件。
xwork.default.invalid.fieldvalue=\u7C7B\u578B\u8F6C\u6362\u6709\u8BEF"{0}"
2》在struts.xml中加载自定义资源文件。
/typeConvert.jsp /errorConvert.jsp
3》测试:
6)针对性中文信息提示:
1》修改properties文件:针对某个属性进行配置。
invalid.fieldvalue.birthday=\u751F\u65E5\u8F6C\u6362\u9519\u8BEF
2》测试:
4.全局类型转换:
首先取消上文中的类型转换配置,重命名ActionTypeConvert-conversion.properties。
1)全局配置文件:
在src下创建指定名称的xwork-conversion.properties文件:
内容:目标类型=目标类型转换类
java.util.Date=cn.cvu.action.DateTypeConvert
2)测试:
5.使用avaBean数据封装:
1)请求页面:
2)JavaBean:
package cn.cvu.bean;public class BeanSinger { private String userName; private String passWord; 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; } @Override public String toString() { return "BeanSinger [userName=" + userName + ", passWord=" + passWord + "]"; } }
3)Action处理类:
package cn.cvu.action;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import cn.cvu.bean.BeanSinger;import com.opensymphony.xwork2.ActionSupport;public class ActionTypeConvert extends ActionSupport { //使用一个JavaBean对数据进行封装 private BeanSinger singer; private Date birthday; public String typeConvert() { //方式一,使用ServletActionContext HttpServletRequest request = ServletActionContext.getRequest(); String username = request.getParameter("singer.userName"); String password = request.getParameter("singer.passWord"); String birthDay = request.getParameter("birthday"); System.out.println("typeConvert: "+username+","+password+","+birthDay+"\n"); //方式二,使用struts2自动化 System.out.println("User's Message: "+singer.toString()+","+birthday); return "success"; } public BeanSinger getSinger() { return singer; } public void setSinger(BeanSinger singer) { this.singer = singer; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; }}
4)测试:
- end