struts-基础内容-6-数据的自动封装和类型转换

来源:互联网 发布:大数据的价值体现在 编辑:程序博客网 时间:2024/05/17 19:56
请求数据的自动封装

实现的原理-参数处理拦截器

<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

方式1.jsp表单数据填充到action中的属性

 

<%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-7-9  Time: 下午11:39  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body>     <form action="${pageContext.request.contextPath}/user_register.action" method="post">          用户名:<input type="text" name="name"> <br>           密码:<input type="password" name="pwd"> <br>         年龄:<input type="text" name="age"> <br>         生日: <input type="text" name="birth"> <br>         <input type="submit" value="注册">     </form></body></html>
import java.util.Date;/** * * struts核心业务,请求数据自动封装及类型转换 * Created by cxspace on 16-7-10. */public class UserAction {    //拿到请求数据    private String name;    private String pwd;    private int age;    private Date birth;    //必须要给set方法get可以不用给    public void setName(String name) {        this.name = name;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }    public void setAge(int age) {        this.age = age;    }    public void setBirth(Date birth) {        this.birth = birth;    }    //处理注册请求     public String register(){         System.out.println(name);         System.out.println(pwd);         System.out.println(age);         System.out.println(birth);         return "success";     }}

 

方式2.jsp表单数据填充到action中的对象中的属性-对象类型一定要给get方法

User

package com.cx.type;import java.util.Date;/** * Created by cxspace on 16-7-10. */public class User {    //拿到请求数据    private String name;    private String pwd;    private int age;    private Date birth;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPwd() {        return pwd;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }    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;    }}
Action

package
com.cx.type;/** * * struts核心业务,请求数据自动封装及类型转换 * Created by cxspace on 16-7-10. */public class UserAction { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } //必须要给set方法get可以不用给 //处理注册请求 public String register(){ System.out.println(user.getName()); System.out.println(user.getPwd()); System.out.println(user.getAge()); System.out.println(user.getBirth()); return "success"; }}
jsp

<%-- Created by IntelliJ IDEA. User: cxspace Date: 16-7-9 Time: 下午11:39 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body> <form action="${pageContext.request.contextPath}/user_register.action" method="post"> 用户名:<input type="text" name="user.name"> <br> 密码:<input type="password" name="user.pwd"> <br> 年龄:<input type="text" name="user.age"> <br> 生日: <input type="text" name="user.birth"> <br> <input type="submit" value="注册"> </form></body></html>

 

类型转换-转换器的使用

Strus中 jsp提交的数据,struts会自动转换为action中属性的类型

对于基本数据类型以及日期类型会自动转换

日期类型只支持yyyy-MM-dd格式

需要自定义类型转换器

局部类型转换器

全局类型转换器

Struts转换器api

|----TypeConverter  转换器接口

     |----默认类型转换器类

         |----StrutsTypeConverter  转换器类

把19900318  ==> 1990-03-18

一.局部类型转换

1.写转换器类

 

package com.cx.type;import org.apache.struts2.util.StrutsTypeConverter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Map;/** * Created by cxspace on 16-7-10. */public class MyConverter extends StrutsTypeConverter {    /*    *    * 把String 转换到特定类型    *    *    * context当期上下问环境    *    * values表单中字符串值    *    *    * */    @Override    public Object convertFromString(Map context, String[] values, Class aClass) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");        try {            return sdf.parse(values[0]);        } catch (ParseException e) {            throw new RuntimeException(e);        }    }    @Override    public String convertToString(Map map, Object o) {        return null;    }}

2.配置转换器类

    在同包的action目录下新建属性配置文件

   该文件命名规则:ActionClassName-conversion.properties

3.转换内容

 配置文件内容-包含要转换的属性和对应的处理类

 

user.birth=com.cx.type.MyConverter  

 user.birth=转换器类全路径(com.cx.type.MyConverter)

4.这个转换器不能给其他的action用

二.全局转换器

写一个转换器给所有action用

-->在src目录下建xwork-conversion.properties

-->内容

     转换的类型=转换器全路径

 

package com.cx.type;import org.apache.struts2.components.Date;import org.apache.struts2.util.StrutsTypeConverter;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Map;/** * Created by cxspace on 16-7-10. */public class MyConverter extends StrutsTypeConverter {    /*    *    * 把String 转换到特定类型    *    *    * context当期上下问环境    *    * values表单中字符串值    *    *    * */    //如果项目中要支持的类型很多  yyyy-MM-dd/yyyyMMdd/yyyy年MM月dd日    DateFormat  [] df = {            new SimpleDateFormat("yyyy-MM-dd"),            new SimpleDateFormat("yyyyMMdd"),            new SimpleDateFormat("yyyy年MM月dd日")    };    //定义项目中支持的转换格式    @Override    public Object convertFromString(Map context, String[] values, Class aClass) {            //判断内容不能为空            if (values==null||values.length==0)            {                return null;            }            //判断类型必须为Date            if(Date.class!=aClass)            {                return null;            }        //转换失败下一次循环        for(int i=0 ; i < df.length ; i++)        {            try {                return df[i].parse(values[0]);            } catch (ParseException e) {                  continue;            }        }        return null;    }    @Override    public String convertToString(Map map, Object o) {        return null;    }}

 

 

 

    

 

0 0
原创粉丝点击