国产亚洲精品xxx,亚洲精品V欧洲精品V日韩精品 ,亚洲av日韩av欧v在线天堂http://www.tkk7.com/feicer/category/38112.htmljava hibernate struts2 springzh-cnWed, 18 Nov 2009 07:48:52 GMTWed, 18 Nov 2009 07:48:52 GMT60Struts2的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 發(fā)表評(píng)論
]]>
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 發(fā)表評(píng)論
]]>
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 發(fā)表評(píng)論
]]>
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 發(fā)表評(píng)論
]]>
Strus 2的新表單標(biāo)志的使用 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為大家提供了不少常用的很酷的表單標(biāo)志,簡(jiǎn)化了我們程序員的工作。不過(guò),由于這些都是新標(biāo)志,大家可能在使用上還存在不少疑問(wèn)。本文將就朋友們的回復(fù)、留言或Email上的問(wèn)題,分別對(duì)這些酷標(biāo)志進(jìn)行講述。

表單標(biāo)志使用小技巧

Struts 2的表單標(biāo)志在輸出(render)HTML時(shí),使用了模板的概念,增加了復(fù)雜性(因?yàn)樗幌馭truts 1.x的表單標(biāo)志,它通常都是一個(gè)標(biāo)志對(duì)應(yīng)HTML的一個(gè)元素),因此大家在使用時(shí),需要一些技巧:

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

下面我將分別對(duì)這些標(biāo)志進(jìn)行講述:

1、<s:checkboxlist />

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

答案其實(shí)很簡(jiǎn)單,只需要將其“value”屬性設(shè)為你的要選中的值,如以代碼所示:

<%@ 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

分布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/checkboxlist.jsp,出現(xiàn)如下圖所示頁(yè)面:

checkboxlist.jsp頁(yè)面
清單2 checkboxlist.jsp頁(yè)面

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 />

很多朋友問(wèn):“上面的‘list’屬性只有兩個(gè)值,如果我有三個(gè)或更多的值,‘doublelist’屬性應(yīng)該如何設(shè)定呢?”

我建議的做法是先定義一個(gè)Map類(lèi)型的對(duì)象,鍵為“list”的集合,值則為“doubleList”的集合,然后“doubleList”的OGNL寫(xiě)成“#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

分布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/doubleselect.action,出現(xiàn)如下圖所示頁(yè)面:

doubleselect.jsp頁(yè)面
清單5 doubleselect.jsp頁(yè)面

3、<s: token />

這個(gè)標(biāo)志可能大家不常用,不過(guò)本人認(rèn)為它還是挺有用的。在使用Struts 1.x時(shí),因?yàn)樘D(zhuǎn)通常是用Forward(而不是Redirect)實(shí)現(xiàn)的,所以當(dāng)用戶(hù)完成請(qǐng)求后,按“F5”刷新頁(yè)面時(shí),就會(huì)重新提交上次的請(qǐng)求,這樣經(jīng)常會(huì)出錯(cuò)。要解決這個(gè)問(wèn)題,<s:token />可以幫你忙。

實(shí)現(xiàn)原理

在頁(yè)面加載時(shí),<s: token />產(chǎn)生一個(gè)GUID(Globally Unique Identifier,全局唯一標(biāo)識(shí)符)值的隱藏輸入框如:

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

同時(shí),將GUID放到會(huì)話(huà)(session)中;在執(zhí)行action之前,“token”攔截器將會(huì)話(huà)token與請(qǐng)求token比較,如果兩者相同,則將會(huì)話(huà)中的token刪除并往下執(zhí)行,否則向actionErrors加入錯(cuò)誤信息。如此一來(lái),如果用戶(hù)通過(guò)某種手段提交了兩次相同的請(qǐng)求,兩個(gè)token就會(huì)不同。

具體實(shí)現(xiàn)

首先看一下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的寫(xiě)法:

%@ 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也很簡(jiǎn)單,就是加入<s:token />標(biāo)志。接下來(lái)是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”結(jié)果,因?yàn)?#8220;token”攔截器在會(huì)話(huà)token與請(qǐng)求token不一致時(shí),將會(huì)直接返回“invalid.token”結(jié)果。

發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/token.jsp,出現(xiàn)如下圖所示頁(yè)面:

正常顯示的token.jsp頁(yè)面
清單10 正常顯示的token.jsp頁(yè)面

隨便填點(diǎn)東西并提交頁(yè)面,一切正常返回以上頁(yè)面,然后按“F5”刷新頁(yè)面,在彈出的對(duì)話(huà)框中點(diǎn)擊“Retry”,出現(xiàn)如下圖所示頁(yè)面:

重復(fù)提交出錯(cuò)顯示
清單11 重復(fù)提交出錯(cuò)顯示

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

這幾個(gè)標(biāo)志的使用相對(duì)簡(jiǎn)單,所以我想小舉一例即可,以下是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頁(yè)面

發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入:http://localhost:8080/Struts2_CoolTags/others.jsp,出現(xiàn)如下圖所示頁(yè)面:

點(diǎn)擊查看大圖
清單13 其它表單標(biāo)志頁(yè)面

總結(jié)

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



fd 2009-03-06 16:34 發(fā)表評(píng)論
]]>
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 發(fā)表評(píng)論
]]>
在Struts 2中實(shí)現(xiàn)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 發(fā)表評(píng)論
]]>
在Struts 2中實(shí)現(xiàn)文件上傳 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 發(fā)表評(píng)論
]]>
在Struts 2中實(shí)現(xiàn)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,以下譯為控制反轉(zhuǎn))隨著Java社區(qū)中輕量級(jí)容器(Lightweight Contianer)的推廣而越來(lái)越為大家耳熟能詳。在此,我不想再多費(fèi)唇舌來(lái)解釋“什么是控制反轉(zhuǎn)”和“為什么需要控制反轉(zhuǎn)”。因?yàn)榛ヂ?lián)網(wǎng)上已經(jīng)有非常多的文章對(duì)諸如此類(lèi)的問(wèn)題作了精彩而準(zhǔn)確的回答。大家可以去讀一下Rod Johnson和Juergen Hoeller合著的《Expert one-on-one J2EE Development without EJB》或Martin Fowler所寫(xiě)的《Inversion of Control Containers and the Dependency Injection pattern》。

言歸正傳,本文的目的主要是介紹在Struts 2中實(shí)現(xiàn)控制反轉(zhuǎn)。

歷史背景

眾所周知,Struts 2是以Webwork 2作為基礎(chǔ)發(fā)展出來(lái)。而在Webwork 2.2之前的Webwork版本,其自身有一套控制反轉(zhuǎn)的實(shí)現(xiàn),Webwork 2.2在Spring 框架的如火如荼發(fā)展的背景下,決定放棄控制反轉(zhuǎn)功能的開(kāi)發(fā),轉(zhuǎn)由Spring實(shí)現(xiàn)。值得一提的是,Spring確實(shí)是一個(gè)值得學(xué)習(xí)的框架,因?yàn)橛性絹?lái)越多的開(kāi)源組件(如iBATIS等)都放棄與Spring重疊的功能的開(kāi)發(fā)。因此,Struts 2推薦大家通過(guò)Spring實(shí)現(xiàn)控制反轉(zhuǎn)。

具體實(shí)現(xiàn)

首先,在開(kāi)發(fā)環(huán)境中配置好Struts 2的工程。對(duì)這部分仍然有問(wèn)題的朋友,請(qǐng)參考我的早前的文章。

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

圖1 所依賴(lài)的Spring的jar包
圖1 所依賴(lài)的Spring的jar包

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

接下來(lái),就要修改WEB-INF\web.xml文件了,內(nèi)容為:

<? 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監(jiān)聽(tīng)器,方便Spring與Web容器交互。

緊接著,修改Struts.properties文件,告知Struts 2運(yùn)行時(shí)使用Spring來(lái)創(chuàng)建對(duì)象(如Action等),內(nèi)容如下:

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

再下來(lái),遵循Spring的原則——面向接口編程,創(chuàng)建接口ChatService,代碼如下:

package tutorial;

import java.util.Set;

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

然后,再創(chuàng)建一個(gè)默認(rèn)實(shí)現(xiàn)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

接下來(lái),就該新建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類(lèi)使用屬性(Getter/Setter)注入法取得ChatService對(duì)象。

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

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

上述代碼有二點(diǎn)值得大家注意的:

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

接下來(lái),在classes/struts.xml中配置Action,內(nèi)容如下:

<! 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屬性,它對(duì)應(yīng)于Spring所定義的bean的id,而不是它的類(lèi)全名。

最后,讓我們看看/UserList.jsp,內(nèi)容如下:

<% @ 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

大功告成,分布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入http://localhost:8080/Struts2_IoC/Chat.action,出現(xiàn)如圖2所示頁(yè)面:

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

總結(jié)

通過(guò)Spring在Struts 2上實(shí)現(xiàn)控制反轉(zhuǎn)是強(qiáng)烈推薦的做法,當(dāng)然您也可以組合其它的實(shí)現(xiàn)(如Pico等)。



fd 2009-03-06 16:03 發(fā)表評(píng)論
]]>
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 發(fā)表評(píng)論
]]>
在Struts 2.0中實(shí)現(xiàn)表單數(shù)據(jù)校驗(yàn)(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 發(fā)表評(píng)論
]]>
轉(zhuǎn)換器(Converter)——Struts 2.0中的魔術(shù)師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 發(fā)表評(píng)論
]]>
在Struts 2.0中國(guó)際化(i18n)您的應(yīng)用程序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中國(guó)際化(i18n)您的應(yīng)用程序

國(guó)際化是商業(yè)系統(tǒng)中不可或缺的一部分,所以無(wú)論您學(xué)習(xí)的是什么Web框架,它都是必須掌握的技能。

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

不過(guò),所謂“沒(méi)有最好,只有更好”。Struts 2.0并沒(méi)有在這部分止步,而是在原有的簡(jiǎn)單易用的基礎(chǔ)上,將其做得更靈活、更強(qiáng)大。

國(guó)際化Hello World

下面讓我們看一個(gè)例子——HelloWorld。這個(gè)例子演示如何根據(jù)用戶(hù)瀏覽器的設(shè)置輸出相應(yīng)的HelloWorld。

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

資源文件查找順序

之所以說(shuō)Struts 2.0的國(guó)際化更靈活是因?yàn)樗梢阅芨鶕?jù)不同需要配置和獲取資源(properties)文件。在Struts 2.0中有下面幾種方法:

  1. 使用全局的資源文件,方法如上例所示。這適用于遍布于整個(gè)應(yīng)用程序的國(guó)際化字符串,它們?cè)诓煌陌╬ackage)中被引用,如一些比較共用的出錯(cuò)提示;
  2. 使用包范圍內(nèi)的資源文件。做法是在包的根目錄下新建名的package.properties和package_xx_XX.properties文件。這就適用于在包中不同類(lèi)訪(fǎng)問(wèn)的資源;
  3. 使用Action范圍的資源文件。做法為Action的包下新建文件名(除文件擴(kuò)展名外)與Action類(lèi)名同樣的資源文件。它只能在該Action中訪(fǎng)問(wèn)。如此一來(lái),我們就可以在不同的Action里使用相同的properties名表示不同的值。例如,在A(yíng)ctonOne中title為“動(dòng)作一”,而同樣用title在A(yíng)ctionTwo表示“動(dòng)作二”,節(jié)省一些命名工夫;
  4. 使用<s:i18n>標(biāo)志訪(fǎng)問(wèn)特定路徑的properties文件。使用方法請(qǐng)參考我早前的文章《常用的Struts 2.0的標(biāo)志(Tag)介紹》。在您使用這一方法時(shí),請(qǐng)注意<s:i18n>標(biāo)志的范圍。在<s:i18n name="xxxxx">到</s:i18n>之間,所有的國(guó)際化字符串都會(huì)在名為xxxxx資源文件查找,如果找不到,Struts 2.0就會(huì)輸出默認(rèn)值(國(guó)際化字符串的名字)。

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

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

假設(shè)我們?cè)谀硞€(gè)ChildAction中調(diào)用了getText("user.title"),Struts 2.0的將會(huì)執(zhí)行以下的操作:

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

參數(shù)化國(guó)際化字符串

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

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

讓用戶(hù)方便地選擇語(yǔ)言

開(kāi)發(fā)國(guó)際化的應(yīng)用程序時(shí),有一個(gè)功能是必不可少的——讓用戶(hù)快捷地選擇或切換語(yǔ)言。在Struts 2.0中,通過(guò)ActionContext.getContext().setLocale(Locale arg)可以設(shè)置用戶(hù)的默認(rèn)語(yǔ)言。不過(guò),由于這是一個(gè)比較普遍的應(yīng)用場(chǎng)景(Scenario),所以Struts 2.0為您提供了一個(gè)名i18n的攔截器(Interceptor),并在默認(rèn)情況下將其注冊(cè)到攔截器鏈(Interceptor chain)中。它的原理為在執(zhí)行Action方法前,i18n攔截器查找請(qǐng)求中的一個(gè)名為"request_locale"的參數(shù)。如果其存在,攔截器就將其作為參數(shù)實(shí)例化Locale對(duì)象,并將其設(shè)為用戶(hù)默認(rèn)的區(qū)域(Locale),最后,將此Locale對(duì)象保存在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先實(shí)例化一個(gè)Locales對(duì)象,并把對(duì)象的Map類(lèi)型的屬性locales賦予下拉列表(select) 。如此一來(lái),下拉列表就獲得可用語(yǔ)言的列表。大家看到LangSelector有<s:form>標(biāo)志和一段Javascript腳本,它們的作用就是在用戶(hù)在下拉列表中選擇了后,提交包含“reqeust_locale”變量的表單到Action。在打開(kāi)頁(yè)面時(shí),為了下拉列表的選中的當(dāng)前區(qū)域,我們需要到session取得當(dāng)前區(qū)域(鍵為“WW_TRANS_I18N_LOCALE”的屬性),而該屬性在沒(méi)有設(shè)置語(yǔ)言前是為空的,所以通過(guò)值棧中l(wèi)ocale屬性來(lái)取得當(dāng)前區(qū)域(用戶(hù)瀏覽器所設(shè)置的語(yǔ)言)。

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

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

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

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

在下拉列表中,選擇“American English”,出現(xiàn)圖5所示頁(yè)面:
圖3 HelloWorld.action
圖4 HelloWorld.action
可能大家會(huì)問(wèn)為什么一定要通過(guò)Action來(lái)訪(fǎng)問(wèn)頁(yè)面呢?
你可以試一下不用Action而直接用JSP的地址來(lái)訪(fǎng)問(wèn)頁(yè)面,結(jié)果會(huì)是無(wú)論你在下拉列表中選擇什么,語(yǔ)言都不會(huì)改變。這表示不能正常運(yùn)行的。其原因?yàn)槿绻苯邮褂肑SP訪(fǎng)問(wèn)頁(yè)面,Struts 2.0在web.xml的配置的過(guò)濾器(Filter)就不會(huì)工作,所以攔截器鏈也不會(huì)工作。


fd 2009-03-06 15:26 發(fā)表評(píng)論
]]>
常用的Struts 2.0的標(biāo)志(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 發(fā)表評(píng)論
]]>
為Struts 2.0做好準(zhǔn)備 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 發(fā)表評(píng)論
]]>
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>

如果沒(méi)有指定 namespace 屬性,默認(rèn) namespace 是 ""。使用 namespace 可以方便于按不同目的規(guī)劃對(duì)應(yīng)用的訪(fǎng)問(wèn)規(guī)則。比如不同 namespace 下配置了不同的攔截器就可以實(shí)現(xiàn)權(quán)限的控制,如 "/secure" 下已登陸用戶(hù)才能訪(fǎng)問(wèn),"/public" 下可公開(kāi)訪(fǎng)問(wèn)的。

配置了 namespace 直接就是反應(yīng)在訪(fǎng)問(wèn) URL 上,例如 namespace="/secure"  name="test" 的 action

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

訪(fǎng)問(wèn)它的 URL 就是 http://ip:port/context/secure/test.action,那如果在 namespace "/secure" 下沒(méi)有 test action 會(huì)出現(xiàn)什么情況呢?Struts 還會(huì)嘗試在默認(rèn) namespace,即 "" 下找 test。

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

用標(biāo)簽 <s:url value="/secure/test.action"/>  對(duì)應(yīng)頁(yè)面源文件是 /context/secure/test.action

稍有麻的就是 <s:form action="/secure/test.action" .... 對(duì)應(yīng)的源文件是 <form action="/context/secure/test.action" ...

但是后臺(tái)會(huì)有警告:

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

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

但是對(duì)于使用了動(dòng)態(tài)方法調(diào)用(struts.enable.DynamicMethodInvocation = true)就沒(méi)這么幸運(yùn)了。很容易想當(dāng)然的
 
<s:form action="/secure/test!update.action" ....  生成的 HTML 源文件卻是 action="/TestStruts2/om/test"

同時(shí)后臺(tái)的警告信息是:

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

很顯然對(duì)于這個(gè) action="/TestStruts2/om/test",提交時(shí)是會(huì)得到 HTTP Status 404 - /context/secure/test  錯(cuò)誤。

正確的用法是 <s:action...> 也有一個(gè) namespace 屬性,對(duì)了,就是

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

我們要的就是這個(gè)。

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

struts.enable.SlashesInActionNames=true                      默認(rèn)為 false

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

對(duì)于上面使用了斜框的 Action Name,<s:form 中的寫(xiě)法要用

<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 無(wú)所謂,只是要保證 <s:form>  的 action 屬性一定要與 struts.xml 中的 <action> 的 name 匹配上,如果你自作多情的在前面加個(gè)斜杠,如寫(xiě)成了
 
<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 帶來(lái)的弊病,因?yàn)?Struts1 中 <html:form> action 屬性對(duì)應(yīng)的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 屬性對(duì)應(yīng)的是 <action> 的 name;name 要完全匹配,path 可以加些層次。


fd 2009-03-05 17:25 發(fā)表評(píng)論
]]>
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 發(fā)表評(píng)論
]]>
主站蜘蛛池模板: 黄色毛片免费网站| 亚洲午夜无码毛片av久久京东热| 波多野结衣免费在线观看| 久久亚洲国产欧洲精品一| AAAAA级少妇高潮大片免费看| 亚洲成a人片在线观看久| 亚洲av无码乱码国产精品| 亚洲综合色婷婷在线观看| 毛片免费视频播放| 亚洲一线产品二线产品| 国产精品免费视频播放器| 亚洲网站在线观看| 搜日本一区二区三区免费高清视频 | 免费人成网站在线观看10分钟| 国产一区二区三区无码免费| 亚洲6080yy久久无码产自国产| 亚洲av无码成人精品区在线播放| 一级毛片大全免费播放| 成年女人看片免费视频播放器| 亚洲成在人线在线播放无码| 亚洲精品成a人在线观看| 亚洲精品动漫免费二区| 亚洲国产成人a精品不卡在线| 男女拍拍拍免费视频网站| 亚洲天天在线日亚洲洲精| 成人免费毛片内射美女APP| 免费精品国自产拍在线播放| 欧美大尺寸SUV免费| 真正全免费视频a毛片| 九月丁香婷婷亚洲综合色| 免费视频爱爱太爽了| 久久久久久久亚洲Av无码 | 国产精品久久久久影院免费| 久久久精品视频免费观看| 亚洲最大在线观看| 久久国产精品一区免费下载| 亚洲一区中文字幕在线电影网| 免费又黄又爽的视频| 免费一级做a爰片久久毛片潮| 亚洲Av永久无码精品三区在线| 成人免费视频一区|