一.在Servlet中,表單提交的非字元串類型需要手動轉換 1.在struts中,表單提供的常見數據類型struts框架自動轉換,無需手動轉換 2.在某些情況下,某些自定義類型struts不能完成自動轉換,需要進行手動轉換,如果需要轉換的類型轉換頻率較高時,手動轉換的代碼增多,這時可以使用strut ...
一.在Servlet中,表單提交的非字元串類型需要手動轉換
1.在struts中,表單提供的常見數據類型struts框架自動轉換,無需手動轉換
2.在某些情況下,某些自定義類型struts不能完成自動轉換,需要進行手動轉換,如果需要轉換的類型轉換頻率較高時,手動轉換的代碼增多,這時可以使用struts的類型轉換器來進行轉換
二.類型轉換
使用類型轉換的步驟
- 編寫類型轉換器,繼承StrutsTypeConverter
- 編寫xwork-conversion.properties配置文件,內容為:要轉換的類型=類型轉換器
案例:
編寫實體類:Point

package com.ahd.entity; 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; } @Override public String toString() { return "(" + x + "," + y + ")"; } }Point
編寫PointAction

package com.ahd.action; import com.ahd.entity.Point; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class PointAction extends ActionSupport{ private Point point; public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println(point.getX()+point.getY()); System.out.println(point); return Action.SUCCESS; } }PointAction
xwork-conversion.properties
com.ahd.entity.Point=com.ahd.converter.PointConverter
編寫類型轉換器:PointTypeConverter,繼承StrutsTypeConverter類,並重寫convertFromString方法和convertToString方法,
package com.ahd.converter; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.ahd.entity.Point; public class PointConverter extends StrutsTypeConverter{ @Override public Object convertFromString(Map context, String[] values, Class toClass) { // TODO Auto-generated method stub String value=values[0]; Point point=new Point(); int x=Integer.parseInt(value.substring(0, value.indexOf(","))); int y=Integer.parseInt(value.substring( value.indexOf(",")+1,value.length())); point.setX(x); point.setY(y); return point; } @Override public String convertToString(Map context, Object o) { // TODO Auto-generated method stub Point point=(Point)o; return "("+point.getX()+","+point.getY()+")"; } }
struts.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default" namespace=""> <action name="point" class="com.ahd.action.PointAction"> <result>/success.jsp</result> <result name="input">/input.jsp</result> </action> </package> </struts>struts.xml
web.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app id="WebApp_ID"> <display-name>struts2_02_modelDrive</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>web.xml