目录
目录
1. struts2的自动填充
当jsp和Action类中对象名称一致时候,拦截器会自动拦截填充。
拦截器:
Demo:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/user_register.action" method="post">
用户名:<input type="text" name="name"><br/>
年 龄:<input type="text" name="age"><br/>
生 日:<input type="text" name="birth"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
package per.liyue.struts2_datafomat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/*
* 注意:一定要提供Setter方法
*/
private String name;
private int age;
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String register(){
System.out.println(name);
System.out.println(age);
System.out.println(birth);
return "register";
}
}
<?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="data_conversion" extends="struts-default" >
<!-- 使用通配符实现 -->
<action name="user_*" class="per.liyue.struts2_datafomat.UserAction" method="{1}">
<result name="{1}">/user.jsp</result>
</action>
</package>
</struts>
<?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>
<!-- 在总配置文件中引入其他配置文件 -->
<include file="per/liyue/code/struts2demo/config_HelloStruts2.xml"></include>
<include file="per/liyue/code/struts2_data/data.xml"></include>
<include file="per/liyue/struts2_datafomat/data_conversion.xml"></include>
</struts>
在浏览器地址栏输入:
http://localhost.:8080/StrutsDemo1/user_register.action
填写后提交,得到输入内容
2. struts2的对象填充
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/user_register.action" method="post">
用户名:<input type="text" name="user.name"><br/>
年 龄:<input type="text" name="user.age"><br/>
生 日:<input type="text" name="user.birth"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
package per.liyue.struts2_datafomat;
import java.util.Date;
public class User {
/*
* 注意:一定要提供Setter方法
*/
private String name;
private int age;
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
package per.liyue.struts2_datafomat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/*
* 注意:提供setter和getter方法
*/
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String register(){
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getBirth());
return "register";
}
}
其他保持不变,浏览器地址栏输入:
http://localhost.:8080/StrutsDemo1/user_register.action
填写后提交,得到输入内容.
3. struts2的类型转换器
对于一些没有按照指定格式输入的内容,那么可以用自定义的类型转换器来处理:
3.1 类继承关系
---TypeConverter
------DefaultTypeConverter
---------StrutsTypeConverter
3.2 局部转换器
步骤:
-
编写转换器类:继承转换类
-
在包下建立属性文件,文件名称严格按照格式
- Action名称-conversion.properties
- 然后配置:需要转换的字段名=自定义转换器类的权限名称
例如birthday=cn.itcast.convertor.DateTypeConvertor
3.3 全局转换器
步骤:
- 继承转换类,实现抽象方法
- 在包下建立属性文件,严格按照格式
- xwork-conversion.properties
3.4 注意
二者可以同时共用,但是同时出现时候优先局部转换器