Struts2 类型转换

Struts2内置有类型转换的过滤器,在请求参数封装时对页面传递的参数进行转换,但是Struts2内置类型转换器只支持基本类型转换,例如,Integer,String,Double等,如果是其他类型,如java.util.Date,Struts2不支持转换。这时就需要自定义类型转换器。

1 编写JSP表单

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>一点教程-struts2的类型转换</title>
  </head>
  <body>
  <h3>类型转换</h3>
  <form action="demo1.action" method="post">
  	用户名:<input type="text" name="userName"/><br/>
  	密码:<input type="password" name="userPwd"/><br/>
  	邮箱:<input type="text" name="email"/><br/>
  	年龄:<input type="text" name="age"/><br/>
  	成绩:<input type="text" name="result"/><br/>
  	出生年月:<input type="text" name="birth"/><br/>
  	<input type="submit" value="注册"/>
  </form> 
  </body>
</html>

2 编写Action,接收参数

package com.yiidian.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author 一点教程(yiidian.com)
 */
public class Demo1Action extends ActionSupport{
	//struts2可以自动类型转换
	private String userName;
	private String userPwd;
	private String email;
	private Integer age;
	private Double result;
	private Date birth;//Struts2默认不支持日期类型转换,需要自定义类型转换器
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void setResult(Double result) {
		this.result = result;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	@Override
	public String execute() throws Exception {
		System.out.println(userName);
		System.out.println(userPwd);
		System.out.println(email);
		System.out.println(age);
		System.out.println(result);
		System.out.println(birth);
		return SUCCESS;
	}
	
}

3 编写自定义日期类型转换器

package com.yiidian.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
/**
 * 自定义类型转换器
 * @author 一点教程(yiidian.com)
 */
public class DateConvertor extends StrutsTypeConverter{
	//页面->Action
	@Override
	public Object convertFromString(Map arg0, String[] param, Class arg2) {
		//param:代表页面传递的参数
		String dateString = param[0];
		//转换格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	//Action->页面
	@Override
	public String convertToString(Map arg0, Object arg1) {
		return null;
	}

}

4 配置日期转换器

在src目录下建立xwork-conversion.properties,内容如下:

java.util.Data=com.yiidian.converter.DateConvertor

5 struts.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="base" extends="struts-default" namespace="/">
		<action name="demo1" class="com.yiidian.action.Demo1Action">
			<result>/succ.jsp</result>
		</action>
	</package>	
	
</struts>

6 访问测试

http://localhost:8080/ch02_06_struts2_converter/index.jsp

点击表单提交,控制台输出

 

源码下载:https://pan.baidu.com/s/1erH3EK3pxZ6k86HwiLNmPw

热门文章

优秀文章