Struts2的类型转换

2008-09-11 16:12:33.0     浏览:553     来源:中国IT实验室
关键词:  Struts2  

下面通过一个简单的例子来描述Struts2的类型转换.
准备如下:

1.在web项目中引入Struts2框架:将相应的Jar包(commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.11.1.jar,xwork-2.0.4.jar其它的项目中用不上,所以不需要加入)拷贝到项目的WEB-INF/lib/目录中,然后在web.xml进行如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

//增加如下过滤器
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
2.为了支持国际化所以建立:struts.properties文件和reg_zh_CN.properties文件,其内容分别如下:

//struts.properties
//资源文件reg_zh_CN.properties的前半部份根据下面的"reg"来命名
struts.custom.i18n.resources=reg
//reg_zh_CN.properties
userName=user info:
3.建立User资源类

//类User.java

package cn.edu.hld;

public class User
{
private String userName ;
private String userPass ;
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getUserPass()
{
return userPass;
}
public void setUserPass(String userPass)
{
this.userPass = userPass;
}

}

4.建立实现逻辑控制类

//Action控制类:OgnlTypeConvert.java
package cn.edu.hld;

import com.opensymphony.xwork2.ActionSupport;

public class OgnlTypeConvert extends ActionSupport
{
private User user ;

public User getUser()
{
return user;
}

public void setUser(User user)
{
this.user = user;
}
public String execute()
{
if(this.getUser().getUserName().equals("thtwin") &&
this.getUser().getUserPass().equals("thtwinj2ee") )
{
return SUCCESS ;
}
else
{
return ERROR ;
}
}
}
5.实现类型转换的核心类

//类型转换核心类

package cn.edu.hld;

import java.util.Map;

import ognl.DefaultTypeConverter;

public class TypeConversion extends DefaultTypeConverter
{

@Override
public Object convertValue(Map context, Object values, Class toClass)
{

if(toClass == User.class)
{
String strUser[] = (String[])values ;
String userInfo[] = strUser[0].split(",") ;
User user = new User() ;
user.setUserName(userInfo[0]) ;
user.setUserPass(userInfo[1]) ;
return user ;
}
else if(toClass == String.class)
{
String userInfo = "" ;
User user = (User)values ;
userInfo += "用户名为:" + user.getUserName() ;
userInfo += "输入的密码为:" + user.getUserPass() ;
return userInfo ;
}
else
{
return "类型转换失败!" ;
}

}

}

6.建立OgnlTypeConvert-conversion.properties(前面与Action类名一样,后面是死的都一样)文件并且与OgnlTypeConvert.class放在一起,通知系统怎么进行类型转换,其内容如下:

//说明Action中user这个属性通过cn.edu.hld.TypeConversion这个类来实现类型转换
user=cn.edu.hld.TypeConversion

7.建立相应的index.jsp文件其内容如下:

<%@page contentType="text/html;charset=gb2312"%>
<%@taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试Struts2的类型转换</title>
</head>

<body>
<form action="reg.action" method="post">
<s:text name="userName"></s:text>
<input type="text" name="user"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

8.配置struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

[第1页]   [第2页]   [下一页]