1. 程式人生 > >Struts2的型別轉換器不起作用

Struts2的型別轉換器不起作用

    看了書上關於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"/>標籤向頁面輸出時才呼叫了這個方法。