Struts (tags)

Source: Internet
Author: User
Tags control label

Struts2-tags

Common Tags:

Property

The default value is of type object, as long as the object type is parsed into an OGNL expression, but if you want to pass only a normal string, add ' '; example

<s:property value= "username"/> will be parsed into OGNL expression

<s:proerty value= "' username '"/> The username at this time is an ordinary string

Set Default values:

<s:property value= "admin" default= "admin"/> No admin attribute in the value stack, take the default value, if the value stack has the value of the admin corresponding, then take the value

Set HTML:

<s:property value= "'


Set

The type of value is Object

The name and ID are replaced with obsolete unified use Var

var defines a variable, scope specifies the range of variables (desirable: page,session,application,request,action)

value specifies the values of the variable, without specifying scope: The default scope is request and Actioncontext

<s:set var= "name" value= "username"/> stored in Request and Actioncontext, the value of name is the value saved in username

Remove from Request: <s:property value= "#request. Name"/>

Take value from Actioncontext: <s:property var= "#name"/>

Setting Range: <s:set var= "pass" value= "password" scope= "page"/>

Value from the appropriate range: <%=pagecontext.getattribute ("pass")%>


Bean

To generate an object:

<s:bean name= "Classpath" var= "Object Name" >

<s:param name= "Property name" value= "value" ></s:param>

</s:bean>

When the object is not named: The resulting object is placed in the value stack, and access to the object's properties can be accessed by accessing the value stack: <s:property value= "property name"/>

However, you cannot access the object outside the bean tag, and when the bean tag is finished, the object is removed from the value stack


After the object is named, the object is placed in Stackcontext and can be accessed outside the label <s:property value= "#对象. Properties"/>

Include

The label of value is of type string

Include contains an HTML page to the current page

<s:include value= "HTML URL"/>

When value is stored in a string type, ${} can be cast to an OGNL expression

Not recommended, Chinese often garbled

Param

Debug

Control label

If ElseIf else

The IfElse control label uses test to get the value of the parameter, which is the same as the property label

<s:if test= "#name ==null" > Please fill in your name </s:if>

<s:set var= "Len" value= "Name.length ()"/>

<s:if test= "#len <0" >wrong</s:if>

<s:elseif test= "#len >10" >to long</s:elseif>

<s:else >OK</s:else>

Iterator

To traverse a collection:

<s:iterator value= "{1,2,3,4}" > Define a Collection

<s:property/> Iterating through the collection

</s:iterator>


Custom Variable Traversal Collection

<s:iterator value= "{' AAA ', ' BBB ', ' CCC '}" var= "X" > defines the variable x equivalent to the loop variable in for each, which is the element in the collection

<s:property value= "#x. toUpperCase ()"/> Gets the elements in the collection

</s:iterator>


Use Status:status to record the state of the collection traversal process

<s:iterator value= "{' AAA ', ' BBB, ' CCC '}" status= "status" >

<s:property/>

<s:property value= "#status. Count"/>

<s:property value= "#status. Index"/>

<s:property value= "#status. Odd"/> Determine if it is odd

<s:property value= "#status. Even"/> Determine if an even number

<s:property value= "#status. First"/> To determine if it is the primary element

<s:property value= "#status. Last"/>

</s:iterator>


Traverse the Map Collection

<s:iterator value= "#{1: ' A ', 2: ' B ', 3: ' C '} ' > Define map set must be added #

<s:property value= "key"/> Gets the key of the map collection

<s:property value= "value"/> Gets the value of the map collection

</s:iterator>


Customize to traverse the map collection with variables

<s:iterator value= "#{1: ' A ', 2: ' B ', 3: ' C '}" var= "X" >

<s:property value= "#x. Key"/>

<s:property value= "#x. Value"/>

</s:iterator>

Subset

Ui

Declarative exception Handling:

If you want struts to handle exceptions, in the background code, you will throw the exception display,

In the corresponding Struts.xml configuration the catch of the exception:

The first way to configure your own exception handling in each action:

<exception-mapping result= "result name" exception= "Java exception package"/>

<result name= "" >/xx.jsp</result>

The second type of unified configuration processing:

Put exception handling in one package, and other packages can be used as long as they inherit the package

Example

<package name= "xxxx" extends= "Struts-default" >

<global-results>

<result name= "xxx" >/xx.jsp</result>

</global-results>

<global-exception-mappings>

<exception-mapping result= "result name" exception= "Java exception package"/>

</global-exception-mappings>

</package>


I18N International standards: (rarely used, time-checked documentation)

Interceptors:



Struts (tags)

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.