First , we use the first method: Inherit the Defaulttypeconverter class under the OGNL package, and do a type conversion:
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Next, write the processing class and create a Pointaction class under the Com.test.action package:
Package Com.test.action;import Java.util.date;import Com.opensymphony.xwork2.actionsupport;import Com.test.bean.point;public class Pointaction extends Actionsupport{private point Point;private point point2;private String username;private int age;private Date birthday;public point GetPoint2 () {return point2;} public void SetPoint2 (point point2) {this.point2 = Point2;} Public Point GetPoint () {return point;} public void SetPoint (point point) {this.point = point;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date brithday) {this.birthday = Brithday;} Public String Execute () throws Exception{return "Success";}}
For the assignment of coordinates, you need to create a new class and create a new class point under the Com.test.bean package:
Package Com.test.bean;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;}}
Then com.test.converter the transformation class pointconverter of the point coordinates under the package (the class needs to inherit the Defaulttypeconverter class under the OGNL package):
Package Com.test.converter;import Java.util.map;import OGNL. Defaulttypeconverter;import Com.test.bean.point;public class Pointconverter extends defaulttypeconverter{@ Overridepublic Object Convertvalue (Map context, Object value, Class totype) {if (Point.class==totype) {string[] str = ( String[]) value; String firstvalue = str[0]; string[] Resultvalue = Firstvalue.split (","); Point point = new Point ();p Oint.setx (Integer.parseint (resultvalue[0));p oint.sety (Integer.parseint (resultvalue[1)) ); return point;} else 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;}}
In addition, we need to do some related configuration let struts2 find this processing class, create a file under Com.test.action: pointaction-conversion.properties, notice that the file after the '-' character is fixed, Only the front is changeable, and in addition he must be placed under the same package as Pointaction:
Point=com.test.converter.pointconverterpoint2=com.test.converter.pointconverter
Then configure 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=" struts2 "extends=" Struts-default "> <action name= "HelloWorld" class= "Com.test.action.HelloWorld" ><result name= "Success" >/helloworld.jsp </result></action><action name= "Login" class= "com.test.action.LoginAction" ><result name= " Success ">/result.jsp</result></action><action name=" converteraction "class=" Com.test.action.PointAction "><result name=" Success ">/output.jsp</result></action></ Package></struts>
The
Writes a output.jsp, the output has several choices: JSP script; el;struts2 's tag library. Here we use the latter:
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><%@ taglib uri= "/struts-tags" prefix= "s"% ><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Here is one of the results of the operation:
Secondly , we use the Strutstypeconverter provided by Struts2 (which also inherits the Defaulttypeconverter class under the OGNL package, how to view its source code in MyEclipse? MyEclipse find Struts2-core-2.1.6.jar, expand, find Org.apach.struts2.util, find its struts typeconverter.class, open, click Attach Source->external Folder, Find struts-2.1.6 Extract directory, find the directory of SRC, under SRC to expand the core, expand Main,main Below a Java, choose Java, OK, so that the hard disk on the source and myeclipse associated with the abstract class:
Create a new class PointConverter2 under the Com.test.converter package:
Package Com.test.converter;import Java.util.map;import Org.apache.struts2.util.strutstypeconverter;import Com.test.bean.point;public class PointConverter2 extends strutstypeconverter{@Overridepublic Object ConvertFromString (Map context, string[] values, Class toclass) {Point point = new Point (); String value = Values[0]; String[] result = Value.split (",");p Oint.setx (Integer.parseint (result[0]));p oint.sety (Integer.parseint (result[1)) ); return point;} @Overridepublic String converttostring (Map context, Object o) {point point = (point) o;int x =point.getx (); int y = Point.get Y (); String result = "x:" + x + "y:" + y;return result;}}
Modify Pointaction-conversion.properties:
Point=com.test.converter.pointconverter2point2=com.test.converter.pointconverter2
The browser accesses the http://localhost:8080/struts2/input.jsp and runs the same results as it did in the first way.
One problem : How to do batch processing (if there are 100 points, do you want to define from Point1 to point100)?
We assume that there are three coordinates for "many coordinates", and in input.jsp, notice that the Name property of these points is the same:
<form action= "Converteraction.action" method= "POST" > coordinates: <input type= "text" name= "point" size= ">" <br> coordinates 2:<input type= "text" name= "point" size= "x" ><br> coordinates 3:<input type= "text" Name= " Point "size=" ><br> user name: <input type= "text" name= "username" size= "><br> Age:<" Input type= "text" name= "Age" size= "/><br/> Birth date: <input type=" text "name=" Birthday "size="/> " <br/> <input type= "Submit" value= "Submit"/> " </form>
To modify the Pointaction class:
Package Com.test.action;import Java.util.date;import Java.util.list;import com.opensymphony.xwork2.ActionSupport; Import Com.test.bean.point;public class Pointaction extends Actionsupport{/*private point Point;private point point2;*/ Private list<point> point;private String username;private int age;private Date birthday;public list<point> GetPoint () {return point;} public void SetPoint (list<point> point) {this.point = point;} /*public Point GetPoint2 () {return point2;} public void SetPoint2 (point point2) {this.point2 = Point2;} Public Point GetPoint () {return point;} public void SetPoint (point point) {this.point = point;} */public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date brithday) {this.birthday = Brithday;} Public String Execute () throws Exception{return "Success";}} Then write the transformation class PointConverter3:
Package Com.test.converter;import Java.util.arraylist;import Java.util.list;import java.util.map;import Org.apache.struts2.util.strutstypeconverter;import Com.test.bean.point;public class PointConverter3 extends strutstypeconverter{@Overridepublic Object convertfromstring (Map context, string[] values, Class toclass) {list< point> list = new arraylist<point> (); for (String value:values) {string[] result = Value.split (","); Point point = new Point ();p Oint.setx (Integer.parseint (result[0));p oint.sety (Integer.parseint (result[1])); List.add (point);} return list;} @Override @suppresswarnings ("unchecked") public String converttostring (Map context, Object o) {list<point> List = ( list<point>) O; StringBuffer sb = new StringBuffer (), int number = 0;for (point point:list) {Number++;int x = Point.getx (); int y = point.ge TY ();//Do not use the kind of direct stitching in the previous pointconverter, the string is too much, the efficiency is too low sb.append (number). Append (". x="). Append (x). Append ("y="). Append (y). Append (""); return sb.tostring ();}}To change the configuration file pointaction-conversion.properties:
#point =com.test.converter.pointconverter2#point2=com.test.converter.pointconverter2point= Com.test.converter.PointConverter3
Last change output.jsp:
<body> Coordinates: <s:property value= "point"/><br/> user name: <s:property value= "username"/> <br> Age: <s:property value= "ages"/><br> date of birth: <s:property value= "Birthday"/> </body>
Here is one of the results of the operation:
In addition, a type conversion method is used (requires the user to enter coordinates in two input boxes respectively):
Modify input.jsp:
<body>
Pointaction class content is as follows:
Package Com.test.action;import Java.util.date;import Java.util.list;import com.opensymphony.xwork2.ActionSupport; Import Com.test.bean.point;public class Pointaction extends actionsupport{private point point;private String username; private int age;private Date birthday;public point GetPoint () {return point;} public void SetPoint (point point) {this.point = point;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date brithday) {this.birthday = Brithday;} Public String Execute () throws exception{ //Two line test statement, output to console System.out.println ("X:" + point.getx ()); System.out.println ("Y:" + point.gety ()); return "Success";}}
Change the point class to override its ToString method:
Package Com.test.bean;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;} @Overridepublic string toString () {String result = "x:" + x + "y:" + y;return result;}}
Comment out Pointaction-conversion.properties:
#point =com.test.converter.pointconverter2#point2=com.test.converter.pointconverter2#point= Com.test.converter.PointConverter3
Output.jsp as follows:
<body> Coordinates: <s:property value= "point"/><br/> user name: <s:property value= "username"/> <br> Age: <s:property value= "ages"/><br> date of birth: <s:property value= "Birthday"/> </body>
Browser Run Results:
Coordinates: x:1 y:2
User name: Name
Age: 20
Date of birth: 93-3-30
In addition, there is a small knowledge point: if we have multiple actions, such as add and delete to change, then the class is a bit more, we can use the same action to deal with multiple business requirements, the following changes pointaction (added a test method, Struts2 requires that this method, in addition to the method name and execute, must be identical for other signatures, including the public modifier, throwing an exception, and so on:
Package Com.test.action;import Java.util.date;import Java.util.list;import com.opensymphony.xwork2.ActionSupport; Import Com.test.bean.point;public class Pointaction extends actionsupport{private point point;private String username; private int age;private Date birthday;public point GetPoint () {return point;} public void SetPoint (point point) {this.point = point;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date brithday) {this.birthday = Brithday;} Public String test () throws Exception{system.out.println ("Test invoked"); System.out.println ("X:" + point.getx ()); System.out.println ("Y:" + point.gety ()); return SUCCESS; Read the source code of the action, you know it defines a constant called success, its value is "success", so here the effect and return "success" the same}public String execute () throws Exception{system.out.println ("X:" + point.getx ()); System.out. println ("Y:" + point.gety ()); return "Success";}} So how do we get our program to execute the test method without executing the default execute? The method is to modify the corresponding Struts.xml file:
<action name= "converteraction" class= "com.test.action.PointAction" method= "test" ><result name= "Success" >/output.jsp</result></action>
You can see that on the basis of the original we added a method property and assigned it to test, so that the browser accesses the http://localhost:8080/struts2/input.jsp, the output is still back to the console to see the test Invoked, it means that our practice has been successful.
Add: Note that the above program I gave the return success statement added comments, referred to the action source code, the source code is not in the Struts2, need to download separately according to the version, For example, we here is xwork-2.1.2, download xwork source code, find the corresponding directory, such as I downloaded the completion, the decompression was placed in the struts2 below the D:\ProgramFiles\struts-2.1.6\xwork-2.1.2\src\ Java. MyEclipse can be connected.
How do we know about this kind of thing?
Because our pointaction inherited the Actionsupport, Actionsupport implemented the action.
Add: If the point we want to convert has other action classes in addition to pointaction that need to be converted to the same point, do we have to write the same configuration file under its corresponding package for each action? The answer is no, struts takes this into account, giving us a global conversion function in SRC (not defined in a package, Otherwise the global conversion is invalidated) the following new file is named Xwork-conversion.properties, noting that each character here is fixed. The contents are as follows:
#要转换的对象的类的全名 = Right is the name of the converter Com.test.bean.point=com.test.converter.pointconverter2
Change the coordinate portion of the input.jsp to: coordinates: <input type= "text" name= "point" size= "/><br/>"
Comment out pointaction-conversion.properties, browser access, output normal, indicating that our configuration is correct.
The 11th part of _struts2.1 type conversion fine analysis