#副標=讓Spring解放你的程式碼 (4) #大標=春天的迷「網」 #眉標=J2EE #作者=文/歐宣修 ============box 程式1============== contextConfigLocation /WEB-INF/sccbo-data.xml org.springframework.web.context. ContextLoaderListener   sccbo org.springframework.web.servlet. DispatcherServlet 1 sccbo *.htm ==============end================ ============box 程式2=========== public class TempQueryController extends SimpleFormController { private static Logger log = Logger.getLogger( TempQueryController.class); private RealtimeTempDao realtimeTempDao; public TempQueryController() { //--(1) this.setCommandClass( TempQueryCommand.class); } //--(2) protected ModelAndView onSubmit(Object command,BindException errors) throws Exception { log.debug("Enter onsubmit"); TempQueryCommand tempQueryCommand = (TempQueryCommand) command; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //--(3) if (tempQueryCommand.getStartDate() == null || tempQueryCommand.getEndDate() == null) { //first come in //Set default value //today String today = sdf.format(new Date( System.currentTimeMillis() ) ); tempQueryCommand.setStartDate(today ); tempQueryCommand.setEndDate(today ); //--(4) return new ModelAndView(this.getFormView(),"tempQueryCommand", tempQueryCommand); } //--(5) Timestamp startDate = new Timestamp(sdf. parse(tempQueryCommand.getStartDate()).getTime()); Timestamp endDate = new Timestamp(sdf. parse(tempQueryCommand.getEndDate()).getTime()); RealtimeTemp[] resultList = this.realtimeTempDao.findByReadtime( startDate , endDate ); tempQueryCommand.setResultList(resultList); //--(6) return new ModelAndView(this. getSuccessView(),"tempQueryCommand", tempQueryCommand); } /** * @param realtimeTempDao The realtimeTempDao to set. */ public void setRealtimeTempDao( RealtimeTempDao realtimeTempDao) { this.realtimeTempDao = realtimeTempDao; } } ==============end========== ===========box 程式3============ public class TempQueryCommand { private String startDate; private String endDate; private RealtimeTemp[] resultList; /** * @return Returns the endDate. */ public String getEndDate() { return endDate; } /** * @param endDate The endDate to set. */ public void setEndDate(String endDate) { this.endDate = endDate; } /** * @return Returns the startDate. */ public String getStartDate() { return startDate; } /** * @param startDate The startDate to set. */ public void setStartDate(String startDate) { this.startDate = startDate; } /** * @return Returns the resultList. */ public RealtimeTemp[] getResultList() { return resultList; } /** * @param resultList The resultList to set. */ public void setResultList(RealtimeTemp[] resultList) { this.resultList = resultList; } } =============end============= ===========box 程式4============= TempQuery2.jsp <%@ page contentType="text/html; charset=UTF-8"%> <%--宣告JSTL Tag,使用JSTL作為取得 Controller回傳資料的工具--%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="spring" uri="http://www. springframework.org/tags" %>       <%--用於顯示錯誤訊息--%>         
<%--查詢條件區塊--%>   
啟始時間 結束時間
<%--查詢結果區塊--%> <%--透過TempQueryCommand之 resultList,將一筆筆資料倒出來--%>
時間 編號 溫度
============end============ ============box 程式5============ //--(1) //--(2) //--(3) tempQuery2 tempQuery2 //--(4) /WEB-INF/jsp/ .jsp =================end======================