• 数据类型转换(日期格式转换)


    首先我们先去配置一个自定义类型转换器

    public class Dateconverter extends StrutsTypeConverter{
    	   //将前台获取到的值转换成Date类型
    	   private final DateFormat[] dfs={
    			   new SimpleDateFormat("yyyy/MM/dd"),
    			   new SimpleDateFormat("yyyy-MM-dd"),
    			   new SimpleDateFormat("yyyy年MM月dd日"),
    			   new SimpleDateFormat("yyyy.MM.dd"),
    			   new SimpleDateFormat("yyyyMMdd"),
    	   };
    
    	
    	/**
    	 * String类转换成特定的类
    	 * 用于前台传值到后台
    	 */
         // String[] values 表示从表单传过来的相同名字的字符串数组 @Override // 前台创来的值 public Object convertFromString(Map context, String[] values, Class toType) { String value=(String)values[0]; for (int i = 0; i < dfs.length; i++) { try { return dfs[i].parse(value); } catch (ParseException e) { continue; } } throw new TypeConversionException(); } /** * 前台取值 * */
         //   Object o表进通过convertFromString方法转换成的自定义类型 @Override public String convertToString(Map context, Object obj) { return new SimpleDateFormat("yyyy-MM-dd").format(obj); } }

    抽象类StrutsTypeConverter继承了DefaultTypeConverter接口,有如下方法:

    abstract Object convertFromString(Map context,String[] values, Class toClass) 
              Converts one or more String values to the specified class.
    abstract String convertToString(Map context,Object o) 
              Converts the specified object to a String.
    Object convertValue(Map context,Object o, Class toClass)

    StrutsTypeConverter简化了类型转换代码的编写,StrutsTypeConverter继承DefaultTypeConverter,提供了两个抽象的方法convertFromString()和convertToString,分别表示从页面的字符串转换为后台对象以及从后台对象转为页面的字符串,我们只需要实现这两个抽象方法即可实现类型转换。

    接下来我们需要创建一个全局类型转换:

        在src目录下新建xwork-conversion.properties(该名称固定)。该文件的内容是待转换的类=转换器的名字如:com.struts2.bean.User = cm.struts2.converter.UserConverter。

    然后就是jsp页面:

    一:上传页面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!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>
      <s:fielderror/>
       <form action="AddUser" method="post">
       	姓名:<input type="text" name="user.name"/><br/>
       	年龄:<input type="text" name="user.age"/><br/>
       	出生日期:<input type="text" name="user.birthday"/><br/>
       	<input type="submit" value="提交"/>
       </form>
      </body>
    </html>
    

    二:成功页面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'success.jsp' starting page</title>
       
      </head>
      
      <body>
    	<s:property value="user.birthday"/>
      </body>
    </html>
    

      jsp里的代码我就不多过多的解释了,大家都懂!!!!

    我们来看一下执行结果:

    上传页面

    成功页面

    谢谢大家!!!!!!!!!!!!!!!!!!!!!!!!

    请大家多多关注

  • 相关阅读:
    “浪潮杯”第九届山东省ACM大学生程序设计竞赛 F: Four-tuples容斥定理
    B
    C. Tourist Problem 2021.3.29 晚vj拉题 cf 1600 纯数学题
    C. Sum of Cubes
    Day29、Python中的异常处理及元类
    isinstance,issubclass,反射,内置方法(__str__,__del__,__call__)
    绑定方法与非绑定方法;classmethod及staticmethod装饰器
    组合,多态,封装
    类的继承
    面向对象编程思想基本介绍,类与对象的基本使用,属性查找,绑定方法
  • 原文地址:https://www.cnblogs.com/superws/p/5945113.html
Copyright © 2020-2023  润新知