Spring學(xué)習(xí)筆記系列(一)

          ApplicationContext ctx

          1,定義資源文件獲得資源文件的消息,國際化信息
          <bean id="messageResource" class="org.springFramework.context.support.ResourceBoundleMessageSource">
           <property name="basenames">
            xxxx
           </property>
          </bean>
          將會搜索xxxx.properties,xxxx_zh.properties ,xxxx_ch.properties等。

          2,
          程序里使用資源文件 :ctx.getMessage('key',arg);
          頁面上使用資源文件:<spring:message code="keyxxx"/>


          3,使用其他文件。
           Resource rs = ctx.getResource("classpath:config.properties");
           File file = rs.getFile();
           目錄規(guī)則:
           file:c:/test。txt
           /config.properties
           classpath:config.properties
           三種方式。

          4,事件傳播
            不過沒有找到ApplicationListener這個類。

          5, WebApp獲取ApplicaionContext的方法,
           首先是在web。xml中進(jìn)行配置,可以配置成一個listener,也可以配置成一個servlet。
           然后程序里使用WebApplicationContextUtils.getWebApplicationContext.獲得飲用。

          6,Spring可以和很多框架進(jìn)行集成。
           Struts+Spring,Webwork+Spring。

          7,Spring可以有自己的調(diào)度類,DispatherServlet。
           使用的配置文件就是beans的配置,名字可以隨便取。

          8,Sping配置文件(具體名字見web.xml中對ContextLoaderServlet的參數(shù))類似于Struts的配置文件。
           定義viewer使用的是系統(tǒng)類:
            org.springframework.web.servlet.view.InternalResourceViewResolver
           Request mapping 使用的系統(tǒng)類:
            org.springframework.web.servlet.handler.SimpleUrlHandleMapping
           對Action的定義使用自定義類,但這些類都是下面類的子類:
            SimpleFormController
           Actoin的屬性包括:
            1,跳轉(zhuǎn)的頁面,和view關(guān)聯(lián)
            2,封裝form類,就是一個普通的javabean。
            
          9,Acition類的內(nèi)容,
            實(shí)現(xiàn)onsubmit( 封裝的form,異常對象) 返回一個ModelAndView。
            ModelAndView用字符串初始化,字符串來自Action的兩個跳轉(zhuǎn)view名稱。
            還可以把Map傳入ModelAndView構(gòu)造函數(shù),用來返回消息。


          10,Spring自帶的數(shù)據(jù)驗(yàn)證功能。
           10.1,驗(yàn)證類作為Aciotn的一個名為“validator”的屬性在配置文件中配置。
           此類繼承org.springframework.validation.Validator
           需要實(shí)現(xiàn)兩個接口,support(傳入一個類),驗(yàn)證此類是否是Action對應(yīng)的form類。
           validator(object obj,Errors err )首先把obj轉(zhuǎn)為form類對象,如果有錯誤放到errors里,用法和
            struts類似。
           
           10.2,表現(xiàn)層頁面需要的顯示錯誤
            全部錯誤:
            <spring:bind path="command.*">
             //遍歷status.errorMessages
            </spring:bind>  
           
            單個錯誤:
            <spring:bind path="command.username">
             <input type="text" value="${staus.value}" name="${status.expression}">
             <c:if test="${status.error}">
              //遍歷status.errorMessages
             </c:if>
            </spring:bind>
            如果已經(jīng)在Action中配置了commandName,那么就不使用command了,而是使用配置的名字
            <bean id=‘loginActoni’>
             <property name="commandName">
              <value>RegisterInfo</value>
             </property>
            </bean>

          11, 異常處理
           在Dispather的配置文件中配置
           <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
           定義兩個屬性:
           <property name="defaultErrorView">
           <property name="exceptionMapping">
            <props>
             <prop key="java.sql.SQLException">sss</prop>
             <prop key="java.lang.RuntimeException">yyy</prop>
            </props>
           </property> 
           可以按不同異常映射到不同的頁面。
           異常頁面從request.getAttribute("Exception")取得Exception對象。顯示他的message屬性。

          12,國際化
           看完1后,補(bǔ)充如下:
           Spring判斷用戶Locale的方式有三種,request中取,session中取,cookie中取客戶端的locale。
           分別用三個類,配置到配置文件中。

          13,數(shù)據(jù)持久層(重要)對事務(wù)的封裝
           Spring是依賴容器的參數(shù)化事務(wù)管理不用寫代碼。
           見p67。
           1,在配置文件中配置完數(shù)據(jù)源,
           2,之后配置事務(wù)管理的bean,數(shù)據(jù)源是它的屬性。
           3,DAO,事務(wù)員是它的屬性。
           4,事務(wù)beanDAOProxy,事務(wù)策略,事務(wù)bean,DAO都是它的屬性。

          14,數(shù)據(jù)持久層,對JDBC的封裝
           org.springframework.jdbc.core.JdbcTemplate.
           JdbcTemplate jdbctemplate = new JdbcTemptlate( datasource );
           jdbctemplate.update("xxxxx");
           jdbctemplate.update("xxxxx",new PreparedStatementSetter(){
            public void setValues( PreparedStatementSetter ps ){
             ps.setInt(1,15);
             ps.setString(2,"jjjjjjj");
            }
           });
           jdbctemplate.query("select ...",new RollbackHandler(){
            public void processRow( ResultSet rs ){
             User user = new User();
             user.setName= rs.getString("name");
             userList.add( user );
            }
           });

           .call()可以調(diào)用存儲過程。
           query,update還有很多不同版本的實(shí)現(xiàn)。
           

          15, JDBC封裝還要引入事務(wù)管理機(jī)制,默認(rèn)是沒有事務(wù)的。
           兩種方式
           1,代碼控制的,在DAO里TransactionTemplate使用它的方法。
           2,參數(shù)化配置的事務(wù)。配置一個ProxyDAO,不用寫這個類,只需要在配置文件里增加他對DAO的事物設(shè)置。
            使得DAO代碼十分簡潔。

           測試代碼:
           InputStream is = new FileInputStream("xxx.xml");
           XmlBeanFactory factory = new XmlBeanFactoy( is );
           UserDAO dao = (UserDAO)factory.getBean("ProxyDAO");
           dao.insert();//這樣就行了

          16,Hibernate in Spring
           只需修改配置文件增加一個bean名字為sessionFactory,數(shù)據(jù)源作為他的屬性。
           TrsactionManager的屬性:sessionFactory
           IDAO接口:定義數(shù)據(jù)庫操作方法。
           DAO的屬性:sessionFactory。繼承HibernateDAOSupport,并實(shí)現(xiàn)IDAO接口。
             里面使用getHibernateTemplate模版進(jìn)行數(shù)據(jù)庫操作。
           ProxyDAO的屬性:transactionManager,DAO.
           
           測試?yán)樱?br> IUserDAO dao=(IUserDAO)factory.getBean("ProxyUserDAO"); 
           User user = new User();
           user.setxxx...
           dao.insertOrUpdate(user);
           

          posted on 2007-06-18 17:58 chenguo 閱讀(230) 評論(0)  編輯  收藏 所屬分類: Spring Dev

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿

          隨筆分類(1)

          文章分類(52)

          好友 小山的博客

          最新隨筆

          最新評論

          主站蜘蛛池模板: 高淳县| 铜山县| 宜章县| 宁蒗| 太白县| 从化市| 宜都市| 忻州市| 衡东县| 日喀则市| 彭泽县| 丰县| 嘉禾县| 孟津县| 子长县| 张家口市| 哈巴河县| 三门峡市| 竹山县| 尚志市| 新竹县| 化州市| 东明县| 缙云县| 福安市| 翁牛特旗| 永德县| 鄂伦春自治旗| 繁昌县| 晋宁县| 郓城县| 大姚县| 山西省| 临澧县| 淳化县| 宽城| 五台县| 多伦县| 翁牛特旗| 博兴县| 大余县|