看了书上关于struts2的类型转换器后觉得挺明白,结果一试一大堆bug。发现使用模型驱动时局部配置一直不起作用,先贴代码:
注册表单:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title here
动作类:
package com.hncj.convert;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private Date birthday;
private String password;
private String username; //这里将属性直接写入action
public Date getBirthday() {
return birthday;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public String doUser() {
System.out.println(this.getBirthday());
System.out.println(this.username);
System.out.println(this.password);
return "success";
}
}
转换类:
package com.hncj.convert;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class DateConverter extends StrutsTypeConverter{
private DateFormat[] formats = new SimpleDateFormat[] {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy.MM.dd"),
new SimpleDateFormat("yyyy_MM_dd")
};
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
String date = values[0];
for(int i = 0 ; i < formats.length ; i++) {
try {
return formats[i].parse(date);
} catch (ParseException e) {
continue;
}
}
return null;
}
@Override
public String convertToString(Map context, Object o) {
return new SimpleDateFormat("yyyy-MM-dd").format(o);
}
}
struts.xml
/error.jsp
/success.jsp
转换器配置方法有两种:一、局部配置 二、全局配置
一、局部配置文件 UserAction-conversion.properties
birthday=com.hncj.convert.DateConverter
二、全局配置文件 xwork-conversion.properties
java.util.Date=com.hncj.convert.DateConverter
这种方式将属性直接写入action类中的方法,两种配置都没有问题。但使用模型驱动,将对象作为属性放入action中时,局部配置不起效果,很是郁闷,无奈只好改成全局配置,下面是代码,有没有大神帮忙看一下?
其他页面不变,action页面如下:
package com.hncj.edu;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class TestAction extends ActionSupport implements ModelDriven{
private User user = new User();
public String execute() throws Exception {
System.out.println(user.getBirthday());
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
User实体类:
package com.hncj.edu;
import java.util.Date;
public class User {
private String username;
private String password;
private Date birthday;
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
无论局部配置是这样(TestAction-conversion.properties):
birthday=com.hncj.edu.MyTypeConverter
还是这样:
user.birthday=com.hncj.edu.MyTypeConverter
都不起作用,有没有大佬解答一下?当然,全局配置还是可以的。
后来又这样试了一下,直接用模型传值:
注册表单:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title here
动作类:
package com.hncj.edu;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class TestAction extends ActionSupport{
private User user;
public String execute() throws Exception {
System.out.println(user.getBirthday());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
成功视图:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
Insert title here
//这里可以调用convertToString方法
successfully
struts.xml
success.jsp
index.jsp
局部配置:TestAction-conversion.properties
user.birthday=com.hncj.edu.MyTypeConverter
全局配置:xwork-conversion.properties
java.util.Date=com.hncj.edu.MyTypeConverter
两种都能起作用。
还有一个问题就是输出类型转换的属性时,覆写的StrutsTypeConverter方法中提供的convertToString一直未调用,后来发现在使用