欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struts2_struts类型转换

程序员文章站 2022-06-05 16:05:39
一.在Servlet中,表单提交的非字符串类型需要手动转换 1.在struts中,表单提供的常见数据类型struts框架自动转换,无需手动转换 2.在某些情况下,某些自定义类型struts不能完成自动转换,需要进行手动转换,如果需要转换的类型转换频率较高时,手动转换的代码增多,这时可以使用strut ......

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

  struts2_struts类型转换

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

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

 

二.类型转换

  

使用类型转换的步骤

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

 案例:

  编写实体类:point

  struts2_struts类型转换
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

  struts2_struts类型转换
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

  struts2_struts类型转换
<?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

  struts2_struts类型转换
<?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