重温struts2之类型转换
2014-05-09 09:26
288 查看
在视图页面中输入的参数都只能是字符数据,需要将之转换为具体java类型才能被使用。
struts2提供了强大的类型转换功能,它是基于OGNL表达式的,因此我们只要将HTML输入项命名为合法的OGNL表达式就可以使用struts2的类型转换机制。
另外,开发者可以很方便地定义自己的类型转换器,完成字符型到复合类型的转换。
使用OGNL表达式实现类型转换
以一个例子来说吧。
action:
action配置:
result.jsp:
另外,UserBean:
内置的转换器
struts2提供如下内置的转换器:
boolean和Boolean
short和Short
char和Character
int和Integer
long和Long
float和Float
double和Double
Date
String类型元素的数组
String类型元素的集合
开发者可以写自己的类型转换器,先不说这个。以下以Date为例说明struts2的类型转换:
action的配置:
在login.jsp中:
result.jsp:
在login.jsp页面中输入yyyy-mm-dd格式的日期即可自动被转换了。
转换异常
如果转换过程中出现异常,那么会被conversionError拦截器处理,它负责将转换异常封装成fieldError,然后转入input视图。
struts2提供了强大的类型转换功能,它是基于OGNL表达式的,因此我们只要将HTML输入项命名为合法的OGNL表达式就可以使用struts2的类型转换机制。
另外,开发者可以很方便地定义自己的类型转换器,完成字符型到复合类型的转换。
使用OGNL表达式实现类型转换
以一个例子来说吧。
<%@page language="java" contentType="text/html; charset=gbk"%> <!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>登录页面</title> </head> <body> <center> <form action="login.action" method="post"> <table> <tr> <td> <p>用户:</p> </td> <td> <input type="text" name="user.name" /> </td> </tr> <tr> <td> <p>密码:</p> </td> <td> <input type="text" name="user.password" /> </td> </tr> <tr> <td> <p>生日:</p> </td> <td> <input type="text" name="birth" /> </td> </tr> <tr> <td> <input type="submit" value="登录" /> </td> <td> <input type="reset" value="重置" /> </td> </tr> </table> </form> </center> </body> </html>
action:
public class LoginAction{ private UserBean user; private Date birth; public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public UserBean getUser() { return user; } public void setUser(UserBean user) { this.user = user; } public String execute(){ if(getUser().getName().equals("a")&&getUser().getPassword().equals("a")){ return "success"; } return "error"; } }
action配置:
<action name="login" class="ch5.conversion.LoginAction"> <result name="success">/conversion/result.jsp</result> <result name="error">/conversion/error.jsp</result> </action>
result.jsp:
<%@page contentType="text/html; charset=gbk" %> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> </head> <body> <s:property value="user.name"/> <s:property value="user.password"/> <s:property value="birth"/> <p>转换成功</p> </body> </html>
另外,UserBean:
public class UserBean { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
内置的转换器
struts2提供如下内置的转换器:
boolean和Boolean
short和Short
char和Character
int和Integer
long和Long
float和Float
double和Double
Date
String类型元素的数组
String类型元素的集合
开发者可以写自己的类型转换器,先不说这个。以下以Date为例说明struts2的类型转换:
public class LoginAction { private Date birth; public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public String execute(){ if(birth != null){ return "success"; } return "error"; } }
action的配置:
<action name="login" class="ch5.conversion.LoginAction"> <result name="success">/conversion/result.jsp</result> <result name="error">/conversion/error.jsp</result> </action>
在login.jsp中:
<%@page language="java" contentType="text/html; charset=gbk"%> <!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>登录页面</title> </head> <body> <form action="conversionlogin.action" method="post"> <input type="text" name="birth" /> <input type="submit" value="登录" /> <input type="reset" value="重置" /> </form> </body> </html>
result.jsp:
<%@page contentType="text/html; charset=UTF-8" %> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> </head> <body> <s:property value="birth"/> <p>转换成功</p> </body> </html>
在login.jsp页面中输入yyyy-mm-dd格式的日期即可自动被转换了。
转换异常
如果转换过程中出现异常,那么会被conversionError拦截器处理,它负责将转换异常封装成fieldError,然后转入input视图。
相关文章推荐
- Struts2:类型转换之批量封装Set集合类型的属性
- Struts2-学习笔记系列(9)-OGNL类型转换和类型绑定
- Struts2之简单数据类型转换
- Struts2的类型转换
- struts2里的类型转换发生了错误,错误信息是放在field级别里,而不是放在action级别里
- struts2中的类型转换,从web页面传来的离散的值在后台封装成一个user对象
- struts2 之 类型转换
- struts2在页面输出类型转换错误
- Struts2(十四):类型转换
- Struts2类型转换----常规类型,自定义类型,错误处理
- 4.1.4: Struts2的类型转换---自定义类型转换器
- Struts2中的类型转换器
- Struts2类型转换
- Struts2中转换Date类型的问题 Ljava.lang.String
- struts2类型转换
- 2.Struts2的类型转换
- struts2 类型转换器 Date型数据转换
- struts2_自定义类型转换失败的提示信息
- Struts2数据类型转换之批量数据转换
- Struts2类型转换错误消息的显示