Struts2中的参数传递

我们知道,Struts2完成参数传递处理工作的基础是OGNL和ValueStack。而在这个过程中,我也把Struts2所要做的工作大致归纳为两个方面:

1. 对OGNL操作进行封装,完成OGNL表达式所表示的值到Java对象的值传递机制

2. 在参数传递的过程中,做恰当的类型转化,保证页面上的字符串能够转化成各式各样的Java对象

接下来,通过四个不同的角度,来具体讲述Struts2在这两个方面的工作。

目 录 [ - ]

最简单的参数传递

Array、List、Map等容器类型的参数传递

文件上传

自定义的类型转化实现

最简单的参数传递

public class EnumTypeConverter extends DefaultTypeConverter {
   /**
   * Converts the given object to a given type. How this is to be done is implemented in toClass. The OGNL context, o
   * and toClass are given. This method should be able to handle conversion in general without any context or object
   * specified.
   *
   * @param context - OGNL context under which the conversion is being done
   * @param o    - the object to be converted
   * @param toClass - the class that contains the code to convert to enumeration
   * @return Converted value of type declared in toClass or TypeConverter.NoConversionPossible to indicate that the
   *     conversion was not possible.
   */
   public Object convertValue(Map context, Object o, Class toClass) {
     if (o instanceof String[]) {
       return convertFromString(((String[]) o)[0], toClass);
     } else if (o instanceof String) {
       return convertFromString((String) o, toClass);
     }
     return super.convertValue(context, o, toClass);
   }
   /**
   * Converts one or more String values to the specified class.
   * @param value - the String values to be converted, such as those submitted from an HTML form
   * @param toClass - the class to convert to
   * @return the converted object
   */
   public java.lang.Enum convertFromString(String value, Class toClass) {
     return Enum.valueOf(toClass, value);
   }
}

时间: 2024-09-20 05:36:02

Struts2中的参数传递的相关文章

关于struts2中action参数传递的问题

问题描述 我的action返回一个值后会转发到jsp页面,在jsp页面中代码如下:<s:propertyvalue="#parameters.id[0]"/>请问我在action中要怎么用代码设置,能在jsp中接受到,原来在servlet中代码如下:RequestDispatcherrd=request.getRequestDispatcher("/student/createInteract.jsp?id="+id);rd.forward(reques

struts2中int型参数传递

问题描述 struts2中int型参数id如何从页面表单传递到后台action中?后台action中如何接收int类型的参数?页面用<s:propertyid="id"value="#session.operator.operId"/>传递id的值,其中operId是Long类型的.action中用什么可以接收到id的值?其实,此处id应该用隐藏表单域传递的,但是我用了隐藏表单域后,就抛出异常ognl.MethodFailedException:Meth

struts2中页面表示国际化的方法

在struts2中,前端页面表示国际化的实现更加简单.简单的应用struts2提供的支持国际化的表达式 即可快速方便的进行页面的国际化的实现.如何做呢?本文以英文和中文为例进行说明. 1,自定义struts2的属性, 属性项目:struts.custom.i18n.resources 首先生成一个struts.properties文件, 文件位置:WEB-INF/src目录下 文件内容: struts.custom.i18n.resources=globalMessages 2,根据属性所指定的

struts2中action和field级别错误处理

在struts2中,一般的action都继承ActionSupport这个类,可以重写public void validate()来进行数据校验,对应提示信息来说一般有两个比较常用的方法就是this.addFieldError("field name","error message"); 和 this.addActionError("error message");两个方法. 由于在ActionSupport这个类实现了ValidationAwa

struts2中一个表单中提交多个请求的例子

  在很多Web应用中,为了完成不同的工作,一个HTML form标签中可能有两个或多个submit按钮,Struts2中提供了另外一种方法,使得无需要配置可以在同一个action类中执行不同的方法(默认执行的是execute方法) 使用这种方式也需要通过请求参来来指定要执行的动作.请求参数名的格式为 action!method.action 注:由于Struts2只需要参数名,因此,参数值是什么都可以. 下面我就给出一个实例程序来演示如何处理有多个submit的form: 主页面more_su

Struts2中Action接收参数的方法

Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属性,创建get和set方法:     b.接收:通过属性接收参数,如:userName:     c.发送:使用属性名传递参数,如:user1!add?userName=Magci: 2.使用DomainModel接收参数:     a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法:    

标签-struts2中,jsp页面设置theme=&amp;amp;quot;ajax&amp;amp;quot;

问题描述 struts2中,jsp页面设置theme="ajax" <%@ page language=""java"" contentType=""text/html; charset=UTF-8"" pageEncoding=""UTF-8""%><!DOCTYPE html PUBLIC ""-//W3C//DTD HTML

Js显示Struts2中的内容之escape和escapeHtml

  $(function (){        var msg = '<s:property value="msg" escape="false"/>';        if(""!=msg)        {            alert(msg);        }    });   问题:Struts2 中后台属性值带有的HTML标签,前台页面取值时把HTML标签当成字符串输出了,怎么把它们当成HTML标签处理. 举个例子来

struts2中访问指定了方法,但是却把action下所有的方法都执行了一次这是怎么回事啊

问题描述 struts2中访问指定了方法,但是却把action下所有的方法都执行了一次这是怎么回事啊 具体配置如下图 xml配置如下: jsp如下: action如下: 解决方案 你的BaseAction是自定义的吧,还是哪个别的第三方包的,常用的是继承struct自带的MappingDispatchAction,这个是和struct.xml搭配来用,你也可以继承MappingDispatchAction这个类,然后再写你的action模式 解决方案二: 那应该就是你调用的函数getByPage