action类PersonAction:
package com.wjf;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport{ private Date birthday; public Date getBirthday() {
System.out.println("调用getBirthday的get方法");
return birthday;
} public void setBirthday(Date birthday) {
System.out.println("调用setBirthday的set方法");
this.birthday = birthday;
}
public String execute(){
System.out.println(birthday);
return SUCCESS;
}
}日期转换器DateConverter:
public class DateConverter extends DefaultTypeConverter{
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
if(toType == Date.class){
System.out.println("转换成Date类型");
String[] strs = (String[])value;
try {
return df.parse(strs[0]);
} catch (ParseException e) {
e.printStackTrace();
}
}else if(toType == String.class){
System.out.println("转换成String类型");
Date date = (Date)value;
return df.format(date);
}
return super.convertValue(context, value, toType);
}
}输出get.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>getDate</title>
</head>
<body>
${birthday } <br/>
<s:property value="birthday"/>
</body>
</html>
请问:
${birthday }和<s:property value="birthday"/>这两种写法有什么不一样?为什么第一种写法,在Date转换
为string的时候没有调用自定义的转化器?而第二种调用了?初学者,麻烦大家解释下
学学ognl就知道两者区别了