`
esffor
  • 浏览: 1353441 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2内建类型转换器数据器使用简介

阅读更多

struts2 内建了类型转换器,可以方便的将我们在页面上输入的字符串数据转换成JavaBean中的Boolean,Float,Integer,Double,Long等数据类型,在无法转换成功时,还可以给出错误提示,非常方便

首先简历JavaBean:

 

package HelloWorld;

public class User ...{
    
private String username;
      
private Integer password;

      
private String[] books;
      
public String[] getBooks() ...{
            
return books;
        }

        
public void setBooks(String[] books) ...{
            
this.books = books;
        }

        
public String getUsername() ...{
            
return username;
        }

        
public void setUsername(String username) ...{
            
this.username = username;
        }

        
public Integer getPassword() ...{
            
return password;
        }

        
public void setPassword(Integer password) ...{
            
this.password = password;
        }


    
}

 

编写Action,需要继承ActionSupport,在action中有两个变量,一个是User类,一个是birth属性

我们定义birth属性和user类中的password属性都必须是Integer类型

 

package HelloWorld;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport...{
 

private String tip;
private User user;
private Integer birth;


public Integer getBirth() ...{
    
return birth;
}


public void setBirth(Integer birth) ...{
    
this.birth = birth;
}


public String execute() throws Exception...{
    System.out.println(
this.getUser().getUsername());
    System.out.println(
this.getUser().getPassword());
          
return SUCCESS;
    
  }


public User getUser() ...{
    
return user;
}


public void setUser(User user) ...{
    
this.user = user;
}


public String getTip() ...{
    
return tip;
}

public void setTip(String tip) ...{
    
this.tip = tip;
}



  
}

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns
="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
<filter>
      
<filter-name>struts2</filter-name>
      
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>
    
<filter-mapping>
      
<filter-name>struts2</filter-name>
      
<url-pattern>/*</url-pattern>
    
</filter-mapping>


</web-app>

 

struts.xml (WEB-INF/classes下)

 

<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>
    
<package name="struts" extends="struts-default">
       
<action name="Login" class="HelloWorld.LoginAction">
         
<result name="input">/helloworld/index.jsp</result>
         
<result name="success">/helloworld/welcome.jsp</result>
         
         
<result name="error">/helloworld/error.jsp</result>
       
</action>
    
</package>
</struts>

 

struts.properties(WEB-INF/classes下)

struts.custom.i18n.resources=messageResource

 

信息录入页面:

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title></title>
  </head>
  <body>     
   <div style="color:red">
    <s:fielderror />
</div>
     <s:form action="Login">
       <s:textfield name="user.username" key="username"></s:textfield>

       <s:textfield name="user.password" key="password"></s:textfield>
 <s:textfield name="birth" key="password"></s:textfield>
       <s:submit value="login"></s:submit>
    </s:form>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  </body>
</html>

信息展示页面:

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ page isELIgnored="false" %>
   
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title></title>
  </head>
  <body>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
   <body>
        转换成功!<br>
  用户1的用户名为:<s:property value="user.username"/><br>
  用户1的密码为:<s:property value="user.password"/><br>
  
    </body>
</html>

 

 

我们在资源文件中加入:

xwork.default.invalid.fieldvalue={0}类型转换错误,运行index.jsp.,在三个输入框中都输入admin,则会出现以下提示:


user.password字段类型转换失败
birth字段类型转换失败

 

在资源文件中定义的是全局转换错误提示的内容,如果想对具体Action中的字段进行提示,则编写ActionName.properties,本文中对应的就是LoginAction.properties,放在和Action同一个目录下,内容格式如下:invalid.fieldvalue.birth (其中birth为action中属性名)

本文LoginAction.properties内容为:invalid.fieldvalue.birth=生日必须为数字

再次运行index.jsp.结果如下:

user.password字段类型转换失败
生日必须位数字

分享到:
评论
1 楼 Ethip 2008-10-21  
 
我在做全局转换的时候,出现了问题。
我的实例中,因为要转换多个相同类型的属性,用到泛型。
如果你有空的话,麻烦帮我看看哦 ^_^
http://ethip.iteye.com/blog/254754

相关推荐

    Struts2 in action中文版

    5.3 内建的类型转换器 89 5.3.1 立即可用的类型转换器 89 5.3.2 使用OGNL表达式从表单字段名映射到属性 90 5.4 自定义类型转换 101 5.4.1 实现类型转换器 102 5.4.2 在String和Circle之间转换 102 5.4.3 配置框架...

    深入浅出Struts2(附源码)

    作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    如数据类型转换、文件上传和下载、Struts2应用的安全性、调试与性能分析、FreeMarker、Velocily、Ajax,等等。跟随作者一道深入Struts2。聆听大量来之不易的经验之谈。你对Struts2开发框架的理解和应用水平都将更上...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    3.3.2 JSF内建转换器 144 3.3.3 使用转换器 145 3.3.4 转换失败后的错误消息 149 3.4 自定义转换器 154 3.4.1 实现转换器类 154 3.4.2 注册转换器 156 3.4.3 使用自定义转换器 159 3.4.4 绑定到Bean属性的转换器 159...

    spring chm文档

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 ...

    Spring中文帮助文档

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...

    Spring API

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...

    Spring-Reference_zh_CN(Spring中文参考手册)

    5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ...

    Spring 2.0 开发参考手册

    19.3.1. 使用消息转换器 19.3.2. SessionCallback 和ProducerCallback 19.4. 接收消息 19.4.1. 同步接收 19.4.2. 异步接收 - 消息驱动的POJOs 19.4.3. SessionAwareMessageListener 接口 19.4.4. ...

    freemarker总结

    使用内建的int函数可对数值取整,如: ${ (x/2)?int } ${ 1.1?int } ${ 1.999?int } ${ -1.1?int } ${ -1.999?int } 结果是:2 1 1 -1 -1 1.7 比较运算符 表达式中支持的比较运算符有如下几个: 1. =或者==:...

    开源框架 Spring Gossip

    Controller 的类别 介绍如何在 Controller上搭配使用验证器(Validator)、如何实作Command资料的型态转换,以及如何使用Spring的相关API来实作档案上传的功能。 实作 Validator 使用 PropertyEditor ...

Global site tag (gtag.js) - Google Analytics