strutsローカル、グローバルタイプ変換器
8909 ワード
ステップ01:beanの作成
ステップ02:actionクラスの作成:
ステップ03:コンバータクラスの作成
ステップ04:propertyファイルの作成
propertyファイル:action兄弟ディレクトリに配置
property命名:Transform-conversion.properties(actionクラス名-conversion.properties)
lilan:actionの中のオブジェクトを指します
lilan.birthday:オブジェクト.属性=変換
ステップ05:strutsを構成する.xml
ステップ06:データ入力インタフェースの作成
ステップ07:データ表示画面の作成
/*************以上がローカルタイプ変換器、以下がグローバルタイプ変換器****************/
手順01:propertyファイルの名前を(xwork-conversion.properties)に変更します.
ステップ02:propertyファイルをsrcディレクトリに移動
ステップ03:property構成:(ここでは、エンジニアリングの下ですべてのDateタイプが、このコンバータを使用していることを示します)
package com.self.bean;
import java.util.Date;
public class User {
private Date birthday = null;
public Date getBirthday() {
System.out.println(" -User:" + birthday);
return birthday;
}
public void setBirthday(Date birthday) {
System.out.println(" -User:" + birthday);
this.birthday = birthday;
}
}
ステップ02:actionクラスの作成:
package com.self.action;
import com.self.bean.User;
public class Transform {
private User lilan;
public Transform() {
}
public String showdata() {
return "tsuccess";
}
public User getLilan() {
return lilan;
}
public void setLilan(User lilan) {
this.lilan = lilan;
}
}
ステップ03:コンバータクラスの作成
package com.self.conversion;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
/**
* :DataTransform-conversion.properties
*/
/* 01-- DefaultTypeConverter: */
public class DataTransform extends DefaultTypeConverter {
/* 02-- convertValue */
@Override
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
System.out.println(" ");
/*03-- SimpleDateFormat */
/*04-- action “-conversion.properties” properties
* :Transform-conversion.properties
* : =
* :birthday=com.self.action.DataTransform
*/
SimpleDateFormat dataFormat=new SimpleDateFormat("yyyyMMdd");
try {
if(toType==Date.class){// Date
String[] params=(String[]) value;// ,Request.getParameterValues();
System.out.println(" :Date:"+params[0]);
return dataFormat.parse(params[0]);
}else if(toType==String.class){// String
Date date=(Date) value;
System.out.println(" :String:"+date);
return dataFormat.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
ステップ04:propertyファイルの作成
propertyファイル:action兄弟ディレクトリに配置
property命名:Transform-conversion.properties(actionクラス名-conversion.properties)
lilan.birthday=com.self.conversion.DataTransform
lilan:actionの中のオブジェクトを指します
lilan.birthday:オブジェクト.属性=変換
ステップ05:strutsを構成する.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="do"/>
<package name="transform" namespace="/" extends="struts-default">
<action name="list_*" class="com.self.action.Transform" method="{1}">
<result name="tsuccess">
/outdata.jsp
</result>
</action>
</package>
</struts>
ステップ06:データ入力インタフェースの作成
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
<form action="list_showdata.do">
:<input name="lilan.birthday" type="text" value="20150120" />
<BR>
01 :20150120
<BR>
02 :2015-01-20
<BR>
<input type="submit" value=" ">
</form>
</body>
</html>
ステップ07:データ表示画面の作成
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
${lilan.birthday }
</body>
</html>
/*************以上がローカルタイプ変換器、以下がグローバルタイプ変換器****************/
手順01:propertyファイルの名前を(xwork-conversion.properties)に変更します.
ステップ02:propertyファイルをsrcディレクトリに移動
ステップ03:property構成:(ここでは、エンジニアリングの下ですべてのDateタイプが、このコンバータを使用していることを示します)
java.util.Date=com.self.conversion.DataTransform