1.Converter:
Struts.xml:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts> < Packagename= "Converterdemo"extends= "Struts-default"> <Actionname= "point-*"class= "Cn.zzsxt.action.MyPointAction"Method= "{1}"> <resultname= "Success">/success.jsp</result> </Action> </ Package></Struts>
Conversion:
Packagecn.zzsxt.conversion;ImportJava.util.Map;ImportOrg.apache.struts2.util.StrutsTypeConverter;ImportCn.zzsxt.entity.MyPoint; Public classMypointconverterextendsStrutstypeconverter {/*** 10,20 * Convert string to Composite type * Context: Context * Values: Parameter's value * ToType: Target type*/@Override PublicObject convertfromstring (Map context, string[] values, Class totype) {if(Totype==mypoint.class) {String value= Values[0];//gets the value of the coordinate: 10,20string[] arr = Value.split (",");//arr = {"10,20"}//gets the value of the x-coordinate intx = Integer.parseint (arr[0]); inty = Integer.parseint (arr[1]); MyPoint MyPoint=NewMyPoint (); Mypoint.setx (x); Mypoint.sety (y); returnMyPoint; } return NULL; } /*** Convert a composite type to a string*/@Override PublicString converttostring (Map arg0, Object arg1) {return NULL; }}
Action
Packagecn.zzsxt.action;ImportCom.opensymphony.xwork2.ActionSupport;ImportCn.zzsxt.entity.MyPoint; Public classMypointactionextendsActionsupport {PrivateMyPoint Point; Publicmypoint GetPoint () {returnPoint ; } Public voidsetpoint (MyPoint point) { This. Point =Point ; } PublicString Add ()throwsException {System.out.println ("The Add method is executed with the coordinates:" +Point ); return This. SUCCESS; }}
Action Package properties file (Simple Type):
Point=cn.zzsxt.conversion.mypointconverter
Conversion Package properties File (composite type):
Cn.zzsxt.entity.mypoint=cn.zzsxt.conversion.mypointconverter
INDEX.JSP:
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' index.jsp ' starting page</title> </Head> <Body> <formAction= "Point-add.action"Method= "POST">Please enter coordinates (x, y)<inputtype= "text"name= "point"><BR> <inputtype= "Submit"value= "Submit"> </form> </Body></HTML>
SUCCESS.JSP:
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' index.jsp ' starting page</title> </Head> <Body>X=${point.x}<BR>Y=${point.y}<BR> </Body></HTML>
Java:struts Frame 5 (converter,validation,tags)