Struts-tag Library (logic)

Source: Internet
Author: User

Logic tag Library

This tag library can be divided into three types: condition, loop, and forwarding/redirection.

I.) Condition Type

  1. Logic: Empty and logic: notempty
    Logic: the empty label determines whether the script variable is null, whether it is an empty string (the length is 0), whether it is an empty collection or map (call the isempty () method to determine ). Logic: the notempty label is similar.
    <Logic: Empty name = "mybean">
    The bean is missing
    </Logic: Empty>
    <Logic: notempty name = "mybean">
    The bean is not missing
    </Logic: notempty>

    The code above indicates that when a bean named mybean does not exist in all the scopes, the bean is output as missing; If yes, the bean is not missing.
    This tag has three attributes: name, property, and scope.

  2. Logic: equal, logic: notequal, logic: lessthan, logic: greaterthan, logic: lessequal, and logic: greaterequal
    These logic: * equal * tags can be identified by name, and they are clumsy to use.
    The following attributes are available: name, property, scope, value, Cookie, header, and parameter.
    <Logic: equal name = "Bean" property = "doubleproperty" value = "<% = doub1 %>">
    Equal
    </Logic: equal>
    <Logic: greaterequal name = "Bean" property = "doubleproperty" value = "<% = doub1 %>">
    Greaterequal
    </Logic: greaterequal>

  3. Logic: Present and logic: notpresent
    Check whether the header, request parameter, Cookie, JavaBean, or JavaBean propertie exists and is not equal to null. You can also check the identity of the current user and use the attributes user and role.
    Other attributes include name, property, scope, Cookie, header, and parameter.
    <Logic: notpresent name = "mybean" property = "prop" Scope = "page">
    The bean property bean. Prop is present
    </Logic: notpresent>

    The code above checks whether the bean named mybean in the page scope has a prop attribute.

  4. Logic: Match and logic: notmatch
    Compares the two strings to determine whether they are equal. You can compare the start, end, or part of a string.
    Location attribute: Specifies to start from a certain position for comparison.
    Other attributes include name, property, scope, value, Cookie, header, and parameter.
    The following example shows how to check the browser type:
    <Logic: Match header = "User-Agent" value = "Mozilla">
    Mozilla!
    </Logic: Match>
    <Logic: notmatch header = "User-Agent" value = "Mozilla">
    Not Mozilla
    </Logic: notmatch>

    Check whether the bean property matches the "Hello World" string:

    <Logic: match name = "Bean" property = "stringproperty" value = "Hello World">
    Hello world!
    </Logic: Match>
    <Logic: notmatch name = "Bean" property = "stringproperty" value = "Hello World">
    I'm so sad and lonely.
    </Logic: notmatch>

    Check whether the request parameter named "name" is a sub-string of "XYZ, however, the substring must start from index position 1 of "XYZ" (that is, the substring must be "y" or "YZ "):

    <Logic: Match parameter = "name" value = "XYZ" location = "1">
    The parameter name is a sub-string of the string XYZ from Index 1
    </Logic: Match>

Ii.) loop type

Logic: iterate
Repeat the elements of the specified set nested in this tag body. The set must be of the following types: array, Collection (including arraylist and vector), enumeration, iterator, map (including hashmap, hashtable, and treemap ). For example:

<Logic: iterate id = "employee" name = "department" property = "employees" Scope = "request">
...
<Bean: write name = "employee" property = "username"/>
...
<Bean: write name = "employee" property = "name"/>
...
<Bean: write name = "employee" property = "phone"/>
...
</Logic: iterate>

The above code iterates a set of employee, which is a set of employees in the department attribute and has the scope of request.

To output the fifth to tenth employee, use the length and offset attributes:

<Logic: iterate id = "employee" name = "department" property = "employees" Scope = "request"Length= "10"Offset= "5">
...
<Bean: write name = "employee" property = "username"/>
...
<Bean: write name = "employee" property = "name"/>
...
<Bean: write name = "employee" property = "phone"/>
...
</Logic: iterate>

You can define a variable to save the current index value:

<Ol>
<Logic: iterate id = "element" name = "Bean" property = "stringarray"Indexid= "Index">
<Li>
<Em>
<Bean: write name = "element"/>
</Em> & nbsp;
[<Bean: write name = "Index"/>] </LI>
</Logic: iterate>
</OL>

Iii.) Forwarding/Redirection type

Logic: Forward and logic: Redirect
The logic: Forward label is similar to the JSP: Forward label, but it can use an actionforward:

<Logic: Forward name = "login"/>
Certificate ----------------------------------------------------------------------------------------------------------------------------------
The code in global forward related to the above:
<Global-forwards>
<Forward name = "login" Path = "/loginform. jsp"/>
</Global-forwards>

Logic: the Redirect label is similar to the above label, but it calls response. sendredirect () by default, replacing the above requestdispatcher. Forward (). The biggest difference is that it supports all attributes of the HTML: link tag, so you can specify the request parameter:

<Logic: Redirect name = "login" paramid = "employeeid" paramname = "employee" property = "ID"/>

Tip: 1.) In the MVC Framework, the two labels are not recommended. You should select the next view from the controller instead of the view.
2) do not use too many logic tags on JSP pages.

 

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.