再温struts 2のタイプ変換


ビューページに入力するパラメータは文字データのみで、特定のjavaタイプに変換して使用する必要があります.
struts 2は強力なタイプ変換機能を提供し、OGLL式に基づいているので、HTML入力項目を合法的なOGLL式と命名すればstruts 2のタイプ変換メカニズムを使用することができます.
また,開発者は自分のタイプ変換器を容易に定義し,文字型から複合タイプへの変換を完了することができる.
OGLL式によるタイプ変換
一例で言えば.
<%@page language="java" contentType="text/html; charset=gbk"%>
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
	"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>    </title>
	</head>
	
	<body>
		<center>
		<form action="login.action" method="post">
		<table>
			<tr>
				<td>
					<p>  :</p>
				</td>
				<td>
					<input type="text" name="user.name" />
				</td>
			</tr>
			
			<tr>
				<td>
					<p>  :</p>
				</td>
				<td>
					<input type="text" name="user.password" />
				</td>
			</tr>
			
			<tr>
				<td>
					<p>  :</p>
				</td>
				<td>
					<input type="text" name="birth" />
				</td>
			</tr>
			<tr>
				<td>
					<input type="submit" value="  " />
				</td>
				<td>
					<input type="reset" value="  " />
				</td>
			</tr>
		</table>
		</form>
		</center>
	</body>
</html>

action:
public class LoginAction{
	
	private UserBean user;
	private Date birth;	
	
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public UserBean getUser() {
		return user;
	}
	public void setUser(UserBean user) {
		this.user = user;
	}

	public String execute(){
		if(getUser().getName().equals("a")&&getUser().getPassword().equals("a")){		
			return "success";
		}
		return "error";
	}
}

アクション構成:
<action name="login" class="ch5.conversion.LoginAction">
            <result name="success">/conversion/result.jsp</result>
            <result name="error">/conversion/error.jsp</result>
        </action>

result.jsp:
<%@page contentType="text/html; charset=gbk" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    
</head>

<body>
<s:property value="user.name"/>
<s:property value="user.password"/>
<s:property value="birth"/>
<p>    </p>
</body>
</html>

また、UserBean:
public class UserBean {
	
	private String name;
	private String password;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

内蔵コンバータ
struts 2には、次のような内蔵コンバータがあります.
booleanとBoolean
shortとShort
CharとCharacter
intとInteger
longとLong
floatとFloat
doubleとDouble
Date
Stringタイプ要素の配列
Stringタイプ要素の集合
開発者は自分のタイプの変換器を書くことができて、先にこれを言わないでください.次に、Dateを例にstruts 2のタイプ変換を説明します.
public class LoginAction {
	
	private Date birth;
	
	
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	
	public String execute(){
		if(birth != null){
			return "success";
		}
		return "error";
	}
}

アクションの構成:
<action name="login" class="ch5.conversion.LoginAction">
            <result name="success">/conversion/result.jsp</result>
            <result name="error">/conversion/error.jsp</result>
        </action>

login.jspで:
<%@page language="java" contentType="text/html; charset=gbk"%>
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
	"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>    </title>
	</head>
	
	<body>		
		<form action="conversionlogin.action" method="post">		
			<input type="text" name="birth" />			
			<input type="submit" value="  " />				
			<input type="reset" value="  " />				
		
		</form>		
	</body>
</html>

result.jsp:
<%@page contentType="text/html; charset=UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    
</head>

<body>
<s:property value="birth"/>
<p>    </p>
</body>
</html>

login.jspページにyyy-mm-dd形式の日付を入力すると自動的に変換されます.
変換例外
変換中に異常が発生した場合、conversionErrorブロッカーによって処理され、変換異常をfieldErrorにカプセル化し、inputビューに移行します.