Struts2 的类型转换,对于源生数据类型以及Date、String等常见类型,Struts2可以使用内建的类型转换器实现自动的转换;但对于自定义的对象类型来说,我们需要提供三个信息,1 action的名字,2待转换的属性的名字,3指定转换器,其中 action的名字是通过属性文件名的前部分得到,待转换的属性的名字是一对键值的前半部分,指定转换器 是键值的后半部分
,示范程序如下:
第一步:新建一个jsp页面 为input.jsp,指定将要处理的类是UserAction.action,主要内容如下
<body>
Struts自定义类型的强制转换 假如输入的是 20072355;123456,怎样转化为一个user的对象<br/>
<form action="userAction.action" method="post">
<input name="inputText" type="test">
<input value="提交" type="submit">
</form>
</body>
第二部 :在struts.xml中为UserAction.action配置一个action
<action name="userAction" class="com.struts2.LoginAction">
<result name="success">/result.jsp</result>
</action>
第三步:编写输出的也页面output.jsp
<body>
username:${requestScope.username }<br/>
password:${requestScope.password }
</body>
第四步:编写一个javabean,就是要被转化成的对象,封装user的用户名密码信息:
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
第五步:编写一个类型转换器,要继承ognl.DefaultTypeConverter
import java.util.Map;
import ognl.DefaultTypeConverter;
public class UserConverter extends DefaultTypeConverter {
/**
* Map context:对于类型转化来说是用不上的
* Object value
* Class toType 转到什么样的类型(从页面到后台还是从后台到页面)
*/
@Override
public Object convertValue(Map context, Object value, Class toType) {
//如果是从页面向后台对象转换
if(User.class == toType){
//获得被转换的值
String[] str = (String[])value;
String firstValue = str[0];
String[] userInfo = firstValue.split(";");
String username = userInfo[0];
String password = userInfo[1];
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;
}
return null;
}
}
第六步:编写UserAction.class 要继承一个父类 ActionSupport
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
第七步:创建一个属性文件 ,指定好对应关系,这个文件的命名规则是 ”action名字“-conversion.properties,这里是UserAction-conversion.properties
user=com.struts2.UserConverter
=号前面是将要转换成的成员变量,后面是转换器的名字
另附别人写的:
就是在表单中输入用户名(userId)和密码(password)提交到Action类,转换到用户类(Users)
stringInput.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>stringInput.jsp</title>
</head>
<body>
<s:form name="stringInput" action="stringInput">
<s:textfield name="user" label="UserId"></s:textfield>
<s:textfield name="user" label="Password"></s:textfield>
<s:submit label="Submit"></s:submit>
</s:form>
</body>
</html>
stringOutput.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'stringOutput.jsp' starting page</title>
</head>
<body>
<s:property value="user"/>
</body>
</html>
Users.java
package com.asen.struts2.bean;
public class Users {
private String userId;
private String password;
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the userId
*/
public String getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}
}
UserConverter.java
package com.asen.struts2.converter;
import java.util.Map;
import ognl.DefaultTypeConverter;
import com.asen.struts2.bean.Users;
public class UserConverter extends DefaultTypeConverter {
/* (non-Javadoc)
* @see ognl.DefaultTypeConverter#convertValue(java.util.Map, java.lang.Object, java.lang.Class)
*/
@Override
public Object convertValue(Map context, Object value, Class toType) {
if(Users.class==toType){
Users user=new Users();
String [] values=(String [])value;
String userId=values[0];
String password=values[1];
user.setPassword(password);
user.setUserId(userId);
return user;
}
if(String.class==toType){
Users user=(Users)value;
String result="User: USERID="+user.getUserId()+" PASSWORD="+user.getPassword();
return result;
}
return null;
}
}
StringInputAction.java
package com.asen.struts2.action;
import com.asen.struts2.bean.Users;
import com.opensymphony.xwork2.ActionSupport;
public class StringInputAction extends ActionSupport {
private Users user;
/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
return SUCCESS;
}
/**
* @return the user
*/
public Users getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(Users user) {
this.user = user;
}
}
StringConverterAction-conversion.properties
user=com.asen.struts2.converter.UserConverter
struts.xml里面的action
<action name="stringInput" class="com.asen.struts2.action.StringInputAction">
<result name="success">/stringOutput.jsp</result>
<result name="input">/stringInput.jsp</result>
</action>
提倡使用struts自带的装换,只需改变转换类如下:
package com.converter;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.bean.User;
public class UserConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
// TODO Auto-generated method stub
String[] str = (String[])arg1;
String firstValue = str[0];
String[] userInfo = firstValue.split("_");
String username = userInfo[0];
String password = userInfo[1];
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;
}
@Override
public String convertToString(Map arg0, Object arg1) {
// TODO Auto-generated method stub
User user = (User)arg1;
String username =user.getUsername();
String password =user.getPassword();
String userInfo = "username= "+username+ " password= "+password;
return userInfo;
}
}
其中原理是一样的,只是做了进一步的封装