[標(biāo)題]:[原]Struts2類型轉(zhuǎn)換
[時間]:2009-7-15
[摘要]:Struts2類型轉(zhuǎn)換、全局類型轉(zhuǎn)換、集合類型轉(zhuǎn)換
[關(guān)鍵字]:浪曦視頻,Struts2應(yīng)用開發(fā)系列,WebWork,Apache,類型轉(zhuǎn)換
[環(huán)境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
[作者]:Winty (wintys@gmail.com) http://www.tkk7.com/wintys
[正文]:
在Struts Action中可以自動轉(zhuǎn)換String,Date等類型,但是如果Action中用到自定義類型,就需要使用Struts類型轉(zhuǎn)換。
1、Struts類型轉(zhuǎn)換
自定義類型Point
public class Point {
private int x;
private int y;
//省略getter and setter
}
Struts Action:
public class PointAction extends ActionSupport {
private Point point;
......
}
客戶端輸入:
<s:textfield name="point" label="坐標(biāo)" ></s:textfield>
這樣,從客戶端傳過來point的String值,不能直接set到PointAction中的point對象中。同時,輸出point對象時,直接調(diào)用getter也是不行的。這時,就需要一個類型轉(zhuǎn)換器,實(shí)現(xiàn)Point類與String之間的轉(zhuǎn)換。可以繼承Struts中的ognl.DefaultTypeConverter實(shí)現(xiàn)。
/StrutsHelloWorld/src/wintys/struts2/converter/PointConverter.java:
package wintys.struts2.converter;
import java.util.Map;
import ognl.DefaultTypeConverter;
public class PointConverter extends DefaultTypeConverter {
@SuppressWarnings("unchecked")
@Override
public Object convertValue(Map context, Object value, Class toType) {
//String => Point
if(toType == Point.class){
//獲取request string的字符串?dāng)?shù)組
String[] params = (String[])value;
String param = params[0];
String[] points = param.split(",");
Point point = new Point();
point.setX(Integer.parseInt(points[0]));
point.setY(Integer.parseInt(points[1]));
return point;
}
//Point => String
if(toType == String.class){
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
return "["+x+","+y+"]";
}
return null;
}
}
同時,還需要一個配置文件(須跟PointAction在同一目錄下)PointAction-conversion.properties(名稱為XXXAction-conversion.properties)來告訴Struts2 PointAction中屬性使用哪一個轉(zhuǎn)換器類。
PointAction-conversion.properties:
point=wintys.struts2.converter.PointConverter
2、Struts全局類型轉(zhuǎn)換
對于每一個屬性應(yīng)用轉(zhuǎn)換器,當(dāng)屬性過多時會顯得很繁瑣,這時可以使用Struts全局類型轉(zhuǎn)換。全局類型轉(zhuǎn)換器名稱為xwork-conversion.properties,與struts.xml同在classes目錄下。
xwork-conversion.properties:
#global converter settings
#class to be converted = converter
wintys.struts2.converter.Point=wintys.struts2.converter.PointConverter
3、Struts集合類型轉(zhuǎn)換
當(dāng)PointAction中的屬性是集合類型,跟非集合類型類似。
public class PointAction extends ActionSupport {
private List<Point> points;
......
}
轉(zhuǎn)換器類可以繼承org.apache.struts2.util.StrutsTypeConverter,實(shí)際上StrutsTypeConverter繼承于 ognl.DefaultTypeConverter。
/StrutsHelloWorld/src/wintys/struts2/converter/StrutsPointConverter.java:
package wintys.struts2.converter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
/**
* Struts類型轉(zhuǎn)換: StrutsTypeConverter 繼承于 ognl.DefaultTypeConverter
* @author Winty
* @version 2009-07-14
*
*/
@SuppressWarnings("unchecked")
public class StrutsPointConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
List<Point> points = new ArrayList<Point>();
for(String value : values){
String[] p = value.split(",");
Point point = new Point();
point.setX(Integer.valueOf(p[0]));
point.setY(Integer.valueOf(p[1]));
points.add(point);
}
return points;
}
@Override
public String convertToString(Map context, Object o) {
List<Point> points = (List<Point>)o;
StringBuilder sb = new StringBuilder();
sb.append("[");
for(Point p : points){
sb.append("(");
sb.append(p.getX());
sb.append(",");
sb.append(p.getY());
sb.append(") ");
}
sb.append("]");
return sb.toString();
}
}
當(dāng)然也需要在PointAction-conversion-properties中配置:
PointAction-conversion-properties:
#property = converter
point=wintys.struts2.converter.PointConverter
points=wintys.struts2.converter.StrutsPointConverter
4、詳細(xì)內(nèi)容:
/StrutsHelloWorld/WebRoot/converter/input.jsp:
其中<s:textfield name="point" label="坐標(biāo)" ></s:textfield>
可以換成
<s:textfield name="point.x" label="坐標(biāo).x"/>
<s:textfield name="point.y" label="坐標(biāo).y"/>
此時,Point類無需類型轉(zhuǎn)換,Struts會自己去找point對象的x與y的setter方法。
當(dāng)輸出時,只需重寫Point類的toString()方法即可。
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>請以逗號分割輸入點(diǎn)的坐標(biāo),如:5,3</h1>
<s:form action="pointConverter">
<s:textfield name="point" label="坐標(biāo)" ></s:textfield>
<s:textfield name="points" label="points1"> </s:textfield>
<s:textfield name="points" label="points2"></s:textfield>
<s:textfield name="age" label="年齡"></s:textfield>
<s:textfield name="name" label="姓名"></s:textfield>
<s:textfield name="birthday" label="出生日期"></s:textfield>
<s:submit value="確定"></s:submit>
</s:form>
</body>
</html>
/StrutsHelloWorld/WebRoot/converter/output.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:property value="point"/><br/>
<s:property value="points"/><br/>
<s:property value="age"/><br/>
<s:property value="name"/><br/>
<s:property value="birthday"/> <br/>
</body>
</html>
/StrutsHelloWorld/src/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>
<package name="MyStruts" extends="struts-default">
<action name="pointConverter" class="wintys.struts2.converter.PointAction">
<result name="success">/converter/output.jsp</result>
</action>
</package>
</struts>
/StrutsHelloWorld/WebRoot/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>MyStruts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>MyStruts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
/StrutsHelloWorld/src/wintys/struts2/converter/Point.java:
package wintys.struts2.converter;
/**
*
* @author Winty
* @version 2009-07-12
*/
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
/StrutsHelloWorld/src/wintys/struts2/converter/PointAction.java:
package wintys.struts2.converter;
import java.util.Date;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author Winty
* @verion 2009-07-12
*/
@SuppressWarnings("serial")
public class PointAction extends ActionSupport {
private Point point;
private List<Point> points;//集合類型的轉(zhuǎn)換
private int age;
private String name;
private Date birthday;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
[參考資料]:
《浪曦視頻之Struts2應(yīng)用開發(fā)系列》
[附件]:
源代碼:
http://www.tkk7.com/Files/wintys/StrutsTypeConverter.zip
posted on 2009-07-15 16:51
天堂露珠 閱讀(627)
評論(0) 編輯 收藏 所屬分類:
Struts