水仁博客

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

          2008年6月7日

          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的入?yún)?br /> 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 中運(yùn)行,訪問下面連接,就可以運(yùn)行了。

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

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

          [專業(yè)]JS一些知識-0806081757


          JS的日期加減函數(shù):

          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);


          當(dāng)JS的正則表達(dá)式的pattern需要動態(tài)構(gòu)造時,需要使用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)編輯 收藏

          [專業(yè)]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()

          出現(xiàn)這個錯誤:
          xml.parsers.expat.ExpatError: unknown encoding:


          解決辦法:

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

          解1:利用UltraEdit等工具,將xml文件轉(zhuǎn)換成UTF-8的,然后encoding="utf-8"即可
          轉(zhuǎn)換工具如果沒有,用python可以簡單寫一個,比如
          (以下代碼轉(zhuǎn)自 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')
          將整個文件轉(zhuǎn)成utf-8的 String 來處理,處理結(jié)束后,利用
          unicode(string,'utf-8').encode('gb2312')
          換成本地的gb碼,再將結(jié)果寫回文件。

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


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

          [專業(yè)]代碼閱讀的經(jīng)驗-0806072109


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

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


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

          主站蜘蛛池模板: 大同市| 无为县| 陵水| 扎赉特旗| 大宁县| 穆棱市| 永济市| 江西省| 揭东县| 曲水县| 红原县| 榆林市| 岗巴县| 舟曲县| 玛纳斯县| 尖扎县| 北海市| 成都市| 潜山县| 化德县| 阿克苏市| 郴州市| 浮山县| 玉林市| 长岭县| 汽车| 吉安县| 合作市| 吉林省| 深圳市| 博野县| 石狮市| 江源县| 安陆市| 遵义县| 海兴县| 汾西县| 土默特左旗| 松潘县| 公安县| 莲花县|