我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          Struts2中的鏈接標(biāo)簽

          為了使從一個(gè)頁(yè)面中鏈接一個(gè)動(dòng)態(tài)數(shù)據(jù)變得簡(jiǎn)單,Struts2框架提供了一系列的標(biāo)簽。
          Struts2標(biāo)簽的一種用法是創(chuàng)建鏈接到其他Web資源,特別是針對(duì)那些在本地應(yīng)用中的資源。
           
          1.普通鏈接
          Web程序中最普通的應(yīng)用是鏈接到其他頁(yè)面,下面看Welcome.jsp。
          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <html>
          <head>
              
          <title>Welcome</title>
              
          <link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet"
                    type="text/css"/>
          </head>
           
          <body>
          <h3>Commands</h3>
          <ul>
              
          <li><href="<s:url action="Login_input"/>">Sign On</a></li>
              
          <li><href="<s:url action="Register"/>">Register</a></li>
          </ul>
           
          </body>
          </html>

          1.1說明
          1.<%@ taglib prefix="s" uri="/struts-tags" %>
          此句表示導(dǎo)入struts標(biāo)簽,并以s為前綴。即以s為前綴的標(biāo)簽均來自struts標(biāo)簽庫(kù)。
           
          2.<link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet" type="text/css"/>
          此句表示利用url標(biāo)簽導(dǎo)入一個(gè)路徑,鏈接到一個(gè)文件,注意此路徑為項(xiàng)目下的絕對(duì)路徑。
           
          3.<a href="<s:url action="Login_input"/>">Sign On</a>
          此句表示利用url標(biāo)簽鏈接到一個(gè)action。
           
          1.2注冊(cè)action
          我們?cè)趕truts.xml中注冊(cè)一個(gè)action來顯示welcome.jsp。

          <action name="Welcome">
                 
          <result>/example/Welcome.jsp</result>
          </action> 

           

          注意此action注冊(cè)在package example下,所以在地址欄中敲入http://localhost:8080/StrutsHelloWorld/example/Welcome.action(StrutsHelloWorld是project名),會(huì)導(dǎo)向到Welcome.jsp。
           
          2.使用通配符
          對(duì)于上面的action注冊(cè),我們也可以用下面的語句代替。

          <action name="*">
                 
          <result>/example/{1}.jsp</result>
          </action>

          此句的意思是,如果在沒有找到匹配的action名稱的情況下,默認(rèn)調(diào)用action名稱.jsp。第一句中星號(hào)指任意,而第二句中{1}指代第一句中星號(hào)指代的內(nèi)容。

              舉個(gè)例子,如果在地址欄中敲入http://localhost:8080/StrutsHelloWorld/example/1.action,則系統(tǒng)查找struts.xml,發(fā)現(xiàn)沒有name為1的action,即最后調(diào)用name為星號(hào)的這個(gè)action,根據(jù)此action,將輸出/example/1.jsp。


          或者讀者可以直接點(diǎn)擊Welcome.jsp中的兩個(gè)超鏈接,系統(tǒng)將會(huì)報(bào)錯(cuò)找不到Login_input.jsp和Register.jsp。因?yàn)檫@兩個(gè)action還沒有注冊(cè),也沒有相應(yīng)的jsp文件。
           
          3.帶參數(shù)的鏈接
          超鏈接后面帶有參數(shù)大家不會(huì)陌生,諸如http://www.apache.com/?language=ch。這個(gè)鏈接后面帶有一個(gè)language參數(shù),其值為ch。你可以通過request.getParameter(“language”)找到參數(shù)值。下面演示在struts2中如何設(shè)置帶參數(shù)的鏈接。看HelloWorld.jsp。

          <%@ taglib prefix="s" uri="/struts-tags"%>
           
          <html>
          <head>
          <title>Hello World!</title>
          </head>
          <body>
          <h2><s:property value="message" /></h2>
          <h3>Languages</h3>
          <ul>
                 
          <li>
                   
          <s:url id="url" action="HelloWorld">
                        
          <s:param name="request_locale">en</s:param>
                   
          </s:url>
                   
          <s:a href="%{url}">English</s:a>
                 
          </li>
                 
          <li>
                   
          <s:url id="url" action="HelloWorld">
                        
          <s:param name="request_locale">es</s:param>
                   
          </s:url>
                   
          <s:a href="%{url}">Espanol</s:a>
                 
          </li>
          </ul>
           
          </body>
          </html>
          3.1說明
          1.<s:url id="url" action="HelloWorld">
                        <s:param name="request_locale">en</s:param>
          </s:url>
          此段表示設(shè)置一個(gè)url標(biāo)簽指向名為HelloWorld的action,此標(biāo)簽帶一個(gè)id取名為url,后面會(huì)用到。帶一個(gè)參數(shù)request_locale,其值為en。
           
          2.<s:a href="%{url}">English</s:a>
          此句用到了struts2的超鏈接標(biāo)簽,連接的地址即為1中url,點(diǎn)擊English,發(fā)出的信息為:http://localhost:8080/StrutsHelloWorld/example/HelloWorld.action?request_locale=en
           
          3.2注冊(cè)action到struts.xml
          <struts>
                 
          <package name="example" namespace="/example"
                        extends
          ="struts-default">
                        
          <action name="HelloWorld" >
                               
          <result>/example/HelloWorld.jsp</result>
                        
          </action>



          posted on 2007-11-26 23:52 々上善若水々 閱讀(2218) 評(píng)論(0)  編輯  收藏 所屬分類: Struts2

          主站蜘蛛池模板: 北川| 行唐县| 文成县| 车致| 读书| 十堰市| 荣成市| 建平县| 安福县| 丹棱县| 禄丰县| 泸西县| 横山县| 武隆县| 青海省| 辉南县| 临西县| 图木舒克市| 庆阳市| 思南县| 虞城县| 沅江市| 桑植县| 汉沽区| 彝良县| 邵阳县| 吉林省| 天全县| 闽侯县| 阜城县| 崇左市| 咸阳市| 浦江县| 绥德县| 五大连池市| 巫山县| 马龙县| 昭觉县| 松桃| 稷山县| 大城县|