标签:exce index strong rom system info version oid 2.3
1.在struts中,表单提供的常见数据类型struts框架自动转换,无需手动转换
2.在某些情况下,某些自定义类型struts不能完成自动转换,需要进行手动转换,如果需要转换的类型转换频率较高时,手动转换的代码增多,这时可以使用struts的类型转换器来进行转换
编写实体类: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 + ")"; } }
编写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; } }
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>
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>
标签:exce index strong rom system info version oid 2.3
原文地址:https://www.cnblogs.com/aihuadung/p/10011342.html