struts2 標籤取值方式

來源:互聯網
上載者:User

 http://hi.baidu.com/kalada/blog/item/daaf0482e4b11499f703a617.html

 

1.標籤取值方式一

    通過<s:property value="" />取值

    當Action的valueStack中有該屬性的值時,只需直接使用該屬性的名字即可;

    當Action的valueStack中沒有該屬性的值時,比如在session,application範圍中的屬性值時,需要加#或者#attr.;

    例子:

    假設某Action中有person成員變數,在application中存在company屬性

    那麼我們可以通過以下方法取值:

    <s:property value="person.name" />

    <s:property value="#person.name" />

    <s:property value="company.name" /> //無法取到,因為company不在action的valueStack中

    <s:property value="#company.name" />

2.標籤取值方式二

在任意的<s:/>標籤內使用%{}來取值

當Action的valueStack中有該屬性的值時,只需直接使用該屬性的名字即可;

當Action的valueStack中沒有該屬性的值時,比如在session,application範圍中的屬性值時,需要加#或者#attr.;

例子:

假設某Action中有person成員變數,在application中存在company屬性

<s:textfield name="person.name" value="person.name" /> //錯誤,value會直接顯示person.name字樣

<s:textfield name="person.name" value="%{person.name}" />

<s:textfield name="person.company.name" value="%{#company.name}" />

<s:textfield name="person.company.name" value="%{#attr.company.name}" />

3.擷取JSP頁面的request,session,application中的屬性

   在頁面中可以這樣擷取:

<td>${applicateionScope.counter}</td>

<td>${sessionScope.counter}</td>

<td>${requestScope.counter}</td>

或者直接這樣用:${屬性} ${userBean.username}。userBean可以是request或session中的對象。

struts2中的Action代碼中的內容為:

ActionContext ctx = ActionContext.getContext();

ctx.getApplication.put("counter",new Integer(5));

ctx.getSession.put("counter",new Integer(5));

ctx.put("counter",new Integer(5));

ctx.put就是直接設定request的值。

也可以使用以下方式獲得request:

HttpServletRequest r = ServletActionContext.getRequest();

HttpServletResponse resp = ServletActionContext.getResponse();

struts2還提供了以下介面:

ServletContextAware:Action實現該介面,可以直接存取ServletContext。

ServletRequestAware:Action實現該介面,可以直接存取HttpServletRequest。

ServletResponseAware:Action實現該介面,可以直接存取HttpServletResponse。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.