<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="type" namespace="/type" extends="struts-default">
<!-- 返回视图 -->
<action name="type" class="star.july.b_type.TypeAction" method="type">
<result name="success">/index.jsp</result>
<!-- 定义错误视图 -->
<result name="input">/add.jsp</result>
</action>
</package>
</struts>
创建实体类:
package star.july.b_type;
import java.util.Date;
public class Student {
//不同的类型,输出时会相应的类型
private String name;
private int age;
private double grade;
private Date birth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", grade=" + grade
+ ", birth=" + birth + "]";
}
}
自定义类型转换器:
package star.july.b_type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
//自定义类型转换器
public class MyDateConverter extends StrutsTypeConverter{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
//页面(String)->Action(自己的类型)
//values:页面传入的字符串
//toClass:当前javabean属性类型,java.util.Date
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
//转换日期类型
//将1994/11/02 转为符合的格式
try {
//如果是Date类型才需要转换
if(toClass==java.util.Date.class){
String str = values[0];
//转换成Date类型
Date date = sdf.parse(str);
return date;
}
return null;
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
//Action->转换为String
@Override
public String convertToString(Map arg0, Object arg1) {
// TODO Auto-generated method stub
return null;
}
}
TypeAction:
package star.july.b_type;
import com.opensymphony.xwork2.ActionSupport;
public class TypeAction extends ActionSupport{
//创建一个对象
Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String type(){
//拦截器自动转换类型
//类型与Student.java实体类中的类型相呼应
//输出结果:Student [name=赵甲第, age=22, grade=150.0, birth=Wed Dec 07 00:00:00 CST 1994]
System.out.println(student);
return SUCCESS;
}
}
定义TypeAction-conversion.properties文件
文件名是由:相关联的类型的类名+“-conversion.properties”
student.birth=gz.itcast.b_type.MyDateConverter
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>类型转换</title>
1.
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 返回input视图传来的错误信息 -->
<s:fielderror></s:fielderror>
<body>
<form action="<c:url value='/type/type'/>" method="post">
姓名:<input type="text" name="student.name"/><br>
年龄:<input type="text" name="student.age"/><br>
成绩:<input type="text" name="student.grade"/><br>
日期:<input type="text" name="student.birth"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
局部引用: 对某个Action起作用
在Action类的同目录下,建立一个文件:叫 Action类名-conversion.properties(TypeAction-cnversion.properties)
student.birth=star.july.b_type.MyDateConverter
|
全局引用:对整个网站的所有Action起作用
在项目的src下面,建立一个文件:
xwork-conversion.properties
java.util.Date=
star.july
.b_type.MyDateConverter
|