1.自定义类型转换器
第一种:局部类型转换器
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateTypeConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMdd");
try {
if (toType == Date.class) {
String[] params = (String[]) value;
//parse转换为日期
return dataFormat.parse(params[0]);
}else if(toType == String.class){
Date date = (Date)value;
//format变成字符串
return dataFormat.format(date);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
在你要转换的action同文件夹下建立文件:
ActionName-conversion.properties ActionName是action的名字
里面:date=com.clannan.type.converter.DateTypeConverter
要转换的字段 = 转换器的包名+全名
第二种:全局类型转换器
xwork-conversion.properties放在src文件夹下
java.util.Date=com.clannan.type.converter.DateTypeConverter
待转换的类型=转换器类全名是
2.request ,session,application属性的访问和添加,操作
//往里面存数据
ServletActionContext.getRequest();//request
ServletActionContext.getRequest().getSession();//session
ServletActionContext.getResponse();//response
ServletActionContext.getServletContext();//application
public String execute(){
ActionContext cxt = ActionContext.getContext();
cxt.getSession().put("message","成功的现实了session里面的中文!");
cxt.put("nickname", "泡泡龙");
return "success";
}
//直接得到session,request,servletContext等东西
public String getSession(){
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("math", "天使");
HttpSession s = request.getSession();
s.setAttribute("math", "恶魔");
ServletContext c = ServletActionContext.getServletContext();
//c.getRealPath();
int num =0;
if(c.getAttribute("number")!=null){
num = (Integer)c.getAttribute("number");
}
c.setAttribute("number", ++num);
return "success";
}
3.上传文件!这个太有用了!