[原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 1 package com.jason.struts2.model;
 2 
 3 import java.util.Date;
 4 
 5 
 6 
 7 /*
 8  * 
 9  * 
10  * */
11 public class Manager {
12 
13     private String name;
14     private Date birth;
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public Date getBirth() {
25         return birth;
26     }
27 
28     public void setBirth(Date birth) {
29         this.birth = birth;
30     }
31 
32     @Override
33     public String toString() {
34         return "Manager [name=" + name + "]";
35     }
36 
37 }
Manager
 1 package com.jason.struts2.model;
 2 
 3 
 4 
 5 /*
 6  * 1.Department 是模型,实际录入的Department, deptName, 可以直接写入;manager 属性的处理?
 7  * Struts2  表单标签的name 值可以被赋为属性的属性:name=manager.name / name=manager.birth
 8  * 
 9  * 3.manager 中有一个Date 类型的birth 属性,Struts2 是否能完成自动的转换?
10  * 
11  * 全局的类型转换器可以正常工作
12  * 
13  * */
14 public class Department {
15     private Integer id;
16     private String deptName;
17 
18     private Manager manager;
19 
20     public Integer getId() {
21         return id;
22     }
23 
24     public void setId(Integer id) {
25         this.id = id;
26     }
27 
28     public String getDeptName() {
29         return deptName;
30     }
31 
32     public void setDeptName(String deptName) {
33         this.deptName = deptName;
34     }
35 
36     public Manager getManager() {
37         return manager;
38     }
39 
40     public void setManager(Manager manager) {
41         this.manager = manager;
42     }
43 
44     @Override
45     public String toString() {
46         return "Department [id=" + id + ", deptName=" + deptName + ", manager="
47                 + manager + "]";
48     }
49 
50 }
Department
 1 package com.jason.struts2.model;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.opensymphony.xwork2.ModelDriven;
 5 
 6 public class TestComplexPropertyAction extends ActionSupport implements ModelDriven<Department>{
 7 
 8     /**
 9      *  @Fields:serialVersionUID
10      */
11     private static final long serialVersionUID = 1L;
12     private Department department;
13     
14     @Override
15     public Department getModel() {
16         department = new Department();
17         return department;
18     }
19     
20     @Override
21     public String execute() throws Exception {
22         System.out.println(department);
23         return "success";
24     }
25     
26     
27 
28 }
TestComplexPropertyAction
 1 package com.jason.strut2.conversion;
 2 
 3 import java.text.DateFormat;
 4 import java.text.ParseException;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Date;
 7 import java.util.Map;
 8 
 9 import javax.servlet.ServletContext;
10 import javax.xml.crypto.Data;
11 
12 import org.apache.struts2.ServletActionContext;
13 import org.apache.struts2.util.StrutsTypeConverter;
14 
15 import com.opensymphony.xwork2.ActionContext;
16 import com.sun.jndi.url.dns.dnsURLContext;
17 
18 public class DateConverter extends StrutsTypeConverter {
19 
20     private DateFormat dateFormat;
21     
22     public void setDateFormat(DateFormat dateFormat) {
23         //获取当前web应用下的初始化参数
24         
25         ServletContext servletContext = ServletActionContext.getServletContext();
26         String pattern = servletContext.getInitParameter("datePattern");
27         dateFormat = new SimpleDateFormat(pattern);
28         
29     }
30     
31     public DateFormat getDateFormat() {
32         return dateFormat;
33     }
34     
35     @Override
36     public Object convertFromString(Map context, String[] values, Class toClass) {
37         
38         System.out.println("convertFromString ... ");
39         if(toClass == Data.class){
40             if(values != null && values.length > 0){
41                 String value = values[0];
42                 try {
43                     return getDateFormat().parseObject(value);
44                 } catch (ParseException e) {
45                     
46                     e.printStackTrace();
47                 }
48             }
49         }
50         //若没有转换成功,则返回values
51         return values;
52     }
53 
54     @Override
55     public String convertToString(Map context, Object o) {
56         System.out.println("convertToString ...");
57         if( o instanceof Date){
58             Date date = (Date) o;
59             return getDateFormat().format(date);
60         }
61         //若转换失败,则返回null
62         return null;
63     }
64 
65 }
DateConverter
1 java.util.Date=com.jason.strut2.conversion.DateConverter
xwork-conversion.properties
1 <context-param>
2       <param-name>datePattern</param-name>
3       <param-value>yyyy-MM-dd hh:mm:ss</param-value>
4   </context-param>
5   
web.xml
1 <action name="testComplexPropertyAction" class="com.jason.struts2.model.TestComplexPropertyAction">
2              <result>/success.jsp</result>
3          </action>
strut.xml
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 
12     <s:form action="testComplexPropertyAction">
13         <s:textfield name="deptName" label="DeptName"></s:textfield>
14         <!-- 映射属性的属性 -->
15         <s:textfield name="manager.name" label="ManagerName"></s:textfield>
16         <s:textfield name="manager.birth" label="ManagerBirth"></s:textfield>
17         
18         <s:submit></s:submit>
19     </s:form>
20 
21 </body>
22 </html>
complex-property.jsp

 

 

 

核心点:

  1.struts 的表单标签支持:映射属性的属性

  

1 <s:form action="testComplexPropertyAction">
2         <s:textfield name="deptName" label="DeptName"></s:textfield>
3         <!-- 映射属性的属性 -->
4         <s:textfield name="manager.name" label="ManagerName"></s:textfield>
5         <s:textfield name="manager.birth" label="ManagerBirth"></s:textfield>
6         
7         <s:submit></s:submit>
8     </s:form>
View Code

 

 

  2.通过配置全局的:类型转换类,和配置。进行类型自动的转换

    xwork-conversion.properties

  

1 java.util.Date=com.jason.strut2.conversion.DateConverter

 

  

 

 

 

 

 

 

 

 

  

posted @ 2016-09-28 12:03  jason_zhangz  阅读(245)  评论(0编辑  收藏  举报