码迷,mamicode.com
首页 > 其他好文 > 详细

struts2_struts类型转换

时间:2018-11-24 14:27:26      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:exce   index   strong   rom   system   info   version   oid   2.3   

一.在Servlet中,表单提交的非字符串类型需要手动转换

  技术分享图片

  1.在struts中,表单提供的常见数据类型struts框架自动转换,无需手动转换

  2.在某些情况下,某些自定义类型struts不能完成自动转换,需要进行手动转换,如果需要转换的类型转换频率较高时,手动转换的代码增多,这时可以使用struts的类型转换器来进行转换

 

二.类型转换

  

使用类型转换的步骤

  1. 编写类型转换器,继承StrutsTypeConverter
  2. 编写xwork-conversion.properties配置文件,内容为:要转换的类型=类型转换器

 案例:

  编写实体类: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 + ")";
    }
    
}
Point

 

  编写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;
    }
}
PointAction

 

  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>
struts.xml

 

  

  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>
web.xml

 

struts2_struts类型转换

标签:exce   index   strong   rom   system   info   version   oid   2.3   

原文地址:https://www.cnblogs.com/aihuadung/p/10011342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!