struts2类型转换

xiaoxiao2025-12-11  3

请求页面代码:

 

input.jsp

<body> <s:form action="pointConverter"><!-- 此处的Point须要是类的名称 --> <s:textfield name="Point" label="point"></s:textfield> <s:textfield name="age" label="年龄"></s:textfield> <s:textfield name="name" label="姓名"></s:textfield> <s:textfield name="address" label="地址"></s:textfield> <s:submit label="Submit"></s:submit> </s:form> </body>

 

这是一个jsp 的页面代码,<s>是struts2的标签,要使用此标签使在jsp页面上引入:

<%@ taglib prefix="s" uri="/struts-tags" %>

 

struts.xml

<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <!-- Add packages here --> <package name="struts2" extends="struts-default"> <action name="pointConverter" class="com.struts2.test.action.PointAction"> <result name="success">/output.jsp</result> </action> </package> </struts>

 

PointAction.java

public class PointAction extends ActionSupport { private Point point; private Integer age; private String name; private String address; public Pointss getPoint() { return point; } public void setPoint(Point point) { this.point = point; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String execute() throws Exception { return SUCCESS; } }

 

PointAction.java只是一个简单的pojo Action。只是覆盖了execute()这个方法,返回一个字符串常量SUCCESS。表示action的执行是通过的。

 

在与PointAction.java同一目录下新建一个PointAction-conversion.properties文件,这个文件的起名是有规则的,规则是要处理Action的名称加-conversion.properties组成,如果你的Action的名称是UsersAction的话,则名称则是UsersAction-conversion.properties。在这个文件里添加一行,如下:

#对PointAction里的Point属性用PointConverter这个转换类来转换 Point=com.struts2.test.converter.PointConverter

 

再新建PointConverter.java

public class PointConverter extends DefaultTypeConverter { /** * 自定义转换类 */ @Override public Object convertValue(Map context, Object value, Class toType) { System.out.println(toType.getName()); //toType: 转换成什么 //页面到自定义类 if(Pointss.class == toType){ Pointss point = new Pointss(); String[] str = (String[])value; //value:返回的是一个Object[]数组 String[] tempStr = str[0].split(","); //取出第一个后按,号分隔 // System.out.println(tempStr.length); if(tempStr.length == 2){ //填充到Point类 point.setX(Integer.parseInt(tempStr[0])); point.setY(Integer.parseInt(tempStr[1])); } // System.out.println(point.getX()); // System.out.println(point.getY()); return point; } //服务器到页面 if(String.class == toType){ Pointss point = (Pointss)value; //将value值强制转换为Point类 System.out.println("X坐标为:"+point.getX()+" Y坐标为:"+point.getY()); return "X坐标为:"+point.getX()+" Y坐标为:"+point.getY(); } return null; } }

 PointConverter.java是继承了DefaultTypeConverter的,并重写了convertValue这个方法,具体的看以上的代码注释。其中,value是一个提交过来的数组值,toType是你要转换到什么类型。

 

output.jsp

<body> point:<s:property value="Point"/> age:<s:property value="age" /> name:<s:property value="name" /> address:<s:property value="address" /> </body>

 

页面输出结果。也是用到了struts2的标签来完成。

 

 

要知道struts2的类型转换是如何实现的,就要知道其实现的流程,其流程为:

 

1,input.jsp页面提交一个请求pointConverter,通过struts.xml找到了相应的请求处理类,即action。然后就调用该action。

 

2,在运行action时,由于请求时有一个name为Point的。所以就调用action的setPoint方法,在调用这个setPoint方法时,struts2首先会去查找在同一目录下是否存在一个与Action名字相同加"-conversion.properties"这样的文件,正好发现有一个这样的文件,并通过这个文件得知其Point这个属性要调用指定的类(PointConverter)来进行解释操作。这个指定要写上全路径。

 

3,通过PointConverter.java里的toType来判断到底是进行那个操作,是客户端到服务器,还是服务器到客户端。如果if(Point.class == toType)返回是true的话,则说明是由客户端到服务器,将客户端提交过来的value更换为Point。此时提交过来的value是一个字符串数组。通过解释后将提交过来的值填充至Point去。

 

4,由于PointAction里重写了父类的execute这个方法,并返回success,在sturts.xml里得知,如果返回的是success的话就将请求转发到output.jsp页面去。

 

5,在output.jsp页面里,由于输出的字段有一个Point的字段,此时,struts2又回调用PointAction里的getPoint方法,同时,在调用这个getPoint方法的同时,也会像setPoint一样,先去查找有没有叫Point-conversion.properties这个文件。刚好有,并知道是要调用PointConverter来执行操作这个类型转换的。

 

6,同理,通过toType得知,是由服务器到客户端的。在这个if(String.class == toType )方法里,将返回值更改为你要显示到客户端的显示内容。

 

简单的自定义类型转换的流程步骤大致上就是这样的了。

转载请注明原文地址: https://www.6miu.com/read-5040669.html

最新回复(0)