zoukankan      html  css  js  c++  java
  • Struts 类型转换之局部和全局配置

    我们碰到过很多情况,就是时间日期常常会出现错误,这是我们最头疼的事,在struts2中有一些内置转换器,也有一些需要我们自己配置。 

    我们为什么需要类型转换呢?

    在基于HTTP协议的Web应用中 客户端请求的所有内容(表单中提交的内容等)都以文本编码的方式传输到服务器端
    但服务器端的编程语言(如Java)有着丰富的数据类型 如 int boolean Date 等等
    因此 当这些参数进入应用程序时 必须被转换成适合的服务器端编程语言的数据类型

    在Servlet中 类型转换工作是由开发者自己完成的,例如:

      String s=request.getParameter("pageIndex");
      int pageIndex=Integer.parseInt(s);

    类型转换的工作是必不可少的 重复性的. 如果有一个良好的类型转换机制 必将大大节省开发时间,提高开发效率。

    Struts2提供了强大的类型转换功能:

    内置转换器:

    对于大部分的常用类型 开发人员在开发过程中不需要自己编码实现类型转换 这是因为在Struts2框架爱中可以完成大多数常用的类型转换
     //由内部提供的类型转换器完成
     1.String 将 int long double boolean String数组或者java.util.Date类型转换为字符串
     2.boolean/Boolean 在字符串和布尔值之间转换
     3.char/Character 在字符串和字符之间进行转换
     4.int/Integer float/Float  long/Long double/Double
     5.Date 在字符串和日期只见进行转换 具体输入输出格式与当前的local有关
     6.数组和集合 在字符串数组和数组对象 几何对象之间进行转换

    接下来我们讲解一下自定义类型转换器:

    1.0创建一个Action来继承ActionSupport

    public class StudentAction extends ActionSupport {
        private Date date;
    
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        @Override
        public String execute() throws Exception {
            System.out.println(date);
            return SUCCESS;
        }
    }

    2.0创建一个DateAction继承StrutsTypeConverter

    public class DateAction extends StrutsTypeConverter {
    
        private static final List<SimpleDateFormat> list=new ArrayList<SimpleDateFormat>();
        static {
            list.add(new SimpleDateFormat("yyyy/MM/dd"));
            list.add(new SimpleDateFormat("yyyy-MM-dd"));
        }
        public Object convertFromString(Map context, String[] values, Class toClass) {
            String date=values[0];
            for (SimpleDateFormat item:list){
                try {
                    return item.parse(date);
                } catch (ParseException e) {
                   continue;
                }
            }
          throw new TypeConversionException();
        }
    
        public String convertToString(Map map, Object o) {
            System.out.println(new Date()+"============================");
            return o.toString();
        }
    }

    3.0创建StudentAction-conversion.properties

    date=cn.converter.DateAction

    4.0Struts.xml中的配置

        <package name="default" namespace="/" extends="struts-default">
            <action name="stuAction" class="cn.action2.StudentAction">
                <result>/converter/success.jsp</result>
                <result name="input">/converter/index.jsp</result>
            </action>
        </package>

    5.0页面

    index.jsp:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <html>
    <title>登陆页面</title>
    <body>
    <s:fielderror></s:fielderror>
    <s:form action="stuAction" method="POST">
        <s:textfield name="date"></s:textfield>
        <s:submit value="提交"></s:submit>
    </s:form>
    </body>
    </html>

    success.jsp:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
        <title>成功页面</title>
    </head>
    <body>
    欢迎你:<s:property value="date"></s:property></body>
    </html>

    这就是自己设置类型转换的案例。。。。。

  • 相关阅读:
    uva 147 Dollars
    hdu 2069 Coin Change(完全背包)
    hdu 1708 Fibonacci String
    hdu 1568 Fibonacci
    hdu 1316 How Many Fibs?
    poj 1958 Strange Towers of Hanoi
    poj 3601Tower of Hanoi
    poj 3572 Hanoi Tower
    poj 1920 Towers of Hanoi
    筛选法——素数打表
  • 原文地址:https://www.cnblogs.com/wl0000-03/p/6538536.html
Copyright © 2011-2022 走看看