您的位置:首页 > 编程语言 > Java开发

重温struts2之类型转换

2014-05-09 09:26 288 查看
在视图页面中输入的参数都只能是字符数据,需要将之转换为具体java类型才能被使用。

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视图。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: