Struts2 tag Value Method

Source: Internet
Author: User

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

 

1. Tag Value Method 1

Use <s: property value = ""/>

When the valuestack of an action contains the value of this attribute, you only need to directly use the name of this attribute;

When the valuestack of an action does not have the value of this attribute, for example, when the value of this attribute is in the session or application range, you need to add # Or # ATTR .;

Example:

Assume that an action contains the person member variable and the company attribute exists in the application.

You can use the following methods to set the value:

<S: property value = "person. Name"/>

<S: property value = "# person. Name"/>

<S: property value = "company. Name"/> // cannot be obtained because company is not in the valuestack of action.

<S: property value = "# company. Name"/>

2. Tag value method 2

Use % {} In any <s:/> label.

When the valuestack of an action contains the value of this attribute, you only need to directly use the name of this attribute;

When the valuestack of an action does not have the value of this attribute, for example, when the value of this attribute is in the session or application range, you need to add # Or # ATTR .;

Example:

Assume that an action contains the person member variable and the company attribute exists in the application.

<S: textfield name = "person. Name" value = "person. Name"/> // error. The person. Name is displayed in the Value Field.

<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. Obtain the attributes in the request, session, and application of the JSP page.

You can obtain the following information on the page:

<TD >$ {applicateionscope. Counter} </TD>

<TD >$ {sessionscope. Counter} </TD>

<TD >$ {requestscope. Counter} </TD>

Or use $ {property }$ {userbean. Username} directly }. Userbean can be an object in a request or session.

The content of the Action Code in struts2 is:

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 is to directly set the request value.

You can also obtain the request in the following ways:

Httpservletrequest r = servletactioncontext. getrequest ();

Httpservletresponse resp = servletactioncontext. getresponse ();

Struts2 also provides the following interfaces:

Servletcontextaware: Action implements this interface and can directly access servletcontext.

Servletrequestaware: Action implements this interface and can directly access httpservletrequest.

Servletresponseaware: Action implements this interface and can directly access httpservletresponse.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.