struts2_struts类型转换

Posted 爱华顿g

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2_struts类型转换相关的知识,希望对你有一定的参考价值。

一.在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类型转换的主要内容,如果未能解决你的问题,请参考以下文章

sql 日期转换代码片段 - Dato,120,konvertere

Android:片段无法转换为上下文[重复]

HTML Bookmarklet模板:将任何JavaScript代码片段转换为Bookmarklet

对象不能从 DBNull 转换为其他类型。

结合两个代码片段?将用户输入的 Youtube url 转换为嵌入 url,然后将 iframe src 替换为转换后的 url

片段无法转换为上下文