Loading...

          java .net

          前兩天初步認(rèn)識(shí)了一下struts2
          今兒看來教程的第二三講,搞清了一些前面的一些猜測或是疑問
          1、struts2是不用<html:...>標(biāo)簽了,統(tǒng)一成了<s:...>
          如下這樣:
          <s:form action="Converter">
          <s:textfield name="point" label="Point"></s:textfield>
          <s:submit label="submit"></s:submit>
          </s:form>
          顯示效果:

          注意到<s:textfield name="point" label="Point"></s:textfield>
          中的label屬性,它指定了文本框前面顯示的內(nèi)容,還自動(dòng)加了冒號(hào),哈哈,挺聰明的嘛,但是擔(dān)心這樣在復(fù)雜的頁面設(shè)計(jì)中是不是好使。

          哦對(duì)了,要想這么寫,要在頁面上方加上這個(gè):<%@ taglib prefix="s" uri="/struts-tags" %>

          2、Action
          前面說的Action不再需要繼承任何struts類,現(xiàn)在看來要失望了,為了方便起見還是建議集成ActionSupport類,目前覺得有用的是ActionSupport中定義了幾個(gè)static的result name,比如SUCCESS、ERROR,原來的return "success";現(xiàn)在可以寫成return super.SUCCESS;,將標(biāo)識(shí)用的字符串常量定義成static的是一直提倡的,還有就是validate()方法,驗(yàn)證有錯(cuò)誤可以調(diào)用addFieldError()方法,好像就是struts1 ActionForm里的東西,有所改進(jìn)的是super.addFieldError("username", "username is null");將在頁面中顯示的效果為:錯(cuò)誤信息"username is null"將在名字為"username"文本框的上面顯示,這些如果能用ajax實(shí)現(xiàn)就好了。
          對(duì)于Action解耦,可能在于它不再需要HttpServletRequest 、HttpServletResponse這樣的容器運(yùn)行時(shí)參數(shù)吧

          Powered by Zoundry Raven

          posted @ 2009-01-05 10:47 豬 閱讀(305) | 評(píng)論 (0)編輯 收藏
          今天第一次感覺到經(jīng)濟(jì)危機(jī)在我身邊了,部門現(xiàn)在沒有在做的項(xiàng)目了
          經(jīng)濟(jì)危機(jī)中,趕緊為自己充充電,好到時(shí)候柳暗花明又一村,哈哈
          學(xué)struts2
          據(jù)說struts2基于webwork,基本上跟struts1沒啥關(guān)系,如果有webwork的經(jīng)驗(yàn)上手會(huì)很迅速
          我沒接觸過webwork,就知道有這么個(gè)東西
          今兒開始第一個(gè)struts
          見過好多blog寫有struts2的入門步驟,俺也寫一個(gè),為自己造個(gè)輪子,加深印象。
          首先下載struts2的jar包,到http://struts.apache.org/,右上角有個(gè)struts2的鏈接,今天下到的是
          struts2的2.0.14,昨天在javaeye上看到發(fā)布Struts2.1.3 發(fā)布了,只是主頁還沒看到,不止一次聽大拿們說過不要追求新版本,哈哈
          下載后的目錄:app--struts的例子
                        docs-doc文檔
                        lib-struts的jar包或依賴包
                        src-源碼
          HelloWorld:

          1、index.jsp
            耳目一新的是,不需要用到struts html標(biāo)簽,這只是猜測,或許例子過于簡單?今天工作中還感覺struts1的html標(biāo)簽真是不好用,想加個(gè)class、maxlength、size都不好使,讓我很是郁悶。希望在繼續(xù)學(xué)習(xí)中真的能耳目一新。
          struts的action慣例后綴名改成了.action,不再像struts1的.do了,說是延續(xù)到webwork的慣例。
          下面的頁面代碼submit的時(shí)候?qū)⑻峤坏絣ogin.action
          index.jsp
          <body>
            <form action="login.action" method="post">
                username:<input type="text" name="username"/>
                password:<input type="password" name="password"/>
                <input type="submit" value="submit"/>
            </form>
          </body>
          2、Action類
          struts2的Action可是大進(jìn)步,不用再繼承任何類,實(shí)現(xiàn)了松耦合,它好像將struts1的ActionForm融合了進(jìn)來,據(jù)說struts2不再用ActionForm,頁面上對(duì)應(yīng)的字段寫在了Action中,struts2框架會(huì)自動(dòng)調(diào)用get/set方法,在我印象里struts1中的Action對(duì)象不是線程安全的,會(huì)在不同線程間重用,所以謹(jǐn)慎在里面定義字段,在這看來struts2的Action不是這樣的,只是猜測
          package com.mystart.action;

          public class LoginAction {

              private String username;
              private String password;
            
              public String getUsername() {
                  return username;
              }
              public void setUsername(String username) {
                  this.username = username;
              }
              public String getPassword() {
                  return password;
              }
              public void setPassword(String password) {
                  this.password = password;
              }
            
              public String execute() throws Exception{
                  return "success";
              }
          }

          3、jsp、java類都定義了,現(xiàn)在要它們聯(lián)系起來-struts配置文件
          新的struts配置文件有個(gè)package包的概念,還沒鬧明白這個(gè)package的詳細(xì)用法,有待繼續(xù)深入
          <action>標(biāo)簽有變化,type變成了class,path變成了name,struts1中name屬性是制定ActionForm的,現(xiàn)在ActionForm沒有了
          forward變成了result,result的默認(rèn)name=success

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd">


          <struts>
              <package name="struts" extends="struts-default">
                  <action name="login" class="com.mystart.action.LoginAction">
                      <result name="success">/result.jsp</result>
                  </action>
              </package>

          </struts>

          4、最后啟動(dòng)struts2,配置web.xml
          struts1的web.xml配置是放在<servlet>中,也就是是一個(gè)servlet
          struts2變成了一個(gè)過濾器Filter
          struts1中<url-pattern>被配置成攔截.do的鏈接
          struts2變成了攔截所有鏈接 /*

          <?xml version="1.0" encoding="UTF-8"?>
          <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">
            
            <filter>
                <filter-name>struts2</filter-name>
                <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
            </filter>
            <filter-mapping>
                <filter-name>struts2</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
            <welcome-file-list>
              <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>
          </web-app>

          以上是驢了個(gè)struts的視頻教程,今兒看了第一節(jié)課,看完后做了一下總結(jié),里面有一些自己的理解,有不對(duì)的地方請兄弟們指正,別光光說我是豬,豬也要進(jìn)步啊,嘿嘿,每一步都有疑問,明天帶著問題接著看下一節(jié),睡覺去。
          posted @ 2008-12-29 23:06 豬 閱讀(2245) | 評(píng)論 (6)編輯 收藏
          自定義Annotation
          早就知道jdk5加了新特性Annotation,但是沒研究過,前幾天公司培訓(xùn),有一部分是介紹jdk5新特性的,一個(gè)是注解一個(gè)泛型
          今兒復(fù)習(xí)一下注解
              //用@Deprecated聲明該方法不建議使用
              @Deprecated public void doSomething1(){
                  Map map = new HashMap();
                  map.put("some", "thing");
                  System.out.println(map);
              }
             
              //用@SuppressWarnings聲明不再進(jìn)行類型檢查
              @SuppressWarnings(value={"unchecked"})
              public void doSomething2(){
                  Map map = new HashMap();
                  map.put("some", "thing");
              }


          寫一個(gè)自定義注解先
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          //要在運(yùn)行時(shí)使用這個(gè)注解,必須聲明成RUNTIME
          Annotation分為三種級(jí)別:RUNTIME、CLASS、SOURCE
          @Retention(RetentionPolicy.RUNTIME)
          public @interface SomeAnnotation{
              String value();
              String name();
          }

          下面來使用這個(gè)自定義注解:
          import java.lang.reflect.Method;
          public class AnnotationTest {
             
              @SomeAnnotation(value="value1",name="name1")
              public void doSomething3(){
                 
              }
             
              public static void main(String[] args){
             
                  Class<AnnotationTest> c = AnnotationTest.class;
                  try {
                      //利用反射得到方法doSomething3
                      Method method = c.getMethod("doSomething3");
                      //查找doSomething3方法是否有SomeAnnotation的Annotation
                      if(method.isAnnotationPresent(SomeAnnotation.class)){
                          System.out.println("找到SomeAnnotation");
                          //得到SomeAnnotation
                          SomeAnnotation annotation = method.getAnnotation(SomeAnnotation.class);
                          System.out.println("annotation.value="+annotation.value());
                          System.out.println("annotation.name="+annotation.name());
                      }else{
                          System.out.println("沒有找到omeAnnotation");
                      }
                  } catch (SecurityException e) {
                      e.printStackTrace();
                  } catch (NoSuchMethodException e) {
                      e.printStackTrace();
                  }
              }
          }

          輸出結(jié)果:
          找到SomeAnnotation
          annotation.value=value1
          annotation.name=name1
          posted @ 2008-12-28 16:47 豬 閱讀(3841) | 評(píng)論 (6)編輯 收藏

          遇到一個(gè)郁悶的問題 ,百思不得其解
          到這里請大家分析分析

          outtable 表中有四條記錄 如下圖

          我按out_scrpno排序,為什么2008-1000不在第一行呢???

          同樣的問題,如下圖

          為什么會(huì)是2008-999呢 為啥不是2008-1000???

          請大家?guī)兔κ巧蹲釉颍嘀x,多謝


          Powered by Zoundry Raven

          ps:問題基本解決,感謝各位提示:

          SELECT TOP 1 OUT_SCRPNO FROM OUTTABLE WHERE CHARINDEX('-',OUT_SCRPNO,6) = 0 ORDER BY CONVERT(int,REPLACE(out_scrpno,'-','')) DESC 另好像不該發(fā)到首頁,請管理員包含,心切
          posted @ 2008-12-19 22:00 豬 閱讀(1139) | 評(píng)論 (7)編輯 收藏

          因?yàn)橐薷囊粋€(gè)以前的老項(xiàng)目,老項(xiàng)目用的jdk是1.4版本,遂在項(xiàng)目右鍵Properties-Java Compiler中將Compiler compliance level 設(shè)成了1.4

          以為萬事大吉了呢,昨晚上因?yàn)镮nteger的一個(gè)方法發(fā)現(xiàn)了問題

          Integer中有個(gè)方法valueOf

          static Integer valueOf(int i)
          返回一個(gè)表示指定的 int 值的 Integer 實(shí)例。
          static Integer valueOf(String s)
          返回保持指定的 String 的值的 Integer 對(duì)象。
          static Integer valueOf(String s, int radix)
          返回一個(gè) Integer 對(duì)象,該對(duì)象中保持了用第二個(gè)參數(shù)提供的基數(shù)進(jìn)行分析時(shí)從指定的 String 中提取的值。

          其中valueOf(int i)

          從以下版本開始:
          1.5

          也就是在1.5之前沒有這個(gè)方法,但是在eclipse中卻有這個(gè)方法的提示

          找了半天,原來問題出在這,在Java Build Path 中Libraries 中jdk是1.5的,把它remove掉,添加一個(gè)1.4的就OK了

          還是功力不夠啊 充電 充電ing

          posted @ 2008-12-04 15:02 豬 閱讀(4435) | 評(píng)論 (1)編輯 收藏

          下了個(gè)Hibernate視頻教程聽,雖然一年多以前跟老師學(xué)過Hibernate,但現(xiàn)在聽聽還是有很多收獲的,發(fā)現(xiàn)自己的知識(shí)知道的都是些皮毛,用了很久的東西,知道怎么操作怎么用,但要說說它的所以然,搖搖頭,呵呵

          根據(jù)主鍵Id得到一個(gè)持久對(duì)象,Hibernate中有兩個(gè)方法,一個(gè)get,一個(gè)load,他們兩個(gè)參數(shù)相同,都返回一個(gè)Object

          它們的區(qū)別:
          執(zhí)行g(shù)et方法即時(shí)生成查詢sql去查詢數(shù)據(jù)庫得到相應(yīng)的pojo,如果數(shù)據(jù)庫中沒有相應(yīng)記錄,則返回null
          執(zhí)行l(wèi)oad方法不會(huì)即時(shí)產(chǎn)生sql語句,而是在用到返回的對(duì)象時(shí)采取查詢數(shù)據(jù)庫,也就是load方法有默認(rèn)的延遲加載,在執(zhí)行l(wèi)oad方法后返回的不是一個(gè)pojo對(duì)象,是pojo對(duì)象的一個(gè)代理(據(jù)說Hibernate是用代理的方式實(shí)現(xiàn)延遲加載的,這塊還迷糊),如果數(shù)據(jù)庫中沒有相應(yīng)記錄,load方法會(huì)拋出異常ObjectNotFoundException
          看了一下我們用MyEclipse hibernate工具通過數(shù)據(jù)庫生成的DAO類,它的findById方法是用的session.get()方法,這是即時(shí)獲得pojo對(duì)象,如果是load方法,在執(zhí)行完load后如果關(guān)閉了session,那在接下來用到這個(gè)pojo對(duì)象時(shí)恐怕會(huì)報(bào)session已關(guān)閉的錯(cuò)誤。
          還有就是這兩個(gè)方法的第二個(gè)參數(shù)id,它必須是實(shí)現(xiàn)了java.io.Serializable接口,也就是可序列化的。

          今天好像是立冬,冬天到了,抓緊時(shí)間充充電,明天接著學(xué)...

          posted @ 2008-11-07 10:15 豬 閱讀(1991) | 評(píng)論 (2)編輯 收藏

          學(xué)習(xí)Linux,裝了個(gè)VMware,在上面裝了個(gè)紅帽4
          前輩建議用host-only連接方式,好處是與host建立一個(gè)私有的網(wǎng)絡(luò),跟外界沒有關(guān)系,A private network shared with the host
          在這種方式下Linux如何上網(wǎng)呢

          1、首先,安裝VMware后windows下會(huì)多出兩塊虛擬網(wǎng)卡,VMware Network Adapter VMnet1(為host-only方式用)和VMware Network Adapter VMnet8(為NAT方式用),將這兩塊網(wǎng)卡的IP都設(shè)為自動(dòng)獲取。

          2、將window上自己的網(wǎng)卡設(shè)為共享

          3、進(jìn)入Linux,應(yīng)用程序-系統(tǒng)設(shè)置-網(wǎng)絡(luò)

          選中eth0,點(diǎn)編輯,做如下配置

          完成后點(diǎn)確定,然后點(diǎn)擊激活,OK,打開firefox試試看

          posted @ 2008-11-06 15:19 豬 閱讀(925) | 評(píng)論 (0)編輯 收藏

          下了個(gè)Linux的視頻教程

          學(xué)了幾個(gè)命令記錄下來

          其中對(duì)于Linux名字的解釋挺有意思,Linux is not unix,:-D

          Linux中認(rèn)為所有的硬件,所有的設(shè)備都是文件,文件分為字符文件,和塊設(shè)備(就是二進(jìn)制的),它將所有的設(shè)備都放在dev目錄中

          cd / 訪問根目錄
          ls 顯示目錄中的文件列表
          cd dev 訪問dev目錄
          mkdir my 創(chuàng)建目錄my
          rmdir my 刪除目錄my
          mount /dev/cdrom /mnt/my 將cdrom掛載到my目錄
          nmount /dev/cdrom 解除掛載
          whoami  顯示當(dāng)前用戶
          pwd     顯示當(dāng)前所在目錄

          posted @ 2008-11-05 17:00 豬 閱讀(229) | 評(píng)論 (0)編輯 收藏
               摘要: 原文:http://bbs.80nian.net/thread-428-1-1.html 百度的popup.js這個(gè)文件中的彈出層方法挺好用的,但是,有些時(shí)候,發(fā)現(xiàn)在Mozilla Firefox瀏覽器下彈出層不能正常使用,具體表現(xiàn)為:層不能移動(dòng),一起停在頁面左下角,遮罩層不能完全遮罩頁面。     解決方案:刪除被調(diào)用頁面中的“<!DOCTY...  閱讀全文
          posted @ 2008-10-28 13:20 豬 閱讀(1087) | 評(píng)論 (0)編輯 收藏
          //將給定日期增加NumDay個(gè)月
              function addDate(dtDate,NumDay){
                  var date = new Date(dtDate);
                  var lIntval = parseInt(NumDay);
                  date.setMonth(date.getMonth() + lIntval);
                  return date.getYear() +'-' + (date.getMonth()+1) + '-' +date.getDate();
              }

          addDate("2008-01-01".replace(/-/g, "\/"),2);


          =======================================
          // addDate("5",5,"2004/12/1 00:00:00")
          function addDate(type,NumDay,dtDate){
             var date = new Date(dtDate)
          type = parseInt(type) //類型
          lIntval = parseInt(NumDay)//間隔
          switch(type){
             case 6 ://年
          date.setYear(date.getYear() + lIntval)
          break;
          case 7 ://季度
          date.setMonth(date.getMonth() + (lIntval * 3) )
          break;
          case 5 ://月
          date.setMonth(date.getMonth() + lIntval)
          break;
          case 4 ://天
          date.setDate(date.getDate() + lIntval)
          break
          case 3 ://時(shí)
          date.setHours(date.getHours() + lIntval)
          break
          case 2 ://分
          date.setMinutes(date.getMinutes() + lIntval)
          break
          case 1 ://秒
          date.setSeconds(date.getSeconds() + lIntval)
          break;
          default:
             
          }
          return date.getYear() +'-' + (date.getMonth()+1) + '-' +date.getDate()+ ' '+ date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()
          }
          posted @ 2008-10-26 18:37 豬 閱讀(1254) | 評(píng)論 (0)編輯 收藏
          DWR不能識(shí)別以
          Class c = Class.forName(ClassName);
          方式產(chǎn)生的對(duì)象,
          它被是別為java.lang.Class
          posted @ 2008-10-23 17:34 豬 閱讀(615) | 評(píng)論 (0)編輯 收藏
          隨機(jī)快速排序算法:
          還沒怎么整明白,有點(diǎn)暈
          Java語言:
          import java.util.*;
          public class Test {


              int[] x = {3,7,5,6,4,9,8,1};
              int comps = 0;
              void quicksort(int l, int u)
              {
                  int i, m;
                  if (l >= u) return;
                  swap(l, getRandom(l, u));
                  m = l;
                 
                  comps += u - 1;
                  for (i = l+1; i <= u; i++){
                      //comps++;
                      if (x[i] < x[l])
                          swap(++m, i);
                  }
                  swap(l, m);
                  quicksort(l, m-1);
                  quicksort(m+1, u);
              }
             
              void swap(int a,int b){
                  int temp = x[a];
                  x[a] = x[b];
                  x[b] = temp;
              }
             
              int getRandom(int min,int max){ 
                    return (int)(Math.random()*(max-min+1)) + min;
                    //Math.round(Math.random()*(Max-Min)+Min);
              }
             
              public static void main(String[] args) {
                  Test t = new Test();
                  System.out.println(Arrays.toString(t.x));
                  t.quicksort(0,t.x.length - 1);
                  System.out.println(t.comps);
                  System.out.println(Arrays.toString(t.x));
              }
             

          }


          posted @ 2008-10-23 17:24 豬 閱讀(1741) | 評(píng)論 (4)編輯 收藏

          好久沒寫了
          中間過了個(gè)十一,在家混沌過了好幾天

          回來轉(zhuǎn)眼上了一星期班了,忙的屁滾尿流
          一年前的系統(tǒng)要增加兩個(gè)大功能,200多個(gè)報(bào)表要挨個(gè)修改,報(bào)表校驗(yàn)的頁面效果客戶又提出了新建議,一個(gè)字 改

          從昨天晚上開始搗鼓到現(xiàn)在終于解決了一個(gè)問題,心情好了些,上來寫寫,哈哈

          這兩天用了baidu 百度空間中的彈出窗口js,感覺不錯(cuò),很強(qiáng)大,很好很簡單的解決了好幾個(gè)問題,界面友好度以及美化也好多了,以前都是硬邦邦window.open();

          有興趣的朋友搜索"百度 popup"就好了,已經(jīng)有人給出了注釋,強(qiáng)大。

          最有意思的是用javascript獲取和設(shè)置style

          DOM標(biāo)準(zhǔn)引入了覆蓋樣式表的概念,當(dāng)我們用document.getElementById("id").style.backgroundColor 獲取樣式時(shí) 獲取的只是id中style屬性中設(shè)置的背景色,如果id中的style屬性中沒有設(shè)置background-color那么就會(huì)返回空,也就是說如果id用class屬性引用了一個(gè)外部樣式表,在這個(gè)外部樣式表中設(shè)置的背景色,那么不好意思document.getElementById("id").style.backgroundColor 這種寫法不好使,如果要獲取外部樣式表中的設(shè)置,需要用到window對(duì)象的getComputedStyle()方法,代碼這樣寫window.getComputedStyle(id,null).backgroundColor
          但是兼容問題又來了,這么寫在firefox中好使,但在IE中不好使
          兩者兼容的方式寫成
          window.getComputedStyle?window.getComputedStyle(id,null).backgroundColor:id.currentStyle["backgroundColor"];
          如果是獲取背景色,這種方法在firefox和IE中的返回值還是不一樣的,IE中是返回"#ffff99"樣子的,而firefox中返回"rgb(238, 44, 34) "
          值得注意的是:window.getComputedStyle(id,null)這種方式不能設(shè)置樣式,只能獲取,要設(shè)置還得寫成類似這樣id.style.background="#EE2C21";

          參考:
          JavaScript權(quán)威指南
          http://bokee.shinylife.net/blog/article.asp?id=817
          http://book.csdn.net/bookfiles/679/10067921329.shtml

          posted @ 2008-10-10 14:49 豬 閱讀(4351) | 評(píng)論 (1)編輯 收藏

          早上,
          上班,
          公司樓下等電梯,
          旁邊站一男子,
          微微發(fā)福,
          個(gè)不高,
          遂俯視,
          手拿車鑰匙(大大的那種帶遙控的那種明顯汽車鑰匙),
          另一手拿一大塊手機(jī)(智能的手寫的那種),擺弄著
          肩背筆記本,
          嶄新嶄新的,
          頓生羨慕,
          羨慕ing,
          往上看,
          面色紅潤,
          一看就是吃了早飯了,
          再往上,
          短發(fā),
          1/3白色,
          遂心想:嗯,等我也這些白頭發(fā)了,
          我也背這些裝備,
          呵呵,咧嘴......

          posted @ 2008-09-25 19:20 豬 閱讀(1243) | 評(píng)論 (4)編輯 收藏

          今天發(fā)現(xiàn)一個(gè)好東西

          Url Rewrite Filter

          它可以實(shí)現(xiàn)url重寫,從而隱藏實(shí)際的url,同時(shí)使url看起來更美觀,簡單

          最令人興奮的是它一下解決了一值在尋找的blog用戶訪問自己的空間的問題

          比如http://hi.baidu.com/liuspring 就顯示我的空間

          1、下載Url Rewrite Filter

          2、在項(xiàng)目的web.xml配置過濾器

          XML語言:
          <filter>
          <filter-name>UrlRewriteFilter</filter-name>
          <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
          <init-param>
          <param-name>logLevel</param-name>
          <param-value>debug</param-value>
          </init-param>
          </filter>
          <filter-mapping>
          <filter-name>UrlRewriteFilter</filter-name>
          <url-pattern>/*</url-pattern>
          </filter-mapping>


          3、將urlrewrite-2.6.0.jar放入lib文件夾
          4、新建urlrewrite.xml文件置于WEB-INF目錄
          5、配置urlrewrite.xml

          XML語言: 臨時(shí)自用代碼@代碼發(fā)芽網(wǎng)
          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
          "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

          <!--

          Configuration file for UrlRewriteFilter
          http://tuckey.org/urlrewrite/

          -->
          <urlrewrite>
          <rule>
          <from>^/([a-z]+)/?$</from>
          <to type= "forward" >/blogView.do?go=$1</to>
          </rule>

          <rule>
          <note> 這是一個(gè)通用請求url rewrite</note>
          <from>^/([a-z0-9A-Z_]+)/([a-z0-9A-Z_]+)/?$</from>
          <to type= "forward" >/$2.do?go=$1</to>
          </rule>


          <outbound-rule>
          <note>
          The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
          the url /rewrite-status will be rewritten to /test/status/.

          The above rule and this outbound-rule means that end users should never see the
          url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
          in your pages.
          </note>
          <from>/rewrite-status</from>
          <to>/test/status/</to>
          </outbound-rule>

          </urlrewrite>




          url匹配使用正則表達(dá)式的規(guī)則,
          實(shí)驗(yàn)中發(fā)現(xiàn)一個(gè)問題,就是必須把里面的正則表達(dá)式用小括號(hào)括起來,在正則表達(dá)式中叫分組
          不然會(huì)報(bào)異常:
          java.lang.IndexOutOfBoundsException: No group 2
          哈哈,前幾日還費(fèi)勁的自己寫Servlet重寫url呢,原來這有現(xiàn)成的,更加覺得自己現(xiàn)在的水平遇到的問題網(wǎng)上的前輩們早都遇到過了,一定要站在巨人的肩膀上,少走彎路啊。


          把我的servlet貼在這,呵呵,參考自blojsom

          Java語言: 臨時(shí)自用代碼@代碼發(fā)芽網(wǎng)
          package com.capinfo.servlet;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          import com.capinfo.util.PageConstraint;
          import com.capinfo.util.PigBlogUtil;


          import javax.servlet.ServletConfig;
          import javax.servlet.ServletException;
          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import java.io.IOException;


          /**
          *
          * @author Administrator
          *
          */
          public class PigBlogServlet extends HttpServlet {

          protected Log _logger = LogFactory.getLog(PigBlogServlet.class);



          /**
          * Initialize
          *
          * @param servletConfig {@link ServletConfig}
          * @throws ServletException If there is an error initializing
          */
          public void init(ServletConfig servletConfig) throws ServletException {
          super.init(servletConfig);
          }

          /**
          * Handle requests made to
          *
          * @param httpServletRequest {@link HttpServletRequest} request
          * @param httpServletResponse {@link HttpServletResponse} response
          * @throws ServletException If there is an error serving the request
          * @throws IOException If there is an error serving the request
          */
          protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {


          // Make sure that we have a request URI ending with a / otherwise we need to
          // redirect so that the browser can handle relative link generation
          // if (!httpServletRequest.getRequestURI().endsWith("/")) {
          // StringBuffer redirectURL = new StringBuffer();
          // redirectURL.append(httpServletRequest.getRequestURI());
          // redirectURL.append("/");
          // if (httpServletRequest.getParameterMap().size() > 0) {
          // redirectURL.append("?");
          // redirectURL.append(PigBlogUtil.convertRequestParams(httpServletRequest));
          // }
          //
          // if (_logger.isDebugEnabled()) {
          // _logger.debug("Redirecting the user to: " + redirectURL.toString());
          // }
          //
          // httpServletResponse.sendRedirect(redirectURL.toString());
          //
          // return;
          // }



          // Check for an overriding id
          String blogId = httpServletRequest.getParameter(PageConstraint.GO);
          if (PigBlogUtil.checkNullOrBlank(blogId)) {
          String blogIdFromPath = PigBlogUtil.getBlogFromPath(httpServletRequest.getPathInfo());
          if (blogIdFromPath == null) {
          blogId = PageConstraint.GO1;
          } else {
          blogId = blogIdFromPath;
          }
          }

          if (PigBlogUtil.checkNullOrBlank(blogId)) {
          httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "Blog ID not specified");

          return;
          }
          StringBuffer redirectURL = new StringBuffer();

          //redirectURL.append(httpServletRequest.getContextPath());
          System.out.println(httpServletRequest.getRequestURI());
          if(httpServletRequest.getRequestURI().indexOf("/blog/") > -1 && httpServletRequest.getRequestURI().indexOf(".jsp") == -1 ){
          if(!httpServletRequest.getRequestURI().endsWith("/") && httpServletRequest.getRequestURI().indexOf(".do") > -1){
          redirectURL.append(httpServletRequest.getRequestURI().substring(httpServletRequest.getRequestURI().lastIndexOf("/"), httpServletRequest.getRequestURI().length()));

          }else if(httpServletRequest.getRequestURI().indexOf("/blog/") == -1){

          }else{
          redirectURL.append("/blogView.do");

          }
          redirectURL.append("?go=");
          redirectURL.append(blogId);

          httpServletRequest.getRequestDispatcher(redirectURL.toString())
          .forward((ServletRequest)httpServletRequest, (ServletResponse)httpServletResponse);
          //httpServletResponse.sendRedirect(redirectURL.toString());
          }else{
          httpServletRequest.getRequestDispatcher(httpServletRequest.getRequestURI())
          .forward((ServletRequest)httpServletRequest, (ServletResponse)httpServletResponse);
          //httpServletResponse.sendRedirect(httpServletRequest.getRequestURI());
          }
          System.out.println(redirectURL.toString());
          return;


          }

          /**
          * Take out of service
          */
          public void destroy() {
          super.destroy();

          if (_logger.isDebugEnabled()) {
          _logger.debug(" destroyed");
          }
          }
          }






           

          posted @ 2008-09-24 18:04 豬 閱讀(4774) | 評(píng)論 (4)編輯 收藏
          僅列出標(biāo)題
          共27頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 

          公告

          希望有一天

          我能用鼠標(biāo)雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網(wǎng)絡(luò)

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 黄大仙区| 海淀区| 黎城县| 隆昌县| SHOW| 达日县| 枣庄市| 宁德市| 万州区| 醴陵市| 仁怀市| 岑巩县| 开化县| 特克斯县| 双流县| 兴仁县| 剑川县| 朝阳市| 宜川县| 四会市| 龙海市| 汽车| 三台县| 闵行区| 镇安县| 延津县| 东安县| 万荣县| 化州市| 潍坊市| 娄烦县| 正安县| 呼图壁县| 交城县| 通许县| 北碚区| 绵阳市| 宝鸡市| 荔波县| 义马市| 德安县|