Struts 2 (2): How to pass parameter values to the Struts 2 tag in a label file

Source: Internet
Author: User

A label file is one of the JSP custom tags that is implemented by creating a. tag file, such as Table.tag, in the Web-inf\tags directory or subdirectory, and using the Taglib directive to refer to the directory in which the. tag file resides, as follows:

<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>

If you use the Struts 2 tag in a tag file, you can create a problem. Let's look at an example.

Create a Table.tag file in the Web-inf\tags directory with the following code:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ attribute name="index" type="java.lang.String" required="true" %>
<table border="1" style="font-size:25px">
    <s:iterator var="row" value="${index}">
        <tr>
            <s:iterator var="cell" value="row">
                <td>
                    <s:property value="cell" />
                </td>
        </s:iterator>
        </tr>
    </s:iterator>
</table>

The value attribute values for the <s:iterator> and <s:property> tag,<s:iterator> tags are used in the above tag file, which is not problematic in the lower version of Struts 2. However, in Struts 2.0.11 and later versions, it is not possible to use El and JSP expressions in the Struts 2 attribute values (that is, the default value of the Rtexprvalue parameter is false) and only ognl expressions, and in this case, The code above is not performing properly. One workaround is to set the Rtexprvalue parameter value of one of the corresponding labels in the distribution package for Struts 2 to true, but this method is not universal because it modifies struts 2 directly.

Another method is to use the <s:set> tag to save the corresponding value in the Valuestack object, and then read it using the OGNL expression. Using this method, you can modify the above code to the following form:

<%@ taglib prefix="s" uri="/struts-tags"%>
<table border="1" style="font-size:25px">
    <!--  使用Ognl表达式引用index  -->
    <s:iterator var="row" value="index">
        <tr>
            <s:iterator var="cell" value="row">
                <td>
                    <s:property value="cell" />
                </td>
        </s:iterator>
        </tr>
    </s:iterator>
</table>

The following code calls the table label file and passes the value of the index parameter:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!--  index参数值为grid1  -->
<s:set var="index" value="grid1" />
<t:table/>

<!--  index参数值为grid2  -->
<s:set var="index" value="grid2" />
<t:table/>

Which reader also has other methods to pass the index parameter value (cannot directly modify the STRUTS2 release package, does not use the request parameter), please keep abreast!

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.