看了书上关于struts2的类型转换器后觉得挺明白,结果一试一大堆bug。发现使用模型驱动时局部配置一直不起作用,先贴代码:
注册表单:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/convert" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
生日:<input type="text" name="birthday"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
动作类:
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="myPack" extends="struts-default">
<global-results>
<result name="input">/error.jsp</result>
</global-results>
<action name="convert" class="com.hncj.convert.UserAction" method="doUser">
<result>/success.jsp</result>
</action>
</package>
</struts>
转换器配置方法有两种:一、局部配置 二、全局配置
一、局部配置文件 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<User>{
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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="user.username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="text" name="user.birthday"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
动作类:
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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="user.birthday"/>//这里可以调用convertToString方法
<s:debug></s:debug>
successfully
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="default" extends="struts-default">
<action name="test" class="com.hncj.edu.TestAction">
<result>success.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
局部配置:TestAction-conversion.properties
user.birthday=com.hncj.edu.MyTypeConverter
全局配置:xwork-conversion.properties
java.util.Date=com.hncj.edu.MyTypeConverter
两种都能起作用。
还有一个问题就是输出类型转换的属性时,覆写的StrutsTypeConverter方法中提供的convertToString一直未调用,后来发现在使用<s:property value="xx"/>标签向页面输出时才调用了这个方法。