1.先给一个PointBean,要求对这个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;
- }
- }
2.两种转换器, 第一个侧重于告诉大家转换原理, 第二个侧重具体实践
- package edu.hust.common;
- import java.util.Map;
- import ognl.DefaultTypeConverter;
- import edu.hust.bean.Point;
- public class Converter extends DefaultTypeConverter {
- @SuppressWarnings("unchecked")
- @Override
- public Object convertValue(Map context, Object value, Class toType) {
-
-
- if (Point.class == toType) {
- Point point = new Point();
-
-
-
- String[] strTxt = (String[]) value;
- String[] paramValues = strTxt[0].split(",");
-
-
- int x = Integer.parseInt(paramValues[0]);
- int y = Integer.parseInt(paramValues[1]);
-
- point.setX(x);
- point.setY(y);
-
- return point;
- }
-
- if (String.class == toType) {
- Point point = (Point)value;
- int x = point.getX();
- int y = point.getY();
- String result = x + "," + y;
- return result;
- }
- return null;
- }
- }
- package edu.hust.common;
- import java.util.Map;
- import org.apache.struts2.util.StrutsTypeConverter;
- import edu.hust.bean.Point;
- public class Converter2 extends StrutsTypeConverter {
- @SuppressWarnings("unchecked")
- @Override
- public Object convertFromString(Map context, String[] values, Class toClass) {
- Point point = new Point();
-
- String[] paramValues = values[0].split(",");
-
-
- int x = Integer.parseInt(paramValues[0]);
- int y = Integer.parseInt(paramValues[1]);
-
- point.setX(x);
- point.setY(y);
-
- return point;
- }
- @SuppressWarnings("unchecked")
- @Override
- public String convertToString(Map context, Object o) {
- Point point = (Point)o;
- int x = point.getX();
- int y = point.getY();
- String result = "[x坐标:" + x + "],[y坐标:" + y +"]";
- return result;
- }
- }
3.action:请详细阅读下面这个action的中的注释
- package edu.hust.action;
- import java.util.Date;
- import com.opensymphony.xwork2.ActionSupport;
- import edu.hust.bean.Point;
- public class ConverterAction extends ActionSupport {
-
-
-
- private static final long serialVersionUID = 1L;
-
- private Point point;
- private int age;
- private String username;
- private Date date;
- public Point getPoint() {
- return point;
- }
- public void setPoint(Point point) {
- this.point = point;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Override
- public String execute() throws Exception {
- return SUCCESS;
- }
-
- }
4.类型转换的局部资源配置文件ConverterAction-conversion.properties
- # 局部的 --> 类型转换资源文件
- # 这个资源文件的文件名是有规定的:
- # 前半部分:ConvertorAction --> 必须与对应的Action一致。
- # 后半部分:-conversion.properties --> 固定格式,不能修改。
- # 书写内容:你对ConvertorAction中的哪个属性进行转换
- # 对ConvertorAction类中的point对象属性,用edu.hust.common.Convertor类进行转换
- # 局部convertion.properties和全局convertion.properties同时定义了对某一类型的转换时,局部有效、全局无效。
- point = edu.hust.common.Converter
类型转换的全局资源配置文件xwork-conversion.properties
- # 全局的 --> 类型转换资源文件
- # 这个资源文件的文件名是固定的,不能修改。
- # 对整个的PointBean进行转换,无论哪里用到Point类(即只要调用了PointBean),转换就会执行。
- # 局部convertion.properties和全局convertion.properties同时定义了对某一类型的转换时,局部有效、全局无效。
- edu.hust.bean.Point = edu.hust.common.Converter2
5.最后是struts.xml和jsp页面, 这些和普通struts2无异
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <s:form action="converter" method="post">
- <s:textfield name="point" label="point" />
- <s:textfield name="age" label="age" />
- <s:textfield name="username" label="username" />
- <s:textfield name="date" label="date" />
- <s:submit label="submit" />
- </s:form>
- <!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="converter" class="edu.hust.action.ConverterAction">
- <result>/result.jsp</result>
- </action>
- </package>
- </struts>
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <html>
- <head><title>Login Page</title></head>
- <body bgcolor="#99ffff">
-
-
- <h2>使用s标签显示</h2>
- point: <s:property value="point" /><br>
- age: <s:property value="age" /><br>
- username: <s:property value="username" /><br>
- date: <s:property value="date" /><br><br>
-
- <h2>使用el显示</h2>
- point: ${point}<br>
- age: ${age}<br>
- username: ${username}<br>
- date: ${date}<br>
- </body>
- </html>