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!