重温struts2之类型转换

在视图页面中输入的参数都只能是字符数据,需要将之转换为具体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视图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值