• Struts2自定义转换器输入生日日期输出年、月、日、年龄


    BirthAction.java

    package com.action;
    
    import java.util.Calendar;
    import java.util.Map;
    import com.bean.Birth;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class BirthAction extends ActionSupport {
        private Birth bir;
        
        public Birth getBir() {
            return bir;
        }
    
        public void setBir(Birth bir) {
            this.bir = bir;
        }
    
        public String execute() throws Exception {
            Calendar cal = Calendar.getInstance(); 
            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH)+1; 
            int dayNow = cal.get(Calendar.DAY_OF_MONTH);
            
            int yearBirth=Integer.parseInt(bir.getYear());
            int monthBirth=Integer.parseInt(bir.getMonth());
            int dayBirth=Integer.parseInt(bir.getDay());
            
            int age=yearNow-yearBirth;
            if(monthNow<=monthBirth){
                if(monthNow == monthBirth){
                    if (dayNow < dayBirth) age--; 
                }
                else{
                    age--;
                }
            }
            Map session = ActionContext.getContext().getSession();
            session.put("age", age);
            return SUCCESS;
        }
    }

    BirthConverter.java:

    package com.action;
    
    import java.util.Map;
    
    import org.apache.struts2.util.StrutsTypeConverter;
    
    import com.bean.Birth;
    
    public class BirthConverter extends StrutsTypeConverter {
        @Override
        public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
            Birth bir = new Birth();
            String[] str = arg1[0].split("-");
            bir.setYear(str[0]);
            bir.setMonth(str[1]);
            bir.setDay(str[2]);
            return bir;
        }
        @Override
        public String convertToString(Map arg0, Object arg1) {
            Birth bir = (Birth)arg1;
            return "[" + bir.getYear() + "-" + bir.getMonth() + "-" + bir.getDay() + "]";
        }
    }

    BirthAction-conversion.properties:

    bir=com.action.BirthConverter
    

      Birth.java:

    package com.bean;
    
    public class Birth {
        private String year;
        private String month;
        private String day;
        public String getYear() {
            return year;
        }
        public void setYear(String year) {
            this.year = year;
        }
        public String getMonth() {
            return month;
        }
        public void setMonth(String month) {
            this.month = month;
        }
        public String getDay() {
            return day;
        }
        public void setDay(String day) {
            this.day = day;
        }
        
        
    }
    struts.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    
        <package name="default" extends="struts-default">
            <action name="myBirth" class="com.action.BirthAction">
                <result name="success">/show.jsp</result>
            </action>
        </package>
        
    </struts>    

    index.jsp

    <%@taglib uri="/struts-tags" prefix="s" %>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>Date界面</title>
      </head>
      <body>
      
      <s:form action="myBirth" method="post">
        <s:textfield name="bir" label="请输入生日"></s:textfield>
        <s:submit value="提交"></s:submit>
      </s:form>
      
      </body>
    </html>

    show.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib  uri="/struts-tags" prefix="s"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>Date界面</title>
      </head>
      
      <body>
      生日:<s:property value="bir"/><br>
       年:<s:property value="bir.year"/><br>
       月:<s:property value="bir.month"/><br>
       日:<s:property value="bir.day"/><br>
       年龄:<%=session.getValue("age") %>
      </body>
    </html>
  • 相关阅读:
    关于Update语句在不同数据库中的差别
    MSIL指令速查表
    一个对于博客园的建议
    代码风格关于if语句
    关于Page.cs文件中注释的一点补充
    在Java、C#和C++中遍历集合
    BPEL4WS的开源Java实现
    【Linux】linux固定ip
    【Linux】【MySQL】MySQL主从数据库
    wpf 写个简单的控件吧
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9975583.html
Copyright © 2020-2023  润新知