jstl自定义标记问题 - 忽略c:out,标记参数并减少代码

时间:2021-11-14 08:31:37

I am using jstl in order to create custom tag. Here is the content of location.tag:

我正在使用jstl来创建自定义标记。以下是location.tag的内容:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ attribute name="id" required="true" %>
<%@ attribute name="locationType" required="false" %>
<br/>
<c:out value="${param.id}" /> <---THIS ALWAYS PRINTS NOTHING! WHY?
<br/>
<c:out value="${param.locationType}" /> <---THIS ALWAYS PRINTS NOTHING! WHY?
<br/>
<c:if test="${empty param.locationType}" >
    <select id="<c:out value="${param.id}" />_locationTypeSelect">
        <option value="ADDRESS">כתובת</option>
        <option value="INSTITUTE">מוסד</option>
    </select>
    <script type="text/javascript">
        $(document).ready(function() {
            $('<c:out value="${param.id}" />_locationTypeSelect').change(function() {
                switch($(this).val()) {
                    case 'ADDRESS':
                        $('<c:out value="${param.id}" />_addressCitySelect').show();
                        $('<c:out value="${param.id}" />_addressStreetSelect').show();
                        $('<c:out value="${param.id}" />_addressHouseNumberInput').show();

                        $('<c:out value="${param.id}" />_instituteNameSelect').hide();
                        $('<c:out value="${param.id}" />_instituteBranchSelect').hide();
                        break;
                    case 'INSTITUTE':
                        $('<c:out value="${param.id}" />_addressCitySelect').hide();
                        $('<c:out value="${param.id}" />_addressStreetSelect').hide();
                        $('<c:out value="${param.id}" />_addressHouseNumberInput').hide();

                        $('<c:out value="${param.id}" />_instituteNameSelect').show();
                        $('<c:out value="${param.id}" />_instituteBranchSelect').show();
                        break;
                }
            });
        });
    </script>
</c:if>

<c:if test="${empty param.locationType or param.locationType == 'ADDRESS'}" >
    <select id="<c:out value="${param.id}" />_addressCitySelect"></select>

    <select id="<c:out value="${param.id}" />_addressStreetSelect"></select>

    <input type="text" id="<c:out value="${param.id}" />_addressHouseNumberInput"/>
</c:if>

<c:if test="${empty param.locationType or param.locationType == 'INSTITUTE'}" >
    <select id="<c:out value="${param.id}" />_instituteNameSelect"></select>

    <select id="<c:out value="${param.id}" />_instituteBranchSelect"></select>
</c:if>

Here I am using the location tag:

我在这里使用位置标记:

<h:location id="a" locationType="ADDRESS"></h:location>
<h:location id="b"></h:location>
  1. For some reasons the generated ids of the elements doesn't has the prefix <c:out value="${param.id}" />. For example, in location.tag I wrote <input type="text" id="<c:out value="${param.id}" />_addressHouseNumberInput"/> but the result of both the usages is: <input type="text" id="_addressHouseNumberInput"/> (it ignores the c:out. What is wrong?
  2. 由于某些原因,生成的元素ID不具有前缀 。例如,在location.tag中我写了 _ addressHouseNumberInput”/>但这两个用法的结果是:(它忽略了c:out。有什么问题?

  3. For the both usages the html result is the same, as if it doesn't recognize the parameter locationType. Why is that?
  4. 对于这两种用法,html结果是相同的,就好像它不能识别参数locationType一样。这是为什么?

  5. I have a lot of code duplication here. For example, all the id prefixes: <c:out value="${param.id}" />. Is there any way to reduce the amount of code?
  6. 我这里有很多代码重复。例如,所有id前缀: 。有没有办法减少代码量?

1 个解决方案

#1


0  

The param variable you use there is an implicitly created map of the clients request parameters that got passed to the jsp. The attribute defined in your tag file is available without any prefix, so using

您在那里使用的param变量是一个隐式创建的客户端请求参数映射,它已传递给jsp。您的标记文件中定义的属性可用,没有任何前缀,因此使用

<c:out value="${id}" />

should be enough to output the correct value.

应该足以输出正确的值。

If your supported jsp version is at least 2.0, you can also omit the c:out tag and directly use the el expression in text or attributess. The c:out would be required if you need xml escaping of the value, but since your seem to control the value of id this should not be an issue.

如果您支持的jsp版本至少为2.0,则还可以省略c:out标记并直接在text或attributess中使用el表达式。如果您需要xml转义值,则需要c:out,但由于您似乎控制了id的值,因此这应该不是问题。

#1


0  

The param variable you use there is an implicitly created map of the clients request parameters that got passed to the jsp. The attribute defined in your tag file is available without any prefix, so using

您在那里使用的param变量是一个隐式创建的客户端请求参数映射,它已传递给jsp。您的标记文件中定义的属性可用,没有任何前缀,因此使用

<c:out value="${id}" />

should be enough to output the correct value.

应该足以输出正确的值。

If your supported jsp version is at least 2.0, you can also omit the c:out tag and directly use the el expression in text or attributess. The c:out would be required if you need xml escaping of the value, but since your seem to control the value of id this should not be an issue.

如果您支持的jsp版本至少为2.0,则还可以省略c:out标记并直接在text或attributess中使用el表达式。如果您需要xml转义值,则需要c:out,但由于您似乎控制了id的值,因此这应该不是问题。