1. 寫好轉換用的檔
  2. 寫好設定檔:xwork-conversion.properties (放在跟struts.xml相同的地方)
  3. Struts.xml內一樣要設定    <result name="input">/addBook2.jsp</result> ,另外一樣可以設定資源檔xxx.properties來寫各種提示標語
  4. 也可以在輸入頁直接使用<sx:datetimepicker>強制用小日曆選日期,就只需要步驟1、2了


package datetime.converter;

 

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

 

public class SqlDateConverter extends StrutsTypeConverter { //必須繼承StrutsTypeConverter

 

   @Override //必須複寫

  public Object convertFromString(Map context, String[] values, Class toClass) {

      DateFormat[] SqlDateFormat = {

             new SimpleDateFormat("yy-MM-dd"),  new SimpleDateFormat("yyyy-MM-dd"),

             new SimpleDateFormat("yy/MM/dd"),  new SimpleDateFormat("yyyy/MM/dd")

      };

      for (DateFormat df : SqlDateFormat)

          try {

             //把接收進來的資料values(這是一個陣列,但實際上以時間來說只會有一個元素也就是values[0]存在)轉換成想要的格式

             //如果轉換成功就會return,失敗會被catch,但因為catch後甚麼都不做,所以會繼續輪下一個

             java.util.Date date = (java.util.Date) df.parse(values[0]);

             return new java.sql.Date(date.getTime());

          } catch (ParseException e) {

          }

      return null;

   }

 

   @Override //轉換成正確格式的Object後,再把他轉成String以供viewHTML使用

   public String convertToString(Map context, Object obj) {

      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

      return df.format(obj);

   }

 

   public static void main(String args[]) {

      SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");

      try {

          java.util.Date date = (java.util.Date) sdf.parse("99-07-31");

          System.out.println("java.sql.Date="+ new java.sql.Date(date.getTime()));

 

          String str = sdf.format(date);

          System.out.println("str=" + str);

      } catch (ParseException e) {

          e.printStackTrace();

      }

   }

}



xwork-conversion.properties

 

java.sql.Date=datetime.converter.SqlDateConverter

java.sql.Time=datetime.converter.TimeConverter

java.sql.Timestamp=datetime.converter.TimestampConverter

java.util.Date=datetime.converter.UtilDateConverter



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

   "http://struts.apache.org/dtds/struts-2.3.dtd">

 

<struts>

   <constant name="struts.devMode" value="true" />

  

   <!--以下設定全域範圍的國際化資源檔,檔名globalMessages起頭,如globalMessages_zh_TW.properties-->

   <!--globalMessages名稱可自取 -->

   <!--無預設值 -->

    <constant name="struts.custom.i18n.resources" value="globalMessages" />

       

   <package name="main"  namespace="/Book"  extends="struts-default">

  

       <!-- 以下的 <global-results> <global-exception-mappings> 適用於此 package 內所 acton -->

      <!-- 在執行中如果出現 Exception 時會導向根目錄的 error.jsp 然後由 error.jsp 顯示 500堆疊 -->

      <!-- 此設定可有可無,不一定要設  -->

      <global-results>

          <result name="error">/error.jsp</result>

      </global-results>

      <global-exception-mappings>

          <exception-mapping exception="java.lang.Exception"

             result="error" />

      </global-exception-mappings>

              

      <action name="bookAction1" class="action.BookAction1" method="addBook">

          <result name="success">/addSuccess1.jsp</result>

          <result name="input">/addBook1.jsp</result>

      </action>

     

      <action name="bookAction2" class="action.BookAction2" method="addBook">

          <result name="success">/addSuccess2.jsp</result>

          <result name="input">/addBook2.jsp</result>

   <!-- The action execution require more input in order to succeed. -->

      </action>

     

   </package>

</struts>



xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".  -  (by  global.properties)

arrow
arrow
    全站熱搜

    乙方 發表在 痞客邦 留言(0) 人氣()