Struts2 的類型轉換方法有分成區域性及全域性兩種,以實例來說明這兩種的差別與設定方法。
實作的過程大致是這樣的:客戶端建立一個 X,Y 字串的輸入input.jsp ,然後經過 struts2 的類型轉換後,再顯示於客戶端 pointConverterResult.jsp
Step 01:建立 struts.xml 檔,內容為
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="test" namespace="/test" extends="struts-default"> <action name="Hello" class="com.test.Hello"> <result name="success">/test/Hello.jsp</result> </action> <action name="UserLogin_*" method="{1}" class="com.test.UserLoginAction"> <result name="sign_on">/test/userLogin.jsp</result> <result name="input">/test/userLogin2.jsp</result> <result name="success">/test/userLoginResult.jsp</result> </action> <action name="PointConverter" class="com.test.PointConverterAction"> <result name="success">/test/pointConverterResult.jsp</result> </action> </package> </struts> |
Step 02:建立客戶端的輸入劃面 input.jsp & 處理後的的顥示劃面 pointConverterResult.jsp
input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() +":"+ request.getServerPort() + path; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>" > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Converter Demo</title> </head> <body> point 欄位輸入座標 x,y 取出 x 與 y 值 <s:form name="frm_PointConver" action="PointConverter"> <s:textfield name="point" label="point"/> <s:textfield name="point1" label="point1"/> <s:textfield name="age" label="age"/> <s:textfield name="username" label="userName" /> <s:textfield name="date" label="birthday" /> <s:submit/> </s:form> </body> </html> |
pointConverterResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>point Converter Result</title> </head> <body> Point:<s:property value="point"/> <br> Point1:<s:property value="point1"/> <br> age:<s:property value="age"/><br> username:<s:property value="username"/><br> birthday:<s:property value="date"/><br> </body> </html> |
Step 03:建立 class:Point 於 package: com.test 下,這個 class 很簡單,只是為了設定/取得 X,Y 軸的值
package com.test; public class Point { private int x,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; } } |
Step 04:建立一個自定類型轉換的 class: PointConverter 於 package: com.test 下,因為 struts2 的 JAR 檔內有一個 ognl-x.x.x.jar,此 JAR 包含了一個 class:TypeConverter,這個 class 預設有一個 DefaultTypeConverter method,所以可以利用這個 method 來改寫我們自已想要的類型轉換,文檔可以參考:http://commons.apache.org/proper/commons-ognl/apidocs/index.html
package com.test; import java.util.Date; import java.util.Map; import ognl.DefaultTypeConverter; public class PointConverter extends DefaultTypeConverter { @Override public Object convertValue(Map context, Object value, Class toType) { // TODO Auto-generated method stub //return super.convertValue(context, value, toType); // 從 客戶端 至 伺服器端 if ( Point.class == toType ){ Point point = new Point(); String[] str = (String[]) value; String[] paramValues = str[0].split(","); int x = Integer.parseInt(paramValues[0]); int y = Integer.parseInt(paramValues[1]); point.setX(x); point.setY(y); return point; } // 從伺服器端 至 客戶端 if (String.class == toType){ Point point = (Point) value; int x = point.getX(); int y = point.getY(); String result = "[X:" + x + ",Y:" + y +"]"; return result; } return null; } } |
Step 05: 依 struts.xml 裡指示,建立一個 Action class:PointConverterAction
package com.test; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; /** * @author polin.wei * */ public class PointConverterAction extends ActionSupport { private Point point; private Point point1; private int age; private String username; private Date date; public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } } |
Step 06:在類型轉換有分成區域性及全域性,若是區域性:則在 Action class:PointConverterAction 相同的 package: com.test 下需建立一個檔案 PointConverterAction-conversion.properties (-conversion.properties 是固定的,此檔用來指示 struts2 那一個欄位,[ 如:input.jsp 中的 point 欄位] 在作 setPoint()/getPoint() 前,要先使用此自定義類型 [如: com.test.PointConverter ] 作轉換 );若是全域性:則在 struts.xml 檔案相同目錄下需建立一個檔案:xwork-conversion.properties 來指示 struts2 那一種類型 [如:com.test.Point] 均使用那一個 class [如:com.test.PointConverter ]來作類型轉換。
PointConverterAction-conversion.properties 的內容如下:
point=com.test.PointConverter |
xwork-conversion.properties 的內容如下:
com.test.Point=com.test.PointConverter |
留言列表