`
robin35java
  • 浏览: 20619 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

转换器(Converter)——Struts 2.0中的魔术师

阅读更多
http://www.35java.com/zhibo/forum.php?mod=viewthread&tid=376&extra=page%3D3
开发Web应用程序与开发传统桌面应用程序不同,Web应用程序实际上是分布个不同的主机(当然也可以同一个主机,不过比较少见)上的两个进程之间互交。这种互交建立在HTTP之上,它们互相传递是都是字符串。换句话说,        服务器可以的接收到的来自用户的数据只能是字符串或字符数组,而在服务器上的对象中,这些数据往往有多种不同的类型,如日期(Date),整数(int),浮点数(float)或自定义类型(UDT)等,如图1所示。因此,我们需要服务器端将字符串转换为适合的类型。
                                                                               
                                图1 UI与服务器对象关系               
                        同样的问题也发生在使用UI展示服务器数据的情况。HTML的Form控件不同于桌面应用程序可以表示对象,其值只能为字符串类型,所以我们需要通过某种方式将特定对象转换成字符串。
                        要实现上述转换,Struts 2.0中有位魔术师可以帮到你——Converter。有了它,你不用一遍又一遍的重复编写诸如此类代码:
                        Date birthday = DateFormat.getInstance(DateFormat.SHORT).parse(strDate);
        <input type="text" value="<%= DateFormat.getInstance(DateFormat.SHORT).format(birthday)        %>" />
                        好了,现在让我们来看一个例子。
                        转换器——Hello World                        在我的上一篇文章《在Struts            2.0中国际化(i18n)您的应用程序》的最后我举了一个可以让用户方便地切换语言的例子,下面例子与其相似,但实现方法不同。
                        首先,如《在Struts            2.0中国际化(i18n)您的应用程序》的第一个例子一样,创建和配置默认的资源文件;
                        接着,新建源代码文件夹下的tutorial包创建HelloWorld.java文件,代码如下:

                                                                                package                                tutorial;

                                import                                java.util.Locale;

                                import                                com.opensymphony.xwork2.ActionSupport;
                                import                                com.opensymphony.xwork2.util.LocalizedTextUtil;

                                public                               
                                class                                HelloWorld                                                                                extends                                ActionSupport                                                                                                                                                                                                                                                {
                                                                                                           private                                                                                                            String msg;
                                                                                                               private                                                                                                                Locale loc                                                 =                                                                                                                    Locale.US;
                                                                     
                                                                                                                       public                                                                                                                        String getMsg()                                                                                                                                                                                                                                                                                 {
                                                                                                                                                       return                                                                msg;       
                                                                                       }                                                                                                                                                               
                                                                                                                                                          
                                                                                                                                           public                                                                                                                                            Locale getLoc()                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                           return                                                                loc;
                                                                                                           }                                                                                                                                                               
                                                                                                                                                                              
                                                                                                                                                               public                                               
                                                void                                                setLoc(Locale                                                                                                                loc)                                                                                                                                                                                                                                                                                 {
                                                                                                                                                                                               this                                                                .loc                                                                                                                                                                                        =                                                                loc;
                                                                                                                               }                                                                                                                                                               
                                                                                                                                                                                                  
                                                                                                                                   @Override
                                                                                                                                                                                   public                                                                                                                                                                                    String execute()                                                                                                                                                                                                                                                                                 {
                                                                                                                                       //                                                                                                                                    LocalizedTextUtil是Struts 2.0中国际化的工具类,<s:text>标志就是通过调用它实现国际化的                                                                                                                                               
                                                                                                                                                                                                                        msg                                                                                                                                                 =                                                                LocalizedTextUtil.findDefaultText(                                                                "                                                                HelloWorld                                                                "                                                                , loc);
                                                                                                                                                                   return                                                                SUCCESS;
                                                                                                   }                                                                                                                                                               
                                                                }                                               
                        然后,在源代码文件夹下的struts.xml加入如下代码新建Action:

                                                <                                package                                 name                                ="ConverterDemo"                                extends                                ="struts-default"                                >                               
                                <                                action                                                    name                                ="HelloWorld"                                class                                ="tutorial.HelloWorld"                                >                               
                                <                                result                                >                                /HelloWorld.jsp                                </                                result                                >                               
                                </                                action                                >                               
                                </                                package                                >               
                        再在Web文件夹下,新建 HelloWorld.jsp,代码如下:

                                                <                                %@ page                                  contentType                                ="text/html; charset=UTF-8"                                %                                >                               
                                <                                %@taglib                                                prefix                                ="s"                                uri                                ="/struts-tags"                                %                                >                               
                                <                                html                                >                               
                                <                                head                                >                               
                                <                                title                                >                                Hello World                                </                                title                                >                               
                                </                                head                                >                               
                                <                                body                                >                               
                                <                                s:form                                                                                            action                                ="HelloWorld"                                theme                                ="simple"                                >                                                                                                                  
                                                                                Locale:                                 <                                s:textfield                                 name                                ="loc"                               
                                />                                &nbsp;                                <                                s:submit                                 />                               
                                </                                s:form                                >                               
                                <                                h2                                ><                                s:property                                 value                                ="msg"                               
                                /></                                h2                                >                               
                                </                                body                                >                               
                                </                                html                                >               
                        接下来,在源代码文件夹的tutorial包中新建LocaleConverter.java文件,代码如下:

                                                                                package                                tutorial;

                                import                                java.util.Locale;
                                import                                java.util.Map;

                                public                               
                                class                                LocaleConverter                                                                        extends                                ognl.DefaultTypeConverter                                                                                                                                                                                                                                        {
                                                   @Override
                                                                                                   public                                                                                                    Object convertValue(Map context, Object value, Class toType)                                                                                                                                                                                                                                                                                 {
                                                                                                                                   if                                                                (toType                                                                                                                            ==                                                                Locale.                                                                class                                                                )                                                                                                                                                                                                                                                                                                                                                                 {
                                                                                   String locale                                                                                                                                                             =                                                                                ((String[]) value)[                                                                                0                                                                                ];
                                                                                                                                                                           return                                                                               
                                                                                new                                                                                                                                                                        Locale(locale.substring(                                                                                0                                                                                ,                                                                                                                                                                        2                                                                                ), locale.substring(                                                                                3                                                                                ));
                                                                                                       }                                                                                                                               
                                                                else                                                               
                                                                if                                                                (toType                                                                 ==                                                                String.                                                                class                                                                )                                                                                                                                                                                                                                                                                                                                                                 {
                                                                                   Locale locale                                                                                                                                                             =                                                                                (Locale) value;
                                                                                                                                                                           return                                                                                locale.toString();
                                                                                           }                                                                                                                                                                                                               
                                                                                                                                                                                                                                               return                                                               
                                                                null                                                                ;
                                                                                                   }                                                                                                                                                               
                                                                }                                               
                            再接下来,在源代码文件夹下新建xwork-conversion.properties,并在其中添加如下代码:

                                                java.util.Locale                                =                                tutorial.LocaleConverter               
                        发布运行应用程序,在浏览器中键入http://localhost:8080/Struts2_Converter/HelloWorld.action,输出页面如图2所示:

图2 HelloWorld英文输出
                        在Locale输入框中输入“zh_CN”,按“Submit”提交,出现如图3所示页面:

图3 HelloWorld中文输出
                        上述例子中,Locale文本输入框对应是Action中的类型为java.util.Locale的属性loc,所以需要创建一个自定义转变器实现两者间的转换。所有的Struts        2.0中的转换器都必须实现ognl.TypeConverter接口。        为了简单起见,OGNL包也为你提供了ognl.DefaultTypeConverter类去帮助您实现转换器。在例子中,LocaleConverter继承了ognl.DefaultTypeConverter,重载了其方法原型为“public        Object convertValue(Map context, Object value, Class toType)”的方法。下面简单地介绍一下函数的参数:   
               
分享到:
评论

相关推荐

    struts2.0中文教程

    05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...

    Struts 2.0系列(MAX)

    转换器(Converter)——Struts 2.0中的魔术师 在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD ...

    Struts2.0中文教程权威版

    05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...

    个人认为目前最完备的Struts2教程

    05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...

    struts2.0很全面的中文教程

    1.为Struts 2.0做好准备 2.常用的Struts 2.0的标志(Tag)介绍 3.Struts 2.0的Action讲解 ...5.转换器(Converter)——Struts 2.0中的魔术师 在6.Struts 2.0中实现表单数据校验(Validation) 7....... 8.......

    struts2中文学习资料

    "转换器(Converter)——Struts 2_0中的魔术师" 可能解释了Converter的角色,它负责将HTTP请求参数转换为Action类中的属性,或者将Action的属性转换为适合视图展示的格式。 5. **标签(Tag)使用**: "常用的...

    Struts2例子6

    转换器是Struts2框架中的“魔术师”,因为它可以魔术般地将用户界面提交的不同格式的数据转换为Action类期望的类型。默认情况下,Struts2提供了一些基本类型的转换器,如字符串到整数、浮点数等。但当遇到自定义类型...

    struts2入门的学习资料.pdf

    - 转换器是Struts2中的魔术师,它允许开发者自定义类型转换,将HTTP请求中的参数转换为Action类属性的合适类型。当模型对象的属性类型与请求参数不匹配时,Converter发挥作用,保证数据绑定的正确性。 3. **CRUD...

Global site tag (gtag.js) - Google Analytics