第一步:定義父類java
package group.esperanto.action.util; import java.text.SimpleDateFormat; import java.util.Locale; import javax.annotation.Resource; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.context.MessageSource; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; public class AbstractAction { @InitBinder public void initBinder(WebDataBinder binder){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 本方法的處理旨在追加一個自定義的轉換編輯器,碰見java.util.Date類型就使用定義好的SimpleDateFormat來格式化處理, binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true)); //這裏的true表明容許參數內容爲空 } }
第二步:子類中繼承web
package group.esperanto.action; import java.io.IOException; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.sound.midi.MidiDevice.Info; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import group.esperanto.action.util.AbstractAction; @Controller // 定義該Action的映射路徑 // 該路徑絕對不能重複 @RequestMapping("/pages/emp/*") public class EmpAction extends AbstractAction { //繼承父類 private Logger log = Logger.getLogger(EmpAction.class); @RequestMapping("echo") //映射方法名 public void echo(String msg,Date date){ //第二個參數爲日期類型,將被父類定義的日期格式方法處理 Logger.getLogger(EmpAction.class).info(msg); Logger.getLogger(EmpAction.class).info(date); } }