struts2-OGNL和类型转换_无极天宗_新浪博客_无极

电脑杂谈  发布时间:2019-05-03 23:15:10  来源:网络整理

struts2 ognl 正则表达式_struts2 ognl_struts2 ognl 表达式

struts2 ognl 正则表达式struts2 ognl 正则表达式

t;

<input name=”user.birthday” />

类里:

private String username;

private Double age;

private Date birthday

框架会自动帮我们转换.我们能成功获得到.

2. 数组;

<input name=”ages” />

<input name=”ages” />

<input name=”ages” />

上面这些指向ages属性:Double[] ages

<input name=”names[0]” />

<input name=”names[1]” />

<input name=”names[2]” />

上面指向:String[] names

但要注意的是:当我们提交上面的整个表单时,HTTP请求如下:

struts2 ognl 表达式_struts2 ognl 正则表达式_struts2 ognl

ages=12,33,55

names[0]=jack

names[1]=green

names[2]=mary

可以看出差别了.

当框架转移ages参数时,它先找到ages属性,并且知道ages参数是一个有3个字符串的数组.由于动作上的ages属性也是一个数组,这样OGNL创建一个新的数组,值是参数的值,并将它赋给动作.并且它知道动作的属性ages是double类型的.它会自动将参数的值转换成对应的类型.

3. List

用法和数组一模一样.只不过JAVA中是List但注意,这里最好是用泛型来声明.不然它会自动将参数转换成String类型并存入List.泛型用法:

<input name=”names[0].username” />

<input name=”names[1].username” />

接收:List<User> names

第一个User的username为第一个input提交的值…

4. Map

和list不同,Map需要一个键,一个值.如:

<input name=”nameMap.mary” />

<input name=”nameMap.hellen” />

<input name=”nameMap[‘Sunyu’]” />

struts2 ognl_struts2 ognl 表达式_struts2 ognl 正则表达式

上面一种用点语法,一种用方括号.但两种意思一样,当我们提交上面表单后,后台获得的值如下:

Map<String,String> nameMap;值为:

mary --- >第一input提交的值.

hello --- >第二input提交的值.

…..

自定义类型转换上面那些类型框架自带的类型转换器会帮我们完成转换.但有些时候我们会有一些自定义类型需要使用.这时候我们就要自定义类型转换器,告诉框架如何去转换.

所有类型转换器必须实现ognl.TypeConverter接口.因为HTTP请求的内容是String类型的,所以我们的类型转换器都是从String到某类型或从某类型到String.同样,我们自定义的类型转换器也要实现这两个方法.

Struts2提供了比ognl.TypeConverter更方便的方法:StrutsTypeConverter类,它实现了前面的接口.而且定义了两个抽象方法,即我们要实现的方法.看下例:

String 和 Circle间转换 package manning.chapterFive.utils;

import java.util.Map;

import org.apache.struts2.util.servletcontextaware。import org.apache.struts2.util.strutstypeconverter。import org.apache.poi.hssf.util.hssfcolor。

import com.opensymphony.xwork2.util.TypeConversionException;

public class circletypeconverter extends strutstypeconverter{。public class circletypeconverter extends strutstypeconverter {。 private class mytack extends timertask {// public abstract class timertask implements runnable{} 。

public Object convertFromString(Map context, String[] values, ClasstoClass) {

String userString = values[0];

a great circle is section of a circle that contains the circlediameter: the shortest distance between two (non-antipodal) points onthe spherical surface goes along the great circle connecting those twopoints.。·pygame.draw.circle(surface, color, center_point, radius,width) – this function draws a circle. the center of the circle is atthe center_point parameter. the integer passed forthe radius parameter sets the size of the circle.。/*能否在circle位置出发找到路线到达边缘*/private canexitat(circle:circle):boolean{。

return newCircle;

struts2 ognl 正则表达式_struts2 ognl_struts2 ognl 表达式

}

public String convertToString(Map context, Object o) {

Circle circle = (Circle) o;

String userString = 'C:r' + circle.getRadius();

return userString;

}

private Circle parseCircle( String userString ) throwsTypeConversionException {

Circle circle = null;

int radiusindex = userstring.indexof('r') + 1。 int p2 = str.indexof("\r\n")。void change(int x,int l,int r,int l,int r,int d){。

System.out.println('userString = ' + userString);

if (!userString.startsWith( 'C:r') )

throw new typeconversionexception ( 'invalid syntax')。throw new typeconversionexception ( 'invalid integer value forradius')。 throw new soapexception(e.message, new system.xml.xmlqualifiedname("errorcode01"), e)。

int radius;

try {

radius = Integer.parseInt( userString.substring( radiusIndex ));

}catch ( NumberFormatException e ) {

throw new TypeConversionException ( 'Invalid Integer Value forRadius'); }

struts2 ognl 正则表达式_struts2 ognl_struts2 ognl 表达式

circle = new Circle();

circle.setRadius( radius );

return circle;

}

}

有了转换逻辑,我们要让框架知道什么时候,在哪里使用这个转换器.有两种配置方式:

对给定action声明,或全局声明.

1. 专用声明

在要进行转换的类同级目录下新建资源文件:

ActionName-conversion.properties

ActionName就是要在上面应用转换的类名,如:ConvertTest-conversion.properties

里面的内容是:

circle- CircleTypeConverter

前面的circle就是在要应用转换类里那个类型的变量名.后面就是转换类的详细路径,包括它的包路径.

2. 全局声明

和上面不同,全局声明要求在classpath下新建一个:

xwork-conversion.properties文件,里面内容:

包路径.Circle=转换器路径.CircleTypeConverter

等号前是目录类型的详细路径,后面的转换器类的全路径.

但要注意的是:

在实际开发中,最好少利用struts2的类型转换功能,因为框架帮我们自动进行类型转换的消耗是很大的,所以最好是我们在类中都保存字符类型的值,页面只负责显示就行了.比如时间,我们直接转换成2010-xx-xx...等格式的,而不保存为Date类型.


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-96063-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...