常规的String,int能自动转换,但是,有些类型不是这么简单,比如输入字符串,但需要Date、自定义类型,因此需要自定义类型转换
类型转换器分全局和局部
按惯例,局部的优先级高于全局
需求:
1.输入yyyy-mm-dd,回显yyyy/mm/dd
2.输入电话号码,如果是座机010-11111111,回显(010)11111111
如果是手机,回显18777777777
这里对于需求1,采用局部类型转换器,对于需求2,采用全局类型转环器
Action:C1.java
package org.ah.s2; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class C1 extends ActionSupport implements ModelDriven<Model>{
private Model m;
// ------------------------------------------------------------------
public String m1() {
return "success";
}
@Override
public Model getModel() {
m = new Model();
return m;
}
}
模型层:Model,M2Tel
package org.ah.s2; import java.util.Date; public class Model {
private String uname;
private Date birthday;
private M2Tel tel; public M2Tel getTel() {
return tel;
} public void setTel(M2Tel tel) {
this.tel = tel;
} public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package org.ah.s2; public class M2Tel {
private String areaCode;
private String telCode; public String getAreaCode() {
return areaCode;
} public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
} public String getTelCode() {
return telCode;
} public void setTelCode(String telCode) {
this.telCode = telCode;
} }
表示层核心代码:
index.jsp
<s:form action="m1Action" method="post">
<s:textfield name="uname" />
<s:textfield name="birthday" label="生日"/>
<input type="date" name="birthday"/>
<s:textfield name="tel" label="电话"/>
<s:submit value="测试Struts控件传值"></s:submit>
</s:form>
infoMsg.jsp
<s:property value="uname" />
<s:property value="birthday" />
<s:date name="birthday" format="MM/dd/yy"/>
<br/>
<s:property value="tel" />
<s:debug></s:debug>
errorMsg.jsp无关大局,略
类型转化器
DataConverter.java
package org.ah.s2; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; public class DataConverter extends StrutsTypeConverter { @Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) { if (arg1[0] != null && !"".equals(arg1[0])) {
// 判断一下要转为哪种类型 assignable:可指定的
if (Date.class.isAssignableFrom(arg2)) {
return stringToDate(arg1[0]);
} else if (M2Tel.class.isAssignableFrom(arg2)) {
return stringToTel(arg1[0]);
}
}
return null;
} private Date stringToDate(String inPara) {
System.out.println("String转为Date");
Date date = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
try {
date = fmt.parse(inPara);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
} private M2Tel stringToTel(String inPara) {
System.out.println("String转为Tel");
M2Tel tel = new M2Tel(); if (inPara.indexOf("-") < 0) {
// 手机:indexOf("-")等于-1
tel.setTelCode(inPara);
} else {
// 座机
String[] split = inPara.split("-");
tel.setAreaCode(split[0]);
tel.setTelCode(split[1]);
}
return tel; } @Override
public String convertToString(Map arg0, Object arg1) { String format = "";
if (arg1 instanceof Date) {
return deteToString(arg1);
} else if (arg1 instanceof M2Tel) {
return telToString((M2Tel) arg1);
}
return format;
} private String deteToString(Object Obj_in) {
System.out.println("Date转为String");
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/mm/dd");
return fmt.format(Obj_in);
} private String telToString(M2Tel tel) {
System.out.println("Tel转为String");
String format = tel.getTelCode();
String 区号 = tel.getAreaCode();
if (区号 != null && !"".equals(区号)) {
format = "(" + 区号 + ")" + format;
}
return format;
}
}
配置类型转换器:
局部:和Model放在一起;前缀是“模型类名-”;字段对应转换器
Model-conversion.properties
birthday=org.ah.s2.DataConverter
全局:放在src下,固定命名,Model对应转换器
xwork-conversion.properties
org.ah.s2.M2Tel=org.ah.s2.DataConverter
测试一下 :
输入手机,输出
2017/01/01 01/01/17
18777777777
输入座机,输出
2017/01/01 01/01/17
(010)88888888