import com.opensymphony.xwork2.ActionSupport;
public class DateAction extends ActionSupport {
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String date(){
System.out.println("处理date中.....");
return SUCCESS;
}
}
分别使用的两种转换器类
package www.csdn.convert;
import java.util.Map;
import www.csdn.domain.User;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class UserTypeConvert extends DefaultTypeConverter {
@Override
public Object convertValue(Map context, Object value,
Class toType) {
//由字符串转换成user对象的时候
if(toType == User.class){
//j将用户请求发过来的参数转换成一个数组
String[] values = (String[]) value;
User entity = new User();
entity.setName(values[0]);
entity.setSex(values[1]);
entity.setEmail(values[2]);
return entity;
}else if(toType == String.class){
//由于String内容本身支持内置的类型转换,可以不用操作,使用user.name直接输出
//也可以将需要转换的值强制转换成user对象类型
User entity = (User) value;
return "姓名:"+entity.getName()+"性别:"+entity.getSex()+"邮箱:"+entity.getEmail();
}
return super.convertValue(context, value, toType);
}
}
package www.csdn.convert;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class DateTypeConvert extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
if(toClass == Date.class){
String dateTime = values[0];
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
try {
return dateFormat.parse(dateTime);
} catch (ParseException e) {
System.out.println("日期格式不正确");
e.printStackTrace();
}
}
return null;
}
@Override
public String convertToString(Map context, Object o) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
return dateFormat.format(o);
}
}
Struts.xml
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
Jsp显示页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<
html>