有一天你會長大
什么話該說,什么話不該說,該說的話該怎么說。誰能告訴我。
posted @ 2010-01-19 13:21 追求無止境 閱讀(204) | 評論 (0) | 編輯 收藏
追求無止境我的程序人生
隨筆 - 31, 文章 - 2, 評論 - 20, 引用 - 0
|
2009年度盛事 - 總結(jié)20092009年12月31日,站在2009年的尾巴上,不禁感到時間飛逝。2009年,匆匆而過。在過去的2009年,收獲何多,失去何多。2009年,對我來說,是一個重要的轉(zhuǎn)折點。漸漸的發(fā)現(xiàn)自己應(yīng)該長大了,也發(fā)現(xiàn)自己確實長大了,更發(fā)現(xiàn)自己實際上還是需要繼續(xù)長大。 每年的這個時候,各個新聞媒體都會評出什么十大之類的,我也落一會俗(當(dāng)然自己本身就很俗),來看看今年發(fā)生在我身上的幾大事件: 先大體羅列一下吧:
從時間跨度上來說, 1月到2月在家過年,2月和3月在青島某公司實習(xí),4月和5月在北京某公司實習(xí),6月在中國石油大學(xué)享受畢業(yè)之前的時光,7月到8月繼續(xù)在北京某公司實習(xí),9月到12月在上學(xué)+實習(xí)。2009年最幸福的事情發(fā)生在1到2月,盡快也有不快;最平常的日子在2月和3月,到了4月和5月或許是我最糾結(jié)的日子吧;6月或許是最快樂的日子的吧;7月和8月讓我體會到了工作的滋味;而9月到12月,整天在公司與學(xué)校之間奔跑,知道了工作+上課兩者要做到兼顧的滋味,雖然并沒有做到兼顧。 2009年大體經(jīng)歷如此。2009年對我最大的關(guān)鍵字或許就是“改變”,這一年我訂婚了,在這一點上,改變了我的準(zhǔn)非單身狀態(tài);在這一年,我實習(xí)了,而且大量的時間都在于此,改變了我僅僅是學(xué)生的狀態(tài);在這一年,我畢業(yè)了,我離開了生活學(xué)習(xí)四年的中國石油大學(xué),離開了讓我畢生難忘的日子;在這一年,我來北京了,從對北京的一無所知,到開始的彷徨,然后漸漸熟悉和適應(yīng);在這一年,我的經(jīng)濟(jì)漸漸獨(dú)立,盡管每個月只有不到1000的收入,但能滿足的我的基本需求;在這一年,我買了筆記本,雖然對其他人來說,這不是一件特別的事,對我來說,因采用了分期付款,而用接下來一年中近半個月的工資來還,但我不覺得后悔,在這個過程中,我的經(jīng)濟(jì)觀念和理財觀念開始漸漸改變;在這一年,工作成了我的核心,工作教會我很多東西,在與人交流、在工作態(tài)度、在技術(shù)上,都有一定的提高。 回憶2009年,收獲頗多,也失去不少。但日子總是向前的,有得必有失。希望在以后的2009年,自己能夠漸漸提高自己的能力。無論是在生活上,在工作上,還是在心理上。
2009年10大大事: 與老婆訂婚: 2009年, posted @ 2009-12-31 12:55 追求無止境 閱讀(209) | 評論 (0) | 編輯 收藏 Spring web MVC 框架學(xué)習(xí)筆記 之 ViewResolver技術(shù)上次的文章中介紹了ModelAndView對象中的view對象,可以使用字符串來讓Spring框架進(jìn)行解析獲得適合的視圖。而解析View的就是ViewResolver技術(shù)。 ViewResolver的定義如下: public interface ViewResolver { 在[spring-dispatcher-name]-servlet.xml中,可以定義viewResolver: <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 來讓DispacherServlet進(jìn)行加載默認(rèn)的viewResolver,如果沒有設(shè)置viewResolver,spring使用InternalResourceViewResolver進(jìn)行解析。 Spring實現(xiàn)ViewResolver的非抽象類且我們經(jīng)常使用的viewResolver有以下四種:
使用多視圖解析器: 我們不想只使用一種視圖解析器的話,可以在[spring-dispatcher-name]-servlet.xml定義多個viewResolver: <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> DispatcherServlet會加載所有的viewResolver到一個list中,并按照優(yōu)先級進(jìn)行解析。注意order中的值越小,優(yōu)先級越高。而id為viewResolver 的viewResolver的優(yōu)先級是最低的。 posted @ 2009-11-27 12:11 追求無止境 閱讀(6881) | 評論 (1) | 編輯 收藏 Spring MVC框架學(xué)習(xí)筆記 之 View技術(shù)以前,我們詳細(xì)介紹了Spring的Controller技術(shù)。Spring的面向接口編程,使Controller的實現(xiàn)多種多樣。View技術(shù)也一樣。今天的分析先從在Controller中的ModelAndView開始。 public class ModelAndView { private Object view; //View實例或者view的字符串 /** Model Map */ private ModelMap model; //model /* * Convenient constructor when there is no model data to expose. * Can also be used in conjunction with <code>addObject</code>. * @param view View object to render * @see #addObject */ public ModelAndView(View view) { this.view = view; } public ModelAndView(String viewName){ this.view = viewName; } 可以看到view實例可以指向一個View對象或者字符串。現(xiàn)在先看看View接口: public interface View { /** * Return the content type of the view, if predetermined. * <p>Can be used to check the content type upfront, * before the actual rendering process. * @return the content type String (optionally including a character set), * or <code>null</code> if not predetermined. */ String getContentType(); /** * 繪制視圖 * 繪制視圖的第一步是準(zhǔn)備請求: 如果是JSP的視圖技術(shù) * 首先會把model設(shè)為request的屬性。 * 第二步則是真正的繪制視圖 * @param model Map with name Strings as keys and corresponding model * objects as values (Map can also be <code>null</code> in case of empty model) * @param request current HTTP request * @param response HTTP response we are building * @throws Exception if rendering failed */ void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; } 在這之后,我們再來看看View的實現(xiàn)繼承類圖,可以看到Spring支持多種類型視圖: org.springframework.web.servlet.view.AbstractView (implements org.springframework.beans.factory.BeanNameAware, org.springframework.web.servlet.View)
和Controller一樣,View的第一個實現(xiàn)也是AbstractView。所以先讓我們看看AbstractView對render函數(shù)的實現(xiàn):
public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 第一步,將靜態(tài)屬性和model的屬性都添加到mergedModel里面。如果需要請求上下文,則將請求上下文添加到model中。 靜態(tài)屬性是繼承AbstractView進(jìn)行設(shè)置的屬性。而請求上下文如果設(shè)置的名字就會創(chuàng)建一個request上下文。在requestContext中定義了一些包括本地化和主題的處理工具。 第二步,對響應(yīng)進(jìn)行預(yù)處理。最后調(diào)用子類需要實現(xiàn)的函數(shù)renderMergedOutputModel。 對PDF和EXCEL格式我們暫且不管,且Spring支持多種視圖技術(shù),這里我們主要關(guān)注JSTL技術(shù), 接著我們來看AbstractUrlBasedView 類。在AbstractUrlBasedView 只定義了一個url屬性。別的沒有什么特殊處理。 接著繼承AbstractUrlBasedView 的是InternalResourceView。他對renderMergedOutputModel進(jìn)行實現(xiàn),實現(xiàn)如下: /** exposeModelAsRequestAttributes(model, requestToExpose); 可以看到InternalResourceView對請求進(jìn)行了轉(zhuǎn)發(fā)。轉(zhuǎn)發(fā)到url上。最后我們看看JSTLView的實現(xiàn):
public class JstlView extends InternalResourceView { private MessageSource messageSource; public JstlView() { } public JstlView(String url) { super(url); } public JstlView(String url, MessageSource messageSource) { this(url); this.messageSource = messageSource; } protected void initServletContext(ServletContext servletContext) { if (this.messageSource != null) { this.messageSource = JstlUtils.getJstlAwareMessageSource(servletContext, this.messageSource); } super.initServletContext(servletContext); } protected void exposeHelpers(HttpServletRequest request) throws Exception { if (this.messageSource != null) { JstlUtils.exposeLocalizationContext(request, this.messageSource); } else { JstlUtils.exposeLocalizationContext(new RequestContext(request, getServletContext())); } } }
在InternalResourceView 中,基本上所有的處理都差不多了。在JSTLView對兩個方法進(jìn)行了覆蓋。第一個initServletContext,主要初始化了MessageResource 第二個exposeHelpers將messageSource放在了request里面。 這樣view的解析就結(jié)束了。接下來容器對jsp進(jìn)行解析,并進(jìn)行tag等的處理。然后將生成的頁面返回給客戶端。 posted @ 2009-11-26 13:25 追求無止境 閱讀(8209) | 評論 (2) | 編輯 收藏 SpringMVC web框架學(xué)習(xí) Controller 分析org.springframework.web.servlet.mvc.AbstractController (implements org.springframework.web.servlet.mvc.Controller)
Spring MVC框架中的Controller對請求進(jìn)行處理:所有的Controller都實現(xiàn)接口Controller: public interface Controller { /** * Process the request and return a ModelAndView object which the DispatcherServlet * will render. A <code>null</code> return value is not an error: It indicates that * this object completed request processing itself, thus there is no ModelAndView * to render. * @param request current HTTP request * @param response current HTTP response * @return a ModelAndView to render, or <code>null</code> if handled directly * @throws Exception in case of errors */ ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception; } 上面的doc表明Controller返回的modelandview可以使空,表明請求都是該函數(shù)中處理完成了,不需要modeland來進(jìn)行渲染。 在繼續(xù)之前先介紹一個有用的工具類:WebUtils。用這個可以簡化session,request的處理。具體的內(nèi)容可以參考文檔。 Controller的第一個實現(xiàn)是:AbstractController。他是一個Abstract類,除了實現(xiàn)了Controller接口,它還繼承了WebContentGenerator。 WebContentGenerator的作用是什么?參考文檔可以發(fā)現(xiàn),該類主要對Cache和Session進(jìn)行管理。
用戶可以對這些參數(shù)進(jìn)行測試,cache和expire信息涉及到了http協(xié)議信息,更多信息可以參考http協(xié)議文檔。這里不再說明。 再看AbstractController的代碼: public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) 他會檢查支持的方法,和會話,然后應(yīng)用cache設(shè)置。 如果需要session同步,就進(jìn)行同步處理。session同步應(yīng)用于有session的情況下。如果沒有session,session同步是沒有用的。 AbstractController會調(diào)用handleRequestInternal方法進(jìn)行處理,繼承AbstractController的類需要實現(xiàn)該方法。 下面我們再看看AbstractUrlViewController 的代碼實現(xiàn)和文檔,先看handleRequestInternal的實現(xiàn):
/** 可以看到,它使用了getViewNameForRequest獲取需要的viewName。而getViewNameForRequest是一個抽象函數(shù),需要子類實現(xiàn)。lookupPath就是我們請求的URL中的一部分。如我們使用UrlFilenameViewController來進(jìn)行如下的配置:
<bean name="/index.do" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>、
09-11-25 11:56:06 - DEBUG [http-8200-1] - Returning view name 'index' for lookup path [/index.do]
該Controller對/index.do解析成index,然后再通過viewResolver對index進(jìn)行擴(kuò)展為/jsp/index.jsp。從而找到該頁面。
可以看到這個類的主要是用于對url進(jìn)行解析,然后轉(zhuǎn)到合適的頁面上,而在轉(zhuǎn)到這個頁面之前不需要進(jìn)行特別的處理。
明白了該類的作用自然也就知道了UrlFilenameViewController的作用。這里不再進(jìn)行詳細(xì)分析。
posted @ 2009-11-26 09:35 追求無止境 閱讀(4166) | 評論 (0) | 編輯 收藏 Spring MVC 框架學(xué)習(xí)之AbstractFormController以及AbstractFormControll在看完BaseCommandController和AbstractCommandController之后,我們再看BaseCommandController的另一個實現(xiàn)AbstractFormController,以及AbstractFormController的具體實現(xiàn)SimpleFormController。 先看看AbstractFormController對handleRequestInternal的實現(xiàn): protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { // Form submission or new form to show? if (isFormSubmission(request)) { // Fetch form object from HTTP session, bind, validate, process submission. try { Object command = getCommand(request); ServletRequestDataBinder binder = bindAndValidate(request, command); BindException errors = new BindException(binder.getBindingResult()); return processFormSubmission(request, response, command, errors); } catch (HttpSessionRequiredException ex) { // Cannot submit a session form if no form object is in the session. if (logger.isDebugEnabled()) { logger.debug("Invalid submit detected: " + ex.getMessage()); } return handleInvalidSubmit(request, response); } } else { // New form to show: render form view. return showNewForm(request, response); } } 這個方法,首先判斷是不是Form提交,判斷方法是: protected boolean isFormSubmission(HttpServletRequest request) { 如果是form提交的話,系統(tǒng)首先創(chuàng)建一個Command,然后對數(shù)據(jù)進(jìn)行綁定和驗證,之后調(diào)用processFormSubmission方法。showNewForm則調(diào)用showForm。
在AbstractFormController中里面有兩個抽象方法:
好了,看完AbstractFormController之后,再看看SimpleFormController是如何實現(xiàn): protected ModelAndView processFormSubmission( 在上面的方法中,如果有錯誤,調(diào)用showForm,來顯示form。沒有錯誤的話,則調(diào)用onSubmit方法。 protected final ModelAndView showForm( 在showForm中,設(shè)置屬性,放在model中,然后在viewName進(jìn)行設(shè)置。
FormController就是上面的過程。具體的執(zhí)行過程和詳細(xì)信息會在以后的博客中具體介紹。
posted @ 2009-11-25 17:31 追求無止境 閱讀(2450) | 評論 (0) | 編輯 收藏 Spring MVC 框架學(xué)習(xí)筆記之BaseCommandController和AbstractCommandController
Spring的BaseCommandController繼承自AbstractController。在看BaseCommandController之前先看他的繼承類AbstractCommandController是如何實現(xiàn) AbstractController的handleInternalRequest方法的: protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { Object command = getCommand(request); ServletRequestDataBinder binder = bindAndValidate(request, command); BindException errors = new BindException(binder.getBindingResult()); return handle(request, response, command, errors); } getCommand就是BaseCommandController中的方法。 protected Object getCommand(HttpServletRequest request) throws Exception { return createCommand(); } protected final Object createCommand() throws Exception { if (this.commandClass == null) { throw new IllegalStateException("Cannot create command without commandClass being set - " + "either set commandClass or (in a form controller) override formBackingObject"); } if (logger.isDebugEnabled()) { logger.debug("Creating new command of class [" + this.commandClass.getName() + "]"); } return BeanUtils.instantiateClass(this.commandClass); } createCommand創(chuàng)建了一個CommandClass的對象。 然后再看bindAndValidate方法: protected final ServletRequestDataBinder bindAndValidate(HttpServletRequest request, Object command) throws Exception { ServletRequestDataBinder binder = createBinder(request, command); BindException errors = new BindException(binder.getBindingResult()); if (!suppressBinding(request)) { binder.bind(request); onBind(request, command, errors); if (this.validators != null && isValidateOnBinding() && !suppressValidation(request, command, errors)) { for (int i = 0; i < this.validators.length; i++) { ValidationUtils.invokeValidator(this.validators[i], command, errors); } } onBindAndValidate(request, command, errors); } return binder; } 這個方法首先創(chuàng)建了 DataBinder對象,然后,獲取創(chuàng)建綁定對象時發(fā)生的錯誤。報錯在errors。接下來綁定對象,調(diào)用onBind處理綁定事件;接下來應(yīng)用Validator。然后調(diào)用onBindAndValidate來處理綁定和驗證事件。最后返回binder。 處理完之后調(diào)用handle方法進(jìn)行處理。 綜上所述,AbstractCommandController具有兩個功能: 1、將請求參數(shù)轉(zhuǎn)換為Command對象。在該Controller中,我們設(shè)置一個object對象。然后BaseCommandController將請求的參數(shù)進(jìn)行轉(zhuǎn)換。如果請求參數(shù)有value值,就會調(diào)用object的的setValue對象來設(shè)置對象里的值。如果請求參數(shù)中有address.city.就會調(diào)用object中g(shù)etAddress().setCity()方法來賦值。這個object可以是任意的object,唯一的要求就是這個object類沒有參數(shù)。 2、對數(shù)據(jù)進(jìn)行驗證。在轉(zhuǎn)換和驗證時發(fā)生錯誤時,需要在handle(request, response, command, errors)中進(jìn)行處理。 posted @ 2009-11-25 16:25 追求無止境 閱讀(5384) | 評論 (0) | 編輯 收藏 Spring的MVC web框架學(xué)習(xí)筆記1、Spring web 框架的核心:DispatcherServlet DispatcherServlet 用于接收請求。是使用Spring框架的入口。在web.xml中,需要配置該servlet。在配置該Servlet的時候url-pattern你可以使用你自己想使用的形式,如*.aspx,*.do,*.htm,*.action,用以混淆客戶端對服務(wù)器架構(gòu)的認(rèn)識。 另外,該Servlet在容器中還會加載一個APPlicationContext的xml文件。默認(rèn)加載的是[servlet-name]-servlet.xml。例如,你在web.xml中配置的servlet如下: <web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> </web-app> 該Servlet就會在服務(wù)器啟動時,加載example-servlet.xml。當(dāng)然,你也可以自己來指定加載文件。 要看看DispatcherServlet真面目,打開源文件,發(fā)現(xiàn)定義了很多BeanName的常量,如本地化解析器beanname,主題解析器beanname,視圖解析器beanname,上傳文件解析的multipart解析器beanname。 等: public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver"; public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver"; public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping"; public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter"; public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver"; public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator"; public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver"; 這個類怎么使用這些bean呢?以上面的exexample-servlet.xml為例,我們定義的名字為example的DispatcherServlet使用example-servlet.xml的配置。在example-servlet.xml里,我們可以配置名字上面的beanName的bean,來讓servlet加載這些bean。 這下明白了,servlet.xml該怎么配置,該配置什么了。 好了,看完了最重要的servlet的配置問題,我們再看下一個重要的接口:Controller。至于上面servlet要使用的什么什么解析器啦,我們稍后在分析。 2、Controller 我們的請求提交到DispacherServlet后,會轉(zhuǎn)給Controller。怎么找Controller?通過使用handlerMapping。如果沒有設(shè)置handlerMapping,spring使用默認(rèn)的BeanNameUrlHandlerMapping來找Controller。 BeanNameUrlHandlerMapping?顧名思義,就是通過bean的name屬性來映射controller。bean的name請求是一個url,如果請求的是logout.do,在example-servlet.xml中定義一個名字(name)為login.do的Controller. <bean name="/logout.do" class="com.jy.bookshop.web.spring.LogoutController"> 再插一句話,在handlerMapping中,我們可以使用請求攔截器來對請求進(jìn)行攔截處理。該攔截器怎么使用這里暫且不表,有機(jī)會再討論。 ok,現(xiàn)在我們創(chuàng)建一個LogoutController來讓他處理請求,讓他實現(xiàn)Controller吧: public class LogOutController implements Controller { public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { … return new ModelAndView(new RedirectView("login.do")); } } 看看這個Controller接口的定義,發(fā)現(xiàn)這個接口只定義了一個handleRequest方法。在這個方法中,返回一個ModelAndView。 先說ModelAndView。我們知道MVC,那么ModelAndView就是 MV了。Controller就是C。這樣MVC全了。呵呵。 繼續(xù)說ModelAndView,要了解他的結(jié)構(gòu),那自然要看看他的源代碼了: /** View instance or view name String */ private Object view; /** Model Map */ private ModelMap model;
只關(guān)注我們關(guān)注的,里面包含了一個View對象和model對象。model對象是一個Map,這里不再說了。關(guān)鍵看看view,奇怪,怎么是一個Object,太抽象了。再繼續(xù)看源代碼的話,會更加明白:
public ModelAndView(String viewName, String modelName, Object modelObject) { this.view = viewName; addObject(modelName, modelObject); } public void setViewName(String viewName) { this.view = viewName; } 原來這個view可以指向一個View對象,也可以指向String對象啊。View一個接口,如果看doc的話,他的實現(xiàn)類有AbstractExcelView, AbstractJasperReportsSingleFormatView, AbstractJasperReportsView, AbstractJExcelView, AbstractPdfStamperView, AbstractPdfView,AbstractTemplateView, AbstractUrlBasedView, AbstractView, AbstractXsltView, ConfigurableJasperReportsView, FreeMarkerView, InternalResourceView,JasperReportsCsvView, JasperReportsHtmlView, JasperReportsMultiFormatView, JasperReportsPdfView, JasperReportsXlsView, JstlView, RedirectView,TilesJstlView, TilesView, TilesView, VelocityLayoutView, VelocityToolboxView, VelocityView, XsltView(誠實的說,這些View是拷doc的)。 現(xiàn)在可以知道,我們的Controller返回一個View和Model,來讓Spring框架來創(chuàng)建一個回應(yīng)。 現(xiàn)在奇怪的還有一點,我吧view設(shè)置為字符串,Spring框架怎么處理?在ModelAndView中,如果view是一個字符串,則會將這個值交給DispatcherServlet的viewResovler來處理。記得上面提到的viewResovler了嗎?呵呵,派上用場了。 view的問題解決了,然后再說model吧。在ModelAndView添加了一些對象,Spring是怎么處理的呢?總應(yīng)該把這些對象給弄到request對象里,讓jsp頁面來使用吧。讓View使用?那么看看View接口吧: public interface View { String getContentType(); void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; } render函數(shù)需要帶一個model變量。再找找view的實現(xiàn)類,看看是怎么工作的。不過view有那么多,對于一些像什么pdf啦,excel了他們都不需要在request中添加這個model。 最終呢,我們在jstlView的父類InternalResourceView中的renderMergedOutputModel函數(shù)發(fā)現(xiàn)他把model放在了request里面了。 OK,現(xiàn)在我們明白了,controller返回的modelandview交給Servlet進(jìn)行處理,來生成一個頁面。 最簡單的Controller介紹完畢。現(xiàn)在看看Spring提供的一些controller的實現(xiàn),Spring提供了很多controller的實現(xiàn),繼承的結(jié)構(gòu)如下:
org.springframework.web.servlet.mvc.AbstractController (implements org.springframework.web.servlet.mvc.Controller)
AbstractController是Controller的第一個實現(xiàn)。其他的Controller都是繼承這個Controller的。我們先看比較重要的Controller。 先說UrlFilenameViewController。不如我們自己來部署一個吧。 在example-servlet.xml中增加如下的配置: <bean name="/error.do" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"> OK,當(dāng)/error.do的請求上來后,urlFileNameViewController將他變成/error。然后返回個View,這個view的name就是/error,然后使用viewresolver來進(jìn)行解析,可以解析成/jsp/error.jsp。 另外還有比較重要的controller是baseCommandController,將請求參數(shù)轉(zhuǎn)換為一個對象,并對對象參數(shù)合法性進(jìn)行驗證。另外,SimpleFormController可以對表單進(jìn)行處理。 關(guān)于各個controller的分析。未完待續(xù)。。
posted @ 2009-11-25 11:09 追求無止境 閱讀(3716) | 評論 (2) | 編輯 收藏 [轉(zhuǎn)] java.beans.PropertyEditor(屬性編輯器)簡單應(yīng)用原文:http://www.aygfsteel.com/orangewhy/archive/2007/06/26/126371.html
java.beans.PropertyEditor的從字義來看是一個屬性編輯器,但總覺得它的作用更像一個轉(zhuǎn)換器--從字符串轉(zhuǎn)換為類對象的屬性。 java.beans.PropertyEditor接口定義的方法有好幾個,但是最重要為下面兩個: void setValue(Object value) void setAsText(String text) throws java.lang.IllegalArgumentException; 一般地,我們要使用PropertyEditor時,并不直接實現(xiàn)此接口,而是通過繼承實現(xiàn)此接口的java.beans.PropertyEditorSupport來簡化我們的工作,在子類覆蓋setAsText方法就可以了,setValue方法一般不直接使用,在setAsText方法中將字符串進(jìn)行轉(zhuǎn)換并產(chǎn)生目標(biāo)對象以后,由調(diào)setAsText調(diào)用setValue來把目標(biāo)對象注入到編輯器中。當(dāng)然,你可用覆蓋更多的方法來滿足你的特殊要求。JavaBean的類和接口,被大部分spring包使用,可以從spring中學(xué)習(xí)更成熟的JavaBean使用方法。 簡單的例子: 實體類Person: ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
Person的屬性編輯器: ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
測試代碼: ![]() ![]() ![]() ![]() ![]() ![]()
結(jié)果輸出: ![]() posted @ 2009-11-20 22:07 追求無止境 閱讀(296) | 評論 (0) | 編輯 收藏 提高開發(fā)技術(shù)的Tip以下Tip和學(xué)習(xí)路線從自己身上出發(fā)進(jìn)行總結(jié),僅代表個人觀點。你可以留言進(jìn)行討論。 1.有計劃的學(xué)習(xí)學(xué)習(xí)是一個循序漸進(jìn)的過程。如果沒有一個計劃,學(xué)習(xí)將變得沒有規(guī)律,我們也無法提高自己的能力。想起上學(xué)的時候,學(xué)校每個學(xué)期都會制定一個教學(xué)大綱來指導(dǎo)老師的教學(xué)和我們的學(xué)習(xí)。是的,如果沒有計劃,今天突然想學(xué)這個,明天突然想學(xué)那個,朝三暮四,我們永遠(yuǎn)也無法學(xué)到自己想學(xué)的東西。所以我們需要制定一個學(xué)習(xí)計劃。有計劃的學(xué)習(xí)才能提高自己的能力。Java web項目的開發(fā)是需要很多知識的積累的,包括Java SE,數(shù)據(jù)庫,JDBC,Linux,Log4j,Html/CSS/Javascript,持久層框架,JUNIT及其他測試框架,IOC框架,web MVC框架等等,如果我們沒有一個良好的計劃,今天學(xué)習(xí)Log4j,明天學(xué)習(xí)Junit,這些東西都不會掌握好并學(xué)習(xí)好。 如果給自己做計劃。計劃可以按照時間段來進(jìn)行。例如本年度的工作,本季度要達(dá)到的水平,本月要學(xué)習(xí)的東西,本周學(xué)習(xí)的計劃安排,以及每一天的安排。每天晚上睡覺前,想想今天的計劃安排是否完成,明天該學(xué)習(xí)什么;每周到結(jié)束的時候,總結(jié)一下本周完成了什么,下周要學(xué)習(xí)什么。根據(jù)自己對計劃的實行情況可以改變自己的計劃。總之要有計劃的學(xué)習(xí)。可以使用google 日歷和 qq mail 郵箱等來管理自己的計劃。 2. 同一段時間只學(xué)習(xí)一種技術(shù)我是一個什么都想學(xué)的人。我不想把自己的時間都用在學(xué)習(xí)Java上,我還想學(xué)習(xí)C++,還想學(xué)習(xí) web 設(shè)計,還想學(xué)好windows編程,想學(xué)Linux編程,想學(xué)習(xí)計算機(jī)網(wǎng)絡(luò)編程,想學(xué)習(xí)路由器、網(wǎng)絡(luò)的配置……。于是,今天看了VC++深入詳解,明天學(xué)習(xí)Linux shell編程。計算機(jī)技術(shù)包含了太多技術(shù)。我們無法一一將他們都掌握。所以不要想什么都學(xué)會。至少在一段時間內(nèi)學(xué)習(xí)一種技術(shù)。我給自己制定了這樣的計劃,今年要把所有的精力都致力為 java EE 開發(fā)技術(shù)上。一年后,努力學(xué)習(xí)C/C++編程。 是的。我們學(xué)習(xí)的東西可以廣一點,但一定要有自己專的方面。學(xué)專了一個方面,你就可以接著學(xué)習(xí)其他的技術(shù)。一個什么都會的人,很可能什么都不會;所以,精于一,而博于廣。 3.學(xué)會休息我們都很忙,上學(xué)的時候?qū)W好各科,至少不能掛科,然后在課外學(xué)習(xí)自己喜歡的java 編程;工作的時候,需要做好工作,然后在工作之余多學(xué)一些東西;時間長了,我們就可能倦了,累了。所以我們需要學(xué)會休息來改變自己的精神狀態(tài)。 整天在電腦前進(jìn)行軟件開發(fā)的我們,要學(xué)會放松自己和休息。作為程序員整天在電腦前,極容易養(yǎng)成工作和休息都離不開電腦的習(xí)慣。休息的時候,也是在電腦前看電影,玩游戲。我想,在我們工作累了之后,應(yīng)該離開電腦,走向戶外來放松和休息。或到大街上轉(zhuǎn)轉(zhuǎn),或到商場里購物,或去游泳館游泳,或去健身房健身,或和朋友一起打臺球。等等等等。總之要學(xué)會放松自己,走出戶外,不要整天在電腦前。 以上3點是自己對自己工作學(xué)習(xí)的總結(jié)和提醒,特別寫出來和大家一起分享。
感謝HiMagic!分享自己的觀點。值得學(xué)習(xí)。
posted @ 2009-11-16 15:11 追求無止境 閱讀(1779) | 評論 (6) | 編輯 收藏 |
|||||||||||||||||||||||