亚洲精品9999久久久久无码,国产成人精品久久亚洲,亚洲精品乱码久久久久久下载http://www.tkk7.com/feicer/java hibernate struts2 springzh-cnMon, 12 May 2025 05:57:34 GMTMon, 12 May 2025 05:57:34 GMT60MzTree2.0梅花雪樹單擊Node頁面跳動問題解決及單擊展開字節點http://www.tkk7.com/feicer/articles/MzTree2_0.htmlfdfdFri, 11 Dec 2009 01:45:00 GMThttp://www.tkk7.com/feicer/articles/MzTree2_0.htmlhttp://www.tkk7.com/feicer/comments/305507.htmlhttp://www.tkk7.com/feicer/articles/MzTree2_0.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/305507.htmlhttp://www.tkk7.com/feicer/services/trackbacks/305507.html閱讀全文

fd 2009-12-11 09:45 發表評論
]]>
利用JavaScript創建隨機數與隨機圖片http://www.tkk7.com/feicer/articles/random1.htmlfdfdFri, 06 Nov 2009 07:59:00 GMThttp://www.tkk7.com/feicer/articles/random1.htmlhttp://www.tkk7.com/feicer/comments/301454.htmlhttp://www.tkk7.com/feicer/articles/random1.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/301454.htmlhttp://www.tkk7.com/feicer/services/trackbacks/301454.html閱讀全文

fd 2009-11-06 15:59 發表評論
]]>
struts2中使用Fckeditor 2.6.5 + fckeditor-java-2.5-binhttp://www.tkk7.com/feicer/articles/300867.htmlfdfdTue, 03 Nov 2009 03:48:00 GMThttp://www.tkk7.com/feicer/articles/300867.htmlhttp://www.tkk7.com/feicer/comments/300867.htmlhttp://www.tkk7.com/feicer/articles/300867.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/300867.htmlhttp://www.tkk7.com/feicer/services/trackbacks/300867.html閱讀全文

fd 2009-11-03 11:48 發表評論
]]>
Struts2的properties配置文件詳解http://www.tkk7.com/feicer/articles/Struts2_properties.htmlfdfdMon, 02 Nov 2009 00:14:00 GMThttp://www.tkk7.com/feicer/articles/Struts2_properties.htmlhttp://www.tkk7.com/feicer/comments/300626.htmlhttp://www.tkk7.com/feicer/articles/Struts2_properties.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/300626.htmlhttp://www.tkk7.com/feicer/services/trackbacks/300626.html閱讀全文

fd 2009-11-02 08:14 發表評論
]]>
Hibernate SQLQuery 原生SQL 查詢及返回結果集處理-2http://www.tkk7.com/feicer/articles/SQLQuery_2.htmlfdfdFri, 25 Sep 2009 07:47:00 GMThttp://www.tkk7.com/feicer/articles/SQLQuery_2.htmlhttp://www.tkk7.com/feicer/comments/296435.htmlhttp://www.tkk7.com/feicer/articles/SQLQuery_2.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/296435.htmlhttp://www.tkk7.com/feicer/services/trackbacks/296435.html第二篇:個人方法

1.
返回List,
.setResultTransformer(
     Transformers.ALIAS_TO_ENTITY_MAP);將結果轉為Map,存放到list中,即list中為若干map對象;
獲取可按此方法:

        for (int i = 0; i < list.size(); i++) {
            Map map 
= (Map) list.get(i);
            System.out.println(map.get(
"table_colum_name"));
        }

在jsp頁面,可用S標簽獲取
<s:property value="table_colum_name" />

    public List  getDamTestType(Integer damId) throws Exception {
        List list 
= new ArrayList();
        Session session 
= null;
        Transaction tx 
= null;
        String strSql1 
= "";
        
        strSql1 
= "select distinct testType.id,testType.mc from [" + damId + "_lb] as testType "
                
+ "inner join [" + damId + "_jcxm] as testObj on testObj.lbgl_id=testType.id "
                
+ "inner join [" + damId + "_cd] as testSite on testSite.jcxm_id=testObj.id "
                
+ "inner join [" + damId + "_sctj] as upLoadCond on upLoadCond.cd_id=testSite.id ";

        
try {
            session 
= HibernateSessionFactory.getSession();
            tx 
= session.beginTransaction();
            Query query 
= session.createSQLQuery(strSql1).setResultTransformer(
                    Transformers.ALIAS_TO_ENTITY_MAP);
            list 
= query.list();
            tx.commit();
        } 
catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
            
throw e;
        } 
finally {
            session.close();
        }
        

        
return list;
    }


下次再寫。。。



fd 2009-09-25 15:47 發表評論
]]>
Hibernate SQLQuery 原生SQL 查詢及返回結果集處理-1http://www.tkk7.com/feicer/articles/SQLQuery_1.htmlfdfdFri, 25 Sep 2009 07:37:00 GMThttp://www.tkk7.com/feicer/articles/SQLQuery_1.htmlhttp://www.tkk7.com/feicer/comments/296430.htmlhttp://www.tkk7.com/feicer/articles/SQLQuery_1.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/296430.htmlhttp://www.tkk7.com/feicer/services/trackbacks/296430.html閱讀全文

fd 2009-09-25 15:37 發表評論
]]>
Quartz 的Web 應用-監聽器方式調用http://www.tkk7.com/feicer/articles/QuartzInitializerListener_1.htmlfdfdMon, 07 Sep 2009 09:13:00 GMThttp://www.tkk7.com/feicer/articles/QuartzInitializerListener_1.htmlhttp://www.tkk7.com/feicer/comments/294198.htmlhttp://www.tkk7.com/feicer/articles/QuartzInitializerListener_1.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/294198.htmlhttp://www.tkk7.com/feicer/services/trackbacks/294198.html閱讀全文

fd 2009-09-07 17:13 發表評論
]]>
Quartz 在Java Web Application中使用http://www.tkk7.com/feicer/articles/Quartz_2.htmlfdfdFri, 04 Sep 2009 09:10:00 GMThttp://www.tkk7.com/feicer/articles/Quartz_2.htmlhttp://www.tkk7.com/feicer/comments/293911.htmlhttp://www.tkk7.com/feicer/articles/Quartz_2.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/293911.htmlhttp://www.tkk7.com/feicer/services/trackbacks/293911.html閱讀全文

fd 2009-09-04 17:10 發表評論
]]>
Quartz 項目應用http://www.tkk7.com/feicer/articles/Quartz_1.htmlfdfdFri, 04 Sep 2009 08:22:00 GMThttp://www.tkk7.com/feicer/articles/Quartz_1.htmlhttp://www.tkk7.com/feicer/comments/293897.htmlhttp://www.tkk7.com/feicer/articles/Quartz_1.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/293897.htmlhttp://www.tkk7.com/feicer/services/trackbacks/293897.html閱讀全文

fd 2009-09-04 16:22 發表評論
]]>
一個E-R圖的例子http://www.tkk7.com/feicer/articles/E-R.htmlfdfdTue, 26 May 2009 07:41:00 GMThttp://www.tkk7.com/feicer/articles/E-R.htmlhttp://www.tkk7.com/feicer/comments/278017.htmlhttp://www.tkk7.com/feicer/articles/E-R.html#Feedback1http://www.tkk7.com/feicer/comments/commentRss/278017.htmlhttp://www.tkk7.com/feicer/services/trackbacks/278017.html一個E-R圖的例子




fd 2009-05-26 15:41 發表評論
]]>
Struts 2與AJAX(第三部分) http://www.tkk7.com/feicer/articles/258262.htmlfdfdFri, 06 Mar 2009 08:55:00 GMThttp://www.tkk7.com/feicer/articles/258262.htmlhttp://www.tkk7.com/feicer/comments/258262.htmlhttp://www.tkk7.com/feicer/articles/258262.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258262.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258262.html閱讀全文

fd 2009-03-06 16:55 發表評論
]]>
Struts 2與AJAX(第二部分) http://www.tkk7.com/feicer/articles/258260.htmlfdfdFri, 06 Mar 2009 08:53:00 GMThttp://www.tkk7.com/feicer/articles/258260.htmlhttp://www.tkk7.com/feicer/comments/258260.htmlhttp://www.tkk7.com/feicer/articles/258260.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258260.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258260.html閱讀全文

fd 2009-03-06 16:53 發表評論
]]>
Struts 2與AJAX(第一部分) http://www.tkk7.com/feicer/articles/258248.htmlfdfdFri, 06 Mar 2009 08:44:00 GMThttp://www.tkk7.com/feicer/articles/258248.htmlhttp://www.tkk7.com/feicer/comments/258248.htmlhttp://www.tkk7.com/feicer/articles/258248.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258248.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258248.html閱讀全文

fd 2009-03-06 16:44 發表評論
]]>
Strus 2的新表單標志的使用 http://www.tkk7.com/feicer/articles/258244.htmlfdfdFri, 06 Mar 2009 08:34:00 GMThttp://www.tkk7.com/feicer/articles/258244.htmlhttp://www.tkk7.com/feicer/comments/258244.htmlhttp://www.tkk7.com/feicer/articles/258244.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258244.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258244.html 載自http://www.tkk7.com/max

 Struts 2為大家提供了不少常用的很酷的表單標志,簡化了我們程序員的工作。不過,由于這些都是新標志,大家可能在使用上還存在不少疑問。本文將就朋友們的回復、留言或Email上的問題,分別對這些酷標志進行講述。

表單標志使用小技巧

Struts 2的表單標志在輸出(render)HTML時,使用了模板的概念,增加了復雜性(因為它不像Struts 1.x的表單標志,它通常都是一個標志對應HTML的一個元素),因此大家在使用時,需要一些技巧:

  1. Struts 2的UI標志的表單標志默認是以表格布局,按鈕是右對齊的。如果你不喜歡此風格,你可以簡單地將<s:form />標志的“theme”屬性設為“simple”,然后用以往的做法自已布局表單元素(注意:此法有利有弊,弊就是當你將“theme”屬性設為“simple”時,表單標志以最簡單方式輸出HTML,所以你可能失去一些默認輸出提供的便利,如:友好的錯誤信息的顯示,或客戶端的表單驗證等)。當然更好的做法是通過CSS或自定義主題(theme)然后應用到整個應用程序,這樣可以獲得一致的頁面風格,加強用戶體驗(我會在以后的文章對此進行講解);
  2. 當你在頁面上加入某些標志(如:<s:doubleselect />等)時,應該通過action來訪問頁面,而不是通過*.jsp的URL直接訪問。

下面我將分別對這些標志進行講述:

1、<s:checkboxlist />

大家對<s:checkboxlist />的最大的疑問可能是:“如何在默認情況下,選中某些checkbox?”

答案其實很簡單,只需要將其“value”屬性設為你的要選中的值,如以代碼所示:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
<title>Struts 2 Cool Tags - &lt;s:checkboxlist/ &gt;</title>
   
<s:head />
</head>
<body>    
   
<h2>&lt;s:checkboxlist/&gt;</h2>
   
<s:form action="Store" >
       
<s:checkboxlist name="skills1" 
                        label
="Skills 1" 
                        list
="{ 'Java', '.Net', 'RoR', 'PHP' }" 
                        value
="{ 'Java', '.Net' }" />
       
<s:checkboxlist name="skills2" 
                        label
="Skills 2" 
                        list
="#{ 1:'Java', 2: '.Net', 3: 'RoR', 4: 'PHP' }" 
                        listKey
="key" 
                        listValue
="value" 
                        value
="{ 1, 2, 3 }"/>
   
</s:form>
</body>
</html>
清單1 WebContent/checkboxlist.jsp

分布運行應用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/checkboxlist.jsp,出現如下圖所示頁面:

checkboxlist.jsp頁面
清單2 checkboxlist.jsp頁面

2、<s:doubleselect />

大家看Struts 2的showcase的例子,<s:doubleselect />的用法如下所示:

    <s:doubleselect
           
tooltip="Choose Your State"
            label
="State"
            name
="region" list="{'North', 'South'}"
            value
="'South'"
            doubleValue
="'Florida'"
            doubleList
="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" 
            doubleName
="state"
            headerKey
="-1"
            headerValue
="---------- Please Select ----------"
            emptyOption
="true" />
清單3 Showcase中<s:doubleselect />

很多朋友問:“上面的‘list’屬性只有兩個值,如果我有三個或更多的值,‘doublelist’屬性應該如何設定呢?”

我建議的做法是先定義一個Map類型的對象,鍵為“list”的集合,值則為“doubleList”的集合,然后“doubleList”的OGNL寫成“#myMap[top]”,如以下代碼所示:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
<title>Struts 2 Cool Tags - &lt;s:doubeselect/ &gt;</title>
   
<s:head />
</head>
<body>    
   
<h2>&lt;s:doubleselect/&gt;</h2>
   
<s:form action="Store" >
       
<s:set name="foobar" 
               value
="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" />
       
<s:doubleselect list="#foobar.keySet()"
                          doubleName
="technology" 
                          doubleList
="#foobar[top]" 
                          label
="Technology" />
   
</s:form>
</body>
</html>
清單4 WebContent/doubleselect.jsp

分布運行應用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/doubleselect.action,出現如下圖所示頁面:

doubleselect.jsp頁面
清單5 doubleselect.jsp頁面

3、<s: token />

這個標志可能大家不常用,不過本人認為它還是挺有用的。在使用Struts 1.x時,因為跳轉通常是用Forward(而不是Redirect)實現的,所以當用戶完成請求后,按“F5”刷新頁面時,就會重新提交上次的請求,這樣經常會出錯。要解決這個問題,<s:token />可以幫你忙。

實現原理

在頁面加載時,<s: token />產生一個GUID(Globally Unique Identifier,全局唯一標識符)值的隱藏輸入框如:

<input type="hidden" name="struts.token.name" value="struts.token"/>
<input type="hidden" name="struts.token" value="BXPNNDG6BB11ZXHPI4E106CZ5K7VNMHR"/>
清單6 <s:token />的HTML輸出

同時,將GUID放到會話(session)中;在執行action之前,“token”攔截器將會話token與請求token比較,如果兩者相同,則將會話中的token刪除并往下執行,否則向actionErrors加入錯誤信息。如此一來,如果用戶通過某種手段提交了兩次相同的請求,兩個token就會不同。

具體實現

首先看一下Action的代碼:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class CoolTagAction extends ActionSupport {    
   
private static final long serialVersionUID = 6820659617470261780L;
   
   
private String message;
       
   
public String getMessage() {
       
return message;
   }


   
public void setMessage(String message) {
       
this.message = message;
   }

   
   @Override
   
public String execute() {
       System.out.println(
"Executing action, your message is " + message);
       
return SUCCESS;
   }
   
}
清單7 src/tutorial/CoolTagAction.java

以上代碼一目了然,再看看JSP的寫法:

%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
<title>Struts 2 Cool Tags - &lt;s:token/ &gt;</title>
   
<s:head />
</head>
<body>    
   
<h2>&lt;s:token/&gt;</h2>
   
<s:actionerror />
   
<s:form action="Token" >
       
<s:textfield name="message" label="Message" />
       
<s:token />
       
<s:submit />
   
</s:form>
</body>
</html>
清單8 WebContent/token.jsp

JSP也很簡單,就是加入<s:token />標志。接下來是Actoin配置的XML片段:

<?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="Struts2_COOL_TAGS_DEMO" extends="struts-default">
       
<action name="Token" class="tutorial.CoolTagAction">
           
<interceptor-ref name="defaultStack" />
           
<interceptor-ref name="token" />
           
<result name="invalid.token">/token.jsp</result>                        
           
<result>/token.jsp</result>
       
</action>
       
<action name="*">
           
<result>/{1}.jsp</result>
       
</action>
   
</package>
</struts>
清單9 src/struts.xml

以上XML片段值注意的是加入了“token”攔截器和“invalid.token”結果,因為“token”攔截器在會話token與請求token不一致時,將會直接返回“invalid.token”結果。

發布運行應用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/token.jsp,出現如下圖所示頁面:

正常顯示的token.jsp頁面
清單10 正常顯示的token.jsp頁面

隨便填點東西并提交頁面,一切正常返回以上頁面,然后按“F5”刷新頁面,在彈出的對話框中點擊“Retry”,出現如下圖所示頁面:

重復提交出錯顯示
清單11 重復提交出錯顯示

4、<s:datetimepicker />、<s:optiontransferselect />和<s:updownselect />

這幾個標志的使用相對簡單,所以我想小舉一例即可,以下是JSP的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
<title>Struts 2 Cool Tags - Others</title>
   
<s:head />
</head>
<body>    
   
<h2>Others</h2>
   
<s:form action="Store" >
       
<s:datetimepicker name="birthday" label="Birthday" />
       
<s:updownselect
           
label = "Favourite Countries"
            list
="#{'england':'England', 'america':'America', 'germany':'Germany'}"
            name
="prioritisedFavouriteCountries"
            headerKey
="-1"
            headerValue
="--- Please Order Them Accordingly ---"
            emptyOption
="true" />
       
<s:optiontransferselect            
           
label="Favourite Cartoons Characters"
            name
="leftSideCartoonCharacters" 
            leftTitle
="Left Title"
            rightTitle
="Right Title"
            list
="{'Popeye', 'He-Man', 'Spiderman'}" 
            multiple
="true"
            headerKey
="headerKey"
            headerValue
="--- Please Select ---"
            emptyOption
="true"
            doubleList
="{'Superman', 'Mickey Mouse', 'Donald Duck'}" 
            doubleName
="rightSideCartoonCharacters"
            doubleHeaderKey
="doubleHeaderKey"
            doubleHeaderValue
="--- Please Select ---" 
            doubleEmptyOption
="true"
            doubleMultiple
="true" />
   
</s:form>
</body>
</html>
清單12 WebContent\others.jsp頁面

發布運行應用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/others.jsp,出現如下圖所示頁面:

點擊查看大圖
清單13 其它表單標志頁面

總結

Struts 2在標志上的確比Struts 1.x豐富了許多,同時模板機制也給程序員帶來不少方便(如果你不太喜歡個性化的風格)。另外,Struts 2還有一些AJAX(如<s: autocompleter />等)的標志和非表單的UI標志(如<s: tree />等),我會在以后的文章中講述其使用。



fd 2009-03-06 16:34 發表評論
]]>
Struts 2中的OGNL http://www.tkk7.com/feicer/articles/258239.htmlfdfdFri, 06 Mar 2009 08:20:00 GMThttp://www.tkk7.com/feicer/articles/258239.htmlhttp://www.tkk7.com/feicer/comments/258239.htmlhttp://www.tkk7.com/feicer/articles/258239.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258239.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258239.html閱讀全文

fd 2009-03-06 16:20 發表評論
]]>
在Struts 2中實現CRUD http://www.tkk7.com/feicer/articles/258234.htmlfdfdFri, 06 Mar 2009 08:15:00 GMThttp://www.tkk7.com/feicer/articles/258234.htmlhttp://www.tkk7.com/feicer/comments/258234.htmlhttp://www.tkk7.com/feicer/articles/258234.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258234.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258234.html閱讀全文

fd 2009-03-06 16:15 發表評論
]]>
在Struts 2中實現文件上傳 http://www.tkk7.com/feicer/articles/258231.htmlfdfdFri, 06 Mar 2009 08:05:00 GMThttp://www.tkk7.com/feicer/articles/258231.htmlhttp://www.tkk7.com/feicer/comments/258231.htmlhttp://www.tkk7.com/feicer/articles/258231.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258231.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258231.html閱讀全文

fd 2009-03-06 16:05 發表評論
]]>
在Struts 2中實現IoChttp://www.tkk7.com/feicer/articles/258230.htmlfdfdFri, 06 Mar 2009 08:03:00 GMThttp://www.tkk7.com/feicer/articles/258230.htmlhttp://www.tkk7.com/feicer/comments/258230.htmlhttp://www.tkk7.com/feicer/articles/258230.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258230.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258230.html 載自http://www.tkk7.com/max

IoC(Inversion of Control,以下譯為控制反轉)隨著Java社區中輕量級容器(Lightweight Contianer)的推廣而越來越為大家耳熟能詳。在此,我不想再多費唇舌來解釋“什么是控制反轉”和“為什么需要控制反轉”。因為互聯網上已經有非常多的文章對諸如此類的問題作了精彩而準確的回答。大家可以去讀一下Rod Johnson和Juergen Hoeller合著的《Expert one-on-one J2EE Development without EJB》或Martin Fowler所寫的《Inversion of Control Containers and the Dependency Injection pattern》。

言歸正傳,本文的目的主要是介紹在Struts 2中實現控制反轉。

歷史背景

眾所周知,Struts 2是以Webwork 2作為基礎發展出來。而在Webwork 2.2之前的Webwork版本,其自身有一套控制反轉的實現,Webwork 2.2在Spring 框架的如火如荼發展的背景下,決定放棄控制反轉功能的開發,轉由Spring實現。值得一提的是,Spring確實是一個值得學習的框架,因為有越來越多的開源組件(如iBATIS等)都放棄與Spring重疊的功能的開發。因此,Struts 2推薦大家通過Spring實現控制反轉。

具體實現

首先,在開發環境中配置好Struts 2的工程。對這部分仍然有問題的朋友,請參考我的早前的文章。

然后,將所需的Spring的jar包加入到工程的構建環境(Build Path)中,如下圖1所示:

圖1 所依賴的Spring的jar包
圖1 所依賴的Spring的jar包

本文使用的是Spring 2.0,Spring強烈建議大家在使用其jar包時,只引用需要的包,原因是Spring是一個功能非常強大的框架,其中有些功能是您不需要的;而且Spring提倡的是“按需所取”,而不是EJB的“愛我就要愛我的一切”。當然,如果你怕麻煩或者是不清楚每個包的作用,引用一個Spring的總包也未嘗不可。

接下來,就要修改WEB-INF\web.xml文件了,內容為:

<? 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"
>

   
< display-name > Struts 2 IoC Demo </ display-name >

   
< filter >
       
< filter-name > struts-cleanup </ filter-name >
       
< filter-class >
            org.apache.struts2.dispatcher.ActionContextCleanUp
       
</ filter-class >
   
</ filter >

   
< filter >
       
< filter-name > struts2 </ filter-name >
       
< filter-class >
            org.apache.struts2.dispatcher.FilterDispatcher
       
</ filter-class >
   
</ filter >

   
< filter-mapping >
       
< filter-name > struts-cleanup </ filter-name >
       
< url-pattern > /* </ url-pattern >
   
</ filter-mapping >

   
< filter-mapping >
       
< filter-name > struts2 </ filter-name >
       
< url-pattern > /* </ url-pattern >
   
</ filter-mapping >

   
< listener >
       
< listener-class >
            org.springframework.web.context.ContextLoaderListener
       
</ listener-class >
   
</ listener >

   
< welcome-file-list >
       
< welcome-file > index.html </ welcome-file >
   
</ welcome-file-list >
</ web-app >
清單1 WEB-INF\web.xml

大家一看便知道,主要是加入Spring的ContextLoaderListener監聽器,方便Spring與Web容器交互。

緊接著,修改Struts.properties文件,告知Struts 2運行時使用Spring來創建對象(如Action等),內容如下:

struts.objectFactory = spring
清單2 classes\struts.properties

再下來,遵循Spring的原則——面向接口編程,創建接口ChatService,代碼如下:

package tutorial;

import java.util.Set;

public interface ChatService {
   Set
< String > getUserNames();
}
清單3 tutorial.ChatService.java

然后,再創建一個默認實現ChatServiceImpl,代碼如下:

package tutorial;

import java.util.HashSet;
import java.util.Set;

public class ChatServiceImpl implements ChatService {

   
public Set < String > getUserNames() {
       Set
< String > users = new HashSet < String > ();
       users.add(
" Max " );
       users.add(
" Scott " );
       users.add(
" Bob " );
       
return users;
   }


}
清單4 tutorial.ChatServiceImpl.java

接下來,就該新建Action了。tutorial.ChatAction.java的代碼如下:

package tutorial;

import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

public class ChatAction extends ActionSupport {
   
private static final long serialVersionUID = 8445871212065L
   
   
private ChatService chatService;
   
private Set < String > userNames;

   
public void setChatService(ChatService chatService) {
       
this .chatService = chatService;
   }

   
   
public Set < String > getUserNames() {
       
return userNames;
   }

   
   @Override
   
public String execute() {
       userNames
= chatService.getUserNames();
       
return SUCCESS;
   }

   
}
清單5 tutorial.ChatAction.java

ChatAction類使用屬性(Getter/Setter)注入法取得ChatService對象。

然后,配置Spring的applicationContext.xml(位于WEB-INF下)文件,內容如下:

<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
   
< bean id ="chatService" class ="tutorial.ChatServiceImpl" />
   
< bean id ="chatAction" class ="tutorial.ChatAction" scope ="prototype" >
       
< property name ="chatService" >
           
< ref local ="chatService" />
       
</ property >
   
</ bean >
</ beans >
清單6 WEB-INF\applicationContext.xml

上述代碼有二點值得大家注意的:

  1. Struts 2會為每一個請求創建一個Action對象,所以在定義chatAction時,使用scope="prototype"。這樣Spring就會每次都返回一個新的ChatAction對象了;
  2. 因為ChatServiceImpl被配置為默認的scope(也即是singleton,唯一的),所以在實現時應保證其線程安全(關于編寫線程安全的代碼的討論已經超出本文的范圍,更超出了本人的能力范圍,大家可以參考Addison Wesley Professional出版的《Java Concurrency in Practice》)。

接下來,在classes/struts.xml中配置Action,內容如下:

<! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
   
< include file ="struts-default.xml" />    
    
   
< package name ="Struts2_IoC" extends ="struts-default" >
       
< action name ="Chat" class ="chatAction" >
           
< result > /UserList.jsp </ result >
       
</ action >
   
</ package >    
</ struts >
清單7 classes\struts.xml

這里的Action和平常不同的就是class屬性,它對應于Spring所定義的bean的id,而不是它的類全名。

最后,讓我們看看/UserList.jsp,內容如下:

<% @ page contentType = " text/html; charset=UTF-8 " %>
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
   
< title > User List </ title >
</ head >

< body >
   
< h2 > User List </ h2 >
   
< ol >
   
< s:iterator value ="userNames" >
       
< li >< s:property /></ li >
   
</ s:iterator >
   
</ ol >
</ body >
</ html >
清單8 /UserList.jsp

大功告成,分布運行應用程序,在瀏覽器中鍵入http://localhost:8080/Struts2_IoC/Chat.action,出現如圖2所示頁面:

圖2 /ListUser.jsp
圖2 /ListUser.jsp

總結

通過Spring在Struts 2上實現控制反轉是強烈推薦的做法,當然您也可以組合其它的實現(如Pico等)。



fd 2009-03-06 16:03 發表評論
]]>
Struts 2的基石——攔截器(Interceptor)http://www.tkk7.com/feicer/articles/258229.htmlfdfdFri, 06 Mar 2009 07:59:00 GMThttp://www.tkk7.com/feicer/articles/258229.htmlhttp://www.tkk7.com/feicer/comments/258229.htmlhttp://www.tkk7.com/feicer/articles/258229.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258229.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258229.html閱讀全文

fd 2009-03-06 15:59 發表評論
]]>
在Struts 2.0中實現表單數據校驗(Validation) http://www.tkk7.com/feicer/articles/258224.htmlfdfdFri, 06 Mar 2009 07:45:00 GMThttp://www.tkk7.com/feicer/articles/258224.htmlhttp://www.tkk7.com/feicer/comments/258224.htmlhttp://www.tkk7.com/feicer/articles/258224.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258224.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258224.html閱讀全文

fd 2009-03-06 15:45 發表評論
]]>
轉換器(Converter)——Struts 2.0中的魔術師http://www.tkk7.com/feicer/articles/258218.htmlfdfdFri, 06 Mar 2009 07:32:00 GMThttp://www.tkk7.com/feicer/articles/258218.htmlhttp://www.tkk7.com/feicer/comments/258218.htmlhttp://www.tkk7.com/feicer/articles/258218.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258218.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258218.html閱讀全文

fd 2009-03-06 15:32 發表評論
]]>
在Struts 2.0中國際化(i18n)您的應用程序http://www.tkk7.com/feicer/articles/258216.htmlfdfdFri, 06 Mar 2009 07:26:00 GMThttp://www.tkk7.com/feicer/articles/258216.htmlhttp://www.tkk7.com/feicer/comments/258216.htmlhttp://www.tkk7.com/feicer/articles/258216.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258216.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258216.html載自http://www.tkk7.com/max
在Struts 2.0中國際化(i18n)您的應用程序

國際化是商業系統中不可或缺的一部分,所以無論您學習的是什么Web框架,它都是必須掌握的技能。

其實,Struts 1.x在此部分已經做得相當不錯了。它極大地簡化了我們程序員在做國際化時所需的工作,例如,如果您要輸出一條國際化的信息,只需在代碼包中加入FILE-NAME_xx_XX.properties(其中FILE-NAME為默認資源文件的文件名),然后在struts-config.xml中指明其路徑,再在頁面用<bean:message>標志輸出即可。

不過,所謂“沒有最好,只有更好”。Struts 2.0并沒有在這部分止步,而是在原有的簡單易用的基礎上,將其做得更靈活、更強大。

國際化Hello World

下面讓我們看一個例子——HelloWorld。這個例子演示如何根據用戶瀏覽器的設置輸出相應的HelloWorld。

  1. 在Eclipse創建工程配置開發和運行環境(如果對這個步驟有問題,可以參考我早前的文章《為Struts 2.0做好準備》)。
  2. 在src文件夾中加入struts.properties文件,內容如下:
    struts.custom.i18n.resources=globalMessages
    Struts 2.0有兩個配置文件,struts.xml和struts.properties都是放在WEB-INF/classes/下。
    • struts.xml用于應用程序相關的配置
    • struts.properties用于Struts 2.0的運行時(Runtime)的配置
  3. 在src文件夾中加入globalMessages_en_US.properties文件,內容如下:
    HelloWorld=Hello World!
  4. 在src文件夾中加入globalMessages_zh_CN.properties文件,內容如下:
    HelloWorld=你好,世界!
    在此想和大家分享一個不錯的編寫properties文件的Eclipse插件(plugin),有了它我們在編輯一些簡體中文、繁體中文等Unicode文本時,就不必再使用native2ascii編碼了。您可以通過Eclipse中的軟件升級(Software Update)安裝此插件,步驟如下:
    1、展開Eclipse的Help菜單,將鼠標移到Software Update子項,在出現的子菜單中點擊Find and Install;
    2、在Install/Update對話框中選擇Search for new features to install,點擊Next;
    3、在Install對話框中點擊New Remote Site;
    4、在New Update Site對話框的Name填入“PropEdit”或其它任意非空字符串,在URL中填入http://propedit.sourceforge.jp/eclipse/updates/;
    5、在Site to include to search列表中,除上一步加入的site外的其它選項去掉,點擊Finsih;
    6、在彈出的Updates對話框中的Select the features to install列表中將所有結尾為“3.1.x”的選項去掉(適用于Eclipse 3.2版本的朋友);
    7、點擊Finish關閉對話框;
    8、在下載后,同意安裝,再按提示重啟Eclipse,在工具條看到形似vi的按鈕表示安裝成功,插件可用。此時,Eclpise中所有properties文件的文件名前有綠色的P的圖標作為標識。
  5. 在WebContent文件夾下加入HelloWorl.jsp文件,內容如下:
    <%@ page  contentType="text/html; charset=UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
       
    <title>Hello World</title>
    </head>
    <body>
       
    <h2><s:text name="HelloWorld"/></h2>
       
    <h2><s:property value="%{getText('HelloWorld')}"/></h2>
    </body>
    </html>
  6. 發布運行應用程序,在瀏覽器地址欄中輸入http://localhost:8080/Struts2_i18n/HelloWorld.jsp ,出現圖1所示頁面。
    圖1 中文輸出
    圖1 中文輸出
  7. 將瀏覽器的默認語言改為“英語(美國)”,刷新頁面,出現圖2所示頁面。
    圖2 英文輸出
    圖2 英文輸出
上面的例子的做法,與Struts 1.x的做法相似,似乎并不能體現Struts 2.0的優勢。不過,我在上面的例子用了兩種方法來顯示國際化字符串,其輸出是相同的。其實,這就是Struts 2.0的一個優勢,因為它默認支持EL,所示我們可以用getText方法來簡潔地取得國際化字符串。另外更普遍的情況——在使用UI表單標志時,getText可以用來設置label屬性,例如:
<s:textfield name="name" label="%{getText('UserName')}"/>

資源文件查找順序

之所以說Struts 2.0的國際化更靈活是因為它可以能根據不同需要配置和獲取資源(properties)文件。在Struts 2.0中有下面幾種方法:

  1. 使用全局的資源文件,方法如上例所示。這適用于遍布于整個應用程序的國際化字符串,它們在不同的包(package)中被引用,如一些比較共用的出錯提示;
  2. 使用包范圍內的資源文件。做法是在包的根目錄下新建名的package.properties和package_xx_XX.properties文件。這就適用于在包中不同類訪問的資源;
  3. 使用Action范圍的資源文件。做法為Action的包下新建文件名(除文件擴展名外)與Action類名同樣的資源文件。它只能在該Action中訪問。如此一來,我們就可以在不同的Action里使用相同的properties名表示不同的值。例如,在ActonOne中title為“動作一”,而同樣用title在ActionTwo表示“動作二”,節省一些命名工夫;
  4. 使用<s:i18n>標志訪問特定路徑的properties文件。使用方法請參考我早前的文章《常用的Struts 2.0的標志(Tag)介紹》。在您使用這一方法時,請注意<s:i18n>標志的范圍。在<s:i18n name="xxxxx">到</s:i18n>之間,所有的國際化字符串都會在名為xxxxx資源文件查找,如果找不到,Struts 2.0就會輸出默認值(國際化字符串的名字)。

上面我列舉了四種配置和訪問資源的方法,它們的范圍分別是從大到小,而Struts 2.0在查找國際化字符串所遵循的是特定的順序,如圖3所示:

圖3 資源文件查找順序圖
圖3 資源文件查找順序圖

假設我們在某個ChildAction中調用了getText("user.title"),Struts 2.0的將會執行以下的操作:

  1. 查找ChildAction_xx_XX.properties文件或ChildAction.properties;
  2. 查找ChildAction實現的接口,查找與接口同名的資源文件MyInterface.properties;
  3. 查找ChildAction的父類ParentAction的properties文件,文件名為ParentAction.properties;
  4. 判斷當前ChildAction是否實現接口ModelDriven。如果是,調用getModel()獲得對象,查找與其同名的資源文件;
  5. 查找當前包下的package.properties文件;
  6. 查找當前包的父包,直到最頂層包;
  7. 在值棧(Value Stack)中,查找名為user的屬性,轉到user類型同名的資源文件,查找鍵為title的資源;
  8. 查找在struts.properties配置的默認的資源文件,參考例1;
  9. 輸出user.title。

參數化國際化字符串

許多情況下,我們都需要在動行時(runtime)為國際化字符插入一些參數,例如在輸入驗證提示信息的時候。在Struts 2.0中,我們通過以下兩種方法做到這點:

  1. 在資源文件的國際化字符串中使用OGNL,格式為${表達式},例如:
    validation.require=${getText(fileName)} is required
  2. 使用java.text.MessageFormat中的字符串格式,格式為{ 參數序號(從0開始), 格式類形(number | date | time | choice), 格式樣式},例如:
    validation.between=Date must between {0, date, short} and {1, date, short}
在顯示這些國際化字符時,同樣有兩種方法設置參數的值:
  1. 使用標志的value0、value1...valueN的屬性,如:
    <s:text name="validation.required" value0="User Name"/>
  2. 使用param子元素,這些param將按先后順序,代入到國際化字符串的參數中,例如:
    <s:text name="validation.required">
       
    <s:param value="User Name"/>
    </s:text>

讓用戶方便地選擇語言

開發國際化的應用程序時,有一個功能是必不可少的——讓用戶快捷地選擇或切換語言。在Struts 2.0中,通過ActionContext.getContext().setLocale(Locale arg)可以設置用戶的默認語言。不過,由于這是一個比較普遍的應用場景(Scenario),所以Struts 2.0為您提供了一個名i18n的攔截器(Interceptor),并在默認情況下將其注冊到攔截器鏈(Interceptor chain)中。它的原理為在執行Action方法前,i18n攔截器查找請求中的一個名為"request_locale"的參數。如果其存在,攔截器就將其作為參數實例化Locale對象,并將其設為用戶默認的區域(Locale),最后,將此Locale對象保存在session的名為“WW_TRANS_I18N_LOCALE”的屬性中。

下面,我將提供一完整示例演示它的使用方法。

package tutorial;

import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;

publicclass Locales {
   
public Map<String, Locale> getLocales() {
       Map
<String, Locale> locales =new Hashtable<String, Locale>(2);
       locales.put(
"American English", Locale.US);
       locales.put(
"Simplified Chinese", Locale.CHINA);
       
return locales;
   }

}
tutorial/Locales.java

<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript">
<!--
   
function langSelecter_onChanged() {
        document.langForm.submit();
    }
//-->
</script>
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<s:bean id="locales" name="tutorial.Locales"/>
<form action="<s:url includeParams="get" encode="true"/>" name="langForm" 
    style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;">
    Language:
<s:select label="Language" 
        list
="#locales.locales" listKey="value"    listValue="key"
        value
="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
        name
="request_locale" id="langSelecter" 
        onchange
="langSelecter_onChanged()" theme="simple"/>
</form>
LangSelector.jsp

上述代碼的原理為,LangSelector.jsp先實例化一個Locales對象,并把對象的Map類型的屬性locales賦予下拉列表(select) 。如此一來,下拉列表就獲得可用語言的列表。大家看到LangSelector有<s:form>標志和一段Javascript腳本,它們的作用就是在用戶在下拉列表中選擇了后,提交包含“reqeust_locale”變量的表單到Action。在打開頁面時,為了下拉列表的選中的當前區域,我們需要到session取得當前區域(鍵為“WW_TRANS_I18N_LOCALE”的屬性),而該屬性在沒有設置語言前是為空的,所以通過值棧中locale屬性來取得當前區域(用戶瀏覽器所設置的語言)。

你可以把LangSelector.jsp作為一個控件使用,方法是在JSP頁面中把它包含進來,代碼如下所示:
<s:include value="/LangSelector.jsp"/>

在例1中的HellloWorld.jsp中<body>后加入上述代碼,并在struts.xml中新建Action,代碼如下:
<action name="HelloWorld">
   
<result>/HelloWorld.jsp</result>
</action>

或者,如果你多個JSP需要實現上述功能,你可以使用下面的通用配置,而不是為每一個JSP頁面都新建一個Action。
<action name="*">
   
<result>/{1}.jsp</result>
</action>

分布運行程序,在瀏覽器的地址欄中輸入http://localhost:8080/Struts2_i18n/HelloWorld.action,出現圖4所示頁面:
圖3 HelloWorld.action
圖3 HelloWorld.action

在下拉列表中,選擇“American English”,出現圖5所示頁面:
圖3 HelloWorld.action
圖4 HelloWorld.action
可能大家會問為什么一定要通過Action來訪問頁面呢?
你可以試一下不用Action而直接用JSP的地址來訪問頁面,結果會是無論你在下拉列表中選擇什么,語言都不會改變。這表示不能正常運行的。其原因為如果直接使用JSP訪問頁面,Struts 2.0在web.xml的配置的過濾器(Filter)就不會工作,所以攔截器鏈也不會工作。


fd 2009-03-06 15:26 發表評論
]]>
Struts 2.0的Action講解 http://www.tkk7.com/feicer/articles/258213.htmlfdfdFri, 06 Mar 2009 07:17:00 GMThttp://www.tkk7.com/feicer/articles/258213.htmlhttp://www.tkk7.com/feicer/comments/258213.htmlhttp://www.tkk7.com/feicer/articles/258213.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258213.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258213.html閱讀全文

fd 2009-03-06 15:17 發表評論
]]>
常用的Struts 2.0的標志(Tag)介紹http://www.tkk7.com/feicer/articles/258208.htmlfdfdFri, 06 Mar 2009 07:13:00 GMThttp://www.tkk7.com/feicer/articles/258208.htmlhttp://www.tkk7.com/feicer/comments/258208.htmlhttp://www.tkk7.com/feicer/articles/258208.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258208.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258208.html閱讀全文

fd 2009-03-06 15:13 發表評論
]]>
為Struts 2.0做好準備 http://www.tkk7.com/feicer/articles/258205.htmlfdfdFri, 06 Mar 2009 06:52:00 GMThttp://www.tkk7.com/feicer/articles/258205.htmlhttp://www.tkk7.com/feicer/comments/258205.htmlhttp://www.tkk7.com/feicer/articles/258205.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258205.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258205.html閱讀全文

fd 2009-03-06 14:52 發表評論
]]>
Struts2 的 struts.xml 配置中 namespace 的使用 http://www.tkk7.com/feicer/articles/258036.htmlfdfdThu, 05 Mar 2009 09:25:00 GMThttp://www.tkk7.com/feicer/articles/258036.htmlhttp://www.tkk7.com/feicer/comments/258036.htmlhttp://www.tkk7.com/feicer/articles/258036.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258036.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258036.html
<package namespace="/secure"   ....>
    ......
</package>

如果沒有指定 namespace 屬性,默認 namespace 是 ""。使用 namespace 可以方便于按不同目的規劃對應用的訪問規則。比如不同 namespace 下配置了不同的攔截器就可以實現權限的控制,如 "/secure" 下已登陸用戶才能訪問,"/public" 下可公開訪問的。

配置了 namespace 直接就是反應在訪問 URL 上,例如 namespace="/secure"  name="test" 的 action

 <package namespace="/secure"   ....>
       <action name="test"  ....
</package>

訪問它的 URL 就是 http://ip:port/context/secure/test.action,那如果在 namespace "/secure" 下沒有 test action 會出現什么情況呢?Struts 還會嘗試在默認 namespace,即 "" 下找 test。

再舉個例子,URL 是 http://ip:port/context/some/path/test.action 時,如果在 "/some/path" namespace 下找不到 test action,也是到 "" (default namespace) 下找 test action,但不會去 "/some" 下找的。

用標簽 <s:url value="/secure/test.action"/>  對應頁面源文件是 /context/secure/test.action

稍有麻的就是 <s:form action="/secure/test.action" .... 對應的源文件是 <form action="/context/secure/test.action" ...

但是后臺會有警告:

警告: No configuration found for the specified action: '/secure/test.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

Struts2 把 action 屬性值當整一個 Action Name 了,但這也不影響使用,這個 URL 正好能與 (package namespace) + (action name) 合上拍。

但是對于使用了動態方法調用(struts.enable.DynamicMethodInvocation = true)就沒這么幸運了。很容易想當然的
 
<s:form action="/secure/test!update.action" ....  生成的 HTML 源文件卻是 action="/TestStruts2/om/test"

同時后臺的警告信息是:

警告: No configuration found for the specified action: '/secure/test' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

很顯然對于這個 action="/TestStruts2/om/test",提交時是會得到 HTTP Status 404 - /context/secure/test  錯誤。

正確的用法是 <s:action...> 也有一個 namespace 屬性,對了,就是

<s:form namespace="/secure" action="test!login">  生成的 HTML 源文件是:<form action="/TestStruts2/om/test!login.action" ....>

我們要的就是這個。

如果不配置 namespace 屬性,我們能不能在訪問 action 時也用上目錄層次呢?可以,那是在 struts1 習慣的做法,配置 <action name="secure/test" ....> name 中使用斜杠,但在 Struts2 中 Action Name 中使用斜杠需要設置

struts.enable.SlashesInActionNames=true                      默認為 false

可是 Struts2 大概不贊同這種做法,力挺 namespace 的作用。

對于上面使用了斜框的 Action Name,<s:form 中的寫法要用

<s:form action="secure/test">                 生成 HTML 源文件:<form action="/context/secure/test.action" .....

<s:form action="secure/test!update">            生成 HTML 源文件:<form action="/context/secure/test!login.action" .....



上面的 action 后加不加 .action 無所謂,只是要保證 <s:form>  的 action 屬性一定要與 struts.xml 中的 <action> 的 name 匹配上,如果你自作多情的在前面加個斜杠,如寫成了
 
<s:form action="/secure/test!update"> 、 <s:form action="/secure/test">  或者 <s:form action="/secure/test!update.action">   生成的 HTML 源文件就都成了:<form action="/context/secure/test" .....

這也是從 Struts1 帶來的弊病,因為 Struts1 中 <html:form> action 屬性對應的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 屬性對應的是 <action> 的 name;name 要完全匹配,path 可以加些層次。


fd 2009-03-05 17:25 發表評論
]]>
hibernate-3.3.1GA包結構解釋http://www.tkk7.com/feicer/articles/hibernate_3_3_1GA_package.htmlfdfdThu, 05 Mar 2009 07:53:00 GMThttp://www.tkk7.com/feicer/articles/hibernate_3_3_1GA_package.htmlhttp://www.tkk7.com/feicer/comments/258021.htmlhttp://www.tkk7.com/feicer/articles/hibernate_3_3_1GA_package.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258021.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258021.htmlhibernate3.3.1GA
目錄結構
hibernate-distribution-3.3.1.GA
---hibernate3.jar---lib//Hibernate的庫,沒有什么可說的,必須使用的jar包

------bytecode
------optional
------required
---------antlr-2.7.6.jar//個人認為此包是對HQL的支持
---------commons-collections-3.1.jar//Apache Commons包中的一個,包含了一些Apache開發的集合類,功能比java.util.*強大。必須使用的jar包。
---------dom4j-1.6.1.jar//dom4j是一個Java的XML API,類似于jdom,用來讀寫XML文件的。dom4j是一個非常非常優秀的Java XML API,具有性能優異、功能強大和極端易用使用的特點,同時它也是一個開放源代碼的軟件,可以在SourceForge上找到它。在IBM developerWorks上面可以找到一篇文章,對主流的Java XML API進行的性能、功能和易用性的評測,dom4j無論在那個方面都是非常出色的。我早在將近兩年之前就開始使用dom4j,直到現在。如今你可以看到越來越多的Java軟件都在使用dom4j來讀寫XML,特別值得一提的是連Sun的JAXM也在用dom4j。這是必須使用的jar包,Hibernate用它來讀寫配置文件。

---------javassist-3.4.GA.jar//JAAS是用來進行權限驗證的,已經包含在JDK1.4里面了。所以實際上是多余的包
---------jta-1.1.jar//JTA規范,當Hibernate使用JTA的時候需要,不過App Server都會帶上,所以也是多余的
---------slf4j-api-1.5.2.jar//一種日志插件,事后證明需要slf4j-api-1.5.6.jar 和slf4j-nop-1.5.6.jar兩個包配合(版本一致)
---documentation
---project

此為下載下來的hibernate3.3.1 解壓后在required 文件夾中所帶的包。
本人導入這些包以后,提示:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".  
 很明顯是相關的jar包沒有被引入,而且“hibernate-distribution-3.3.1.GA-dist.zip”中壓根就沒有org/slf4j/impl/StaticLoggerBinder類的存在,還需另外下載slf4j-nop-1.5.2.jar引入到工程中才行。

本人就下載了 slf4j-api-1.5.6.jar 和slf4j-nop-1.5.6.jar ,導入以后問題解決



fd 2009-03-05 15:53 發表評論
]]>
struts2 所需jar包詳解http://www.tkk7.com/feicer/articles/struts2_jar.htmlfdfdThu, 05 Mar 2009 07:36:00 GMThttp://www.tkk7.com/feicer/articles/struts2_jar.htmlhttp://www.tkk7.com/feicer/comments/258018.htmlhttp://www.tkk7.com/feicer/articles/struts2_jar.html#Feedback1http://www.tkk7.com/feicer/comments/commentRss/258018.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258018.html閱讀全文

fd 2009-03-05 15:36 發表評論
]]>
hibernate 3.* C3P0配置http://www.tkk7.com/feicer/articles/C3P0_config.htmlfdfdThu, 05 Mar 2009 07:13:00 GMThttp://www.tkk7.com/feicer/articles/C3P0_config.htmlhttp://www.tkk7.com/feicer/comments/258009.htmlhttp://www.tkk7.com/feicer/articles/C3P0_config.html#Feedback1http://www.tkk7.com/feicer/comments/commentRss/258009.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258009.html        Hibernate自帶的連接池算法相當不成熟。 它只是為了讓你快些上手,并不適合用于產品系統或性能測試中。 出于最佳性能和穩定性考慮你應該使用第三方的連接池。只需要用特定連接池的設置替換 hibernate.connection.pool_size即可。這將關閉Hibernate自帶的連接池。 例如, 你可能會想用C3P0.
        C3P0是一個隨Hibernate一同分發的開源的JDBC連接池, 它位于lib目錄下。 如果你設置了hibernate.c3p0.*相關的屬性, Hibernate將使用 C3P0ConnectionProvider來緩存JDBC連接。 如果你更原意使用Proxool, 請參考發 行包中的hibernate.properties并到Hibernate網站獲取更多的信息。

        這是一個使用C3P0的hibernate.properties樣例文件(來自Hibernate包中etc目錄下):

###########################
### C3P0 Connection Pool###
###########################

#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
 


        在hibernate.cfg.xml文件里面加入如下的配置:

        <!-- 最大連接數 -->
        <property name="hibernate.c3p0.max_size">20</property>

        <!-- 最小連接數 -->
        <property name="hibernate.c3p0.min_size">5</property>

        <!-- 獲得連接的超時時間,如果超過這個時間,會拋出異常,單位毫秒 ***-->
        <property name="hibernate.c3p0.timeout">120</property>

        <!-- 最大的PreparedStatement的數量 -->
        <property name="hibernate.c3p0.max_statements">100</property>

        <!-- 每隔120秒檢查連接池里的空閑連接 ,單位是秒-->
        <property name="hibernate.c3p0.idle_test_period">120</property>

        <!-- 當連接池里面的連接用完的時候,C3P0一下獲取的新的連接數 -->
        <property name="hibernate.c3p0.acquire_increment">2</property>

        <!-- 每次都驗證連接是否可用 -->
        <property name="hibernate.c3p0.validate">true</property>
 


        完整示例如下(hibernate.properties):

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase

hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
 

 


 



fd 2009-03-05 15:13 發表評論
]]>
hibernate-distribution-3.3.1.GA 部署slf4j-api-1.5.6.jar 包導入http://www.tkk7.com/feicer/articles/slf4j.htmlfdfdThu, 05 Mar 2009 06:37:00 GMThttp://www.tkk7.com/feicer/articles/slf4j.htmlhttp://www.tkk7.com/feicer/comments/258001.htmlhttp://www.tkk7.com/feicer/articles/slf4j.html#Feedback0http://www.tkk7.com/feicer/comments/commentRss/258001.htmlhttp://www.tkk7.com/feicer/services/trackbacks/258001.html閱讀全文

fd 2009-03-05 14:37 發表評論
]]>
主站蜘蛛池模板: 91视频免费观看| 美女18毛片免费视频| 日本一道高清不卡免费| 亚洲视频手机在线| 老司机福利在线免费观看| 日韩免费高清视频| 亚洲国产成人久久综合| 女同免费毛片在线播放| 精品国产综合成人亚洲区| 99精品全国免费观看视频..| 亚洲国产成人精品无码区在线观看| 久久国产乱子伦精品免费午夜| 亚洲情XO亚洲色XO无码| 久久aⅴ免费观看| 免费一级成人毛片| 亚洲精品视频免费观看| 午夜免费福利影院| 国内成人精品亚洲日本语音| 亚洲日本韩国在线| 日韩视频免费在线观看| 亚洲精品无码久久久久AV麻豆| 特级做a爰片毛片免费看| 亚洲AV综合色一区二区三区| 国产精品亚洲а∨无码播放麻豆| 国产一区二区三区在线免费观看| 国产大片免费天天看| 亚洲午夜久久久精品影院| 青春禁区视频在线观看直播免费| 亚洲国产精品成人午夜在线观看 | tom影院亚洲国产一区二区| 日本一道高清不卡免费| 色播在线永久免费视频网站| 亚洲成AV人综合在线观看| 国产又黄又爽又猛的免费视频播放 | 美女视频黄a视频全免费网站色| 国产亚洲精品无码专区| 亚洲免费福利在线视频| 有码人妻在线免费看片| 亚洲欧洲精品一区二区三区| 在线观看黄片免费入口不卡| 亚洲国产成人精品无码区在线秒播|