水仁博客

          上善若水,仁恕載物
          隨筆 - 11, 文章 - 0, 評論 - 4, 引用 - 0
          數據加載中……

          2008年1月13日

          SpringMVC 2.5 的HelloWorld

          首先,寫一個Bean

          package springmvc.one.web;

          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.stereotype.Controller;
          import org.springframework.ui.Model;
          import org.springframework.ui.ModelMap;
          import org.springframework.validation.BindingResult;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestMethod;

          @Controller
          @RequestMapping("/hellOne.act")
          public class HelloOneAction { 

          @RequestMapping
          public String handleRequest(String user,Model model) { 
          System.out.println("用戶名:"+user); //GET/POST的入參
          model.addAttribute("user", user); //通過Session返回到界面的出參
          model.addAttribute("helloWord", "Hello");

          return "hellouser";

          }


          再寫一個JSP頁面hellouser.jsp,此頁面放在 WEB-INF/jsp 目錄下,代碼如下:

          <html> 
          <head><title>HelloPage</title></head> 
          <body> 
          Test this sample!
          <H1> ${helloWord}, ${user}!</H2> 
          </body>
          </html>


          接著看看web.xml的配置

          <?xml version="1.0" encoding="ISO-8859-1"?> 

          <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

          <description>Spring 2.5 App</description> 
          <display-name>Spring App Examples</display-name> 

          <servlet> 
          <servlet-name>annomvc</servlet-name> 
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>2</load-on-startup> 
          </servlet> 

          <servlet-mapping> 
          <servlet-name>annomvc</servlet-name> 
          <url-pattern>*.act</url-pattern> 
          </servlet-mapping> 

          </web-app>


          最后就是,annomvc.xml 文件了。

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

          <context:component-scan base-package="springmvc.one.web"/>

          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

          </beans>


          好了,見一個Tomcat工程,在 tomcat 中運行,訪問下面連接,就可以運行了。

          http://localhost:8080/TOMCAT-PROJECT/hellOne.act?user=gjhuai

          posted @ 2008-09-27 14:31 水仁圭 閱讀(3477) | 評論 (1)編輯 收藏

          [專業]JS一些知識-0806081757


          JS的日期加減函數:

          function dateAdd(date,dayNum){
          var a = date.valueOf();
          a = a + dayNum * 24 * 60 * 60 * 1000;
          a = new Date(a);
          return a;
          }


          JS把字符串裝載到DOM對象:
          var doc = new ActiveXObject("MSxml2.DOMDocument");
          doc.loadXML( xmlStr);


          當JS的正則表達式的pattern需要動態構造時,需要使用RegExp類:
          patt=new RegExp(today.replace(/\-/g,"\\-")+"((.|\n|\r|\t)+)"+yesterday.replace(/\-/g,"\\-"),"gm");
          //patt=new RegExp("2008\-5\-26((.|\n)+)2008\-5\-25","gm");
          h = h.replace(patt,yesterday);
          而不能使用/dd/gm之類簡單的pattern




          posted @ 2008-06-08 17:58 水仁圭 閱讀(232) | 評論 (0)編輯 收藏

          [專業]Python讀gbk編碼的xml問題-0806072220


          Python讀xml時,如果編碼不是utf-8或utf-16,就出錯,如下:


          ...

          解析這個xml文件代碼如下:
          from xml.dom import minidom
          f = minidom.parse('f:\\temp\\protocol.xml')
          print f.toxml()

          出現這個錯誤:
          xml.parsers.expat.ExpatError: unknown encoding:


          解決辦法:

          由于xml協會規定,所有xml解析器均需要支持utf-8和utf-16兩種編碼而不要求別的編碼,所以我估計python提供的xml處理模塊就是不支持gb2312的。而windows下的文件,大部分均為gb2312編碼的,因此處理的時候,就會帶來不方便的地方。

          解1:利用UltraEdit等工具,將xml文件轉換成UTF-8的,然后encoding="utf-8"即可
          轉換工具如果沒有,用python可以簡單寫一個,比如
          (以下代碼轉自 http://tenyears.cn/?cat=6 )
          ----------
          # -*- coding: mbcs -*-
          import codecs
          f = codecs.open(‘D:\\normal.txt’, ‘rb’, ‘mbcs’)
          text = f.read().encode(‘utf-8′)
          f.close
          f = open(‘d:\\utf8.txt’, ‘wb’)
          f.write(text)
          f.close()
          print text.decode(‘utf-8′).encode(‘gb2312′)
          -----------------

          解2:xml文件里面不要寫入encoding,保持為gb2312本地編碼,然后程序解析的時候,采用語句
          unicode(file('f:\\temp\\a.txt', 'r', 'gb2312').read(),'gb2312').encode('utf-8')
          將整個文件轉成utf-8的 String 來處理,處理結束后,利用
          unicode(string,'utf-8').encode('gb2312')
          換成本地的gb碼,再將結果寫回文件。

          另外,python2.4的普通函數處理字符串的時候,好像已經支持各種編碼了。


          posted @ 2008-06-07 22:22 水仁圭 閱讀(3169) | 評論 (0)編輯 收藏

          [專業]代碼閱讀的經驗-0806072109


          由于工作上的原因,我不得不看大量別人寫的代碼,這是一件很痛苦的事,尤其是看既少文檔注釋,又無良好命名和結構的代碼.

          有本書叫Code Reading,中文譯作代碼閱讀方法與實踐, 簡單瀏覽了一遍電子文檔, 感覺還是隔靴搔癢, 對提高代碼閱讀效率并無太大的幫助. 自己感覺還是以下方法有些幫助:
          1. 把對代碼閱讀的認識用筆或wiki記下來, 最好根據功能結構分類,可畫些輔助理解的框圖或思維導圖
          2. 利用UML工具反向生成些類圖,包圖, 還可自己動手畫一些流程圖,時序圖和協作圖
          3. 利用調試工具,通過設斷點,單步調試,設觀察哨等手段看看到底它是怎么運行的
          4. 寫一些簡單的測試程序,通過斷言,日志來驗證自己的判斷
          5. 如有可能,和代碼的原作者或其他維護者一起做Code Review


          posted @ 2008-06-07 21:11 水仁圭 閱讀(238) | 評論 (0)編輯 收藏

          Spring 集成測試

          8.3.1. Context管理和緩存


          Spring 中的包 spring-mock.jar 為集成測試提供了一流的支持。所有相關的API在包 org.springframework.test 中,它們不依賴于任何應用服務器或者其他部署環境。

          test包里的各種抽象類提供了如下的功能:
          • 各測試案例執行期間的Spring IoC容器緩存。
          • 測試fixture自身的依賴注入。
          • 適合集成測試的事務管理。
          • 繼承而來的對測試有用的各種實例變量。

          test包對加載的Context提供緩存,緩存功能是通過 AbstractDependencyInjectionSpringContextTests 類的一個方法(如下)實現的,此方法提供contexts xml的位置,且實現這個方法的類必須提供一個包含XML配置文件位置數組。缺省情況下,一旦加載后,這些配置將被所有的測試案例重用。

          protected abstract String[] getConfigLocations();

          當配置環境受到破壞,AbstractDependencyInjectionSpringContextTests 的 setDirty() 方法可以用來重新加載配置,并在執行下一個測試案例前重建application context。

          8.3.2. 測試fixture的依賴注入

          AbstractDependencyInjectionSpringContextTests 類將從getConfigLocations()方法指定的配置文件中自動查找你要測試的類, 并通過Setter注入該類的實例。

          簡單例子

          public class HibernateTitleDaoTests extends AbstractDependencyInjectionSpringContextTests {

          // 被測試類的實例將被(自動的)依賴注入
          private HibernateTitleDao titleDao;

          // 一個用來實現'titleDao'實例變量依賴注入的setter方法
          public void setTitleDao (HibernateTitleDao titleDao) {
          this.titleDao = titleDao;
          }

          public void testLoadTitle() throws Exception {
          Title title = this.titleDao.loadTitle(new Long10));
          assertNotNull(title);
          }

          //指定Spring配置文件加載這個fixture
          protected String[] getConfigLocations() {
          return new String[] { "classpath:com/foo/daos.xml" };
          }

          }

          getConfigLocations() 使用的是 根據類型的自動裝配(autowire byType)來注入的,所以如果你有多個bean都定義為一個類型,則對這些bean你不能用這個方法。在這種情況下你要使用 applicationContext 實例變量,并且使用 getBean() 來進行顯式查找。

          如果你的測試案例不使用依賴注入,只要不定義任何setters方法即可; 或者你可以繼承 org.springframework.test.AbstractSpringContextTests 類,它只包括用來加載Spring Context的便利方法,并且在測試fixture中不進行依賴注入。

          8.3.3. 事務管理

          測試對持久存儲的數據會有改動。類 AbstractTransactionalDataSourceSpringContextTests 在缺省情況下,對每一個測試案例,他們創建并且回滾一個事務。所以使用這個類就不用擔心數據被修改;它依賴于Application Context中定義的一個bean PlatformTransactionManager。

          如果你確實想在測試時修改數據,可以用這個類的 setComplete() 方法,這將提交而不是回滾事務。另外, endTransaction() 方法可以在測試結束前中止事務。

          8.3.4. 方便的變量

          AbstractDependencyInjectionSpringContextTests 類提供了兩個保護屬性性實例變量:

          • applicationContext (a ConfigurableApplicationContext): 可以利用它進行顯式bean查找,或者作為一個整體來測試這個Context的狀態。
          • jdbcTemplate : 對確定數據狀態的查詢很有用。

          8.3.5. 示例

          Spring的PetClinic實例展示了這些測試超類的用法

          public abstract class AbstractClinicTests extends AbstractTransactionalDataSourceSpringContextTests {

          protected Clinic clinic;

          public void setClinic(Clinic clinic) {
          this.clinic = clinic;
          }

          public void testGetVets() {
          Collection vets = this.clinic.getVets();
          assertEquals('JDBC query must show the same number of vets',
          jdbcTemplate.queryForInt('SELECT COUNT(0) FROM VETS'),
          vets.size());
          Vet v1 = (Vet) EntityUtils.getById(vets, Vet.class, 2);
          assertEquals('Leary', v1.getLastName());
          assertEquals(1, v1.getNrOfSpecialties());
          assertEquals('radiology', ((Specialty) v1.getSpecialties().get(0)).getName());
          Vet v2 = (Vet) EntityUtils.getById(vets, Vet.class, 3);
          assertEquals('Douglas', v2.getLastName());
          assertEquals(2, v2.getNrOfSpecialties());
          assertEquals('dentistry', ((Specialty) v2.getSpecialties().get(0)).getName());
          assertEquals('surgery', ((Specialty) v2.getSpecialties().get(1)).getName());
          }

          JdbcTemplate 變量用于驗證被測試的代碼是否正確。JdbcTemplate 讓測試更嚴密,且減少了對測試數據的依賴。例如,可以在不中止測試的情況下在數據庫里增加額外的數據行。

          PetClinic應用支持四種數據訪問技術--JDBC、Hibernate、TopLink和JPA ,因此 AbstractClinicTests 類不指定Context位置,這個操作在子類中實現:

          例如,用Hibernate實現的PetClinic測試包含如下方法:

          public class HibernateClinicTests extends AbstractClinicTests {

          protected String[] getConfigLocations() {
          return new String[] {
          '/org/springframework/samples/petclinic/hibernate/applicationContext-hibernate.xml'
          };
          }
          }

          8.3.6. 運行集成測試

          集成測試比單元測試更依賴于測試環境,它是測試的一個補充,而不是代替單元測試的。這種依賴主要指完整數據模型的數據庫。也可以通過DbUnit或者數據庫工具來導入測試數據。

          posted @ 2008-01-13 21:57 水仁圭 閱讀(1450) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 稷山县| 金华市| 双城市| 隆昌县| 古田县| 静乐县| 山阳县| 哈尔滨市| 宕昌县| 唐河县| 柞水县| 武安市| 长葛市| 孝昌县| 江城| 新竹县| 乌拉特中旗| 来宾市| 泰和县| 克山县| 饶河县| 城固县| 内江市| 甘孜县| 通化县| 略阳县| 德州市| 忻州市| 绥阳县| 达拉特旗| 大新县| 清徐县| 保康县| 怀安县| 寿光市| 化州市| 巴塘县| 昌吉市| 门头沟区| 崇明县| 长海县|