I am using jstl in order to create custom tag. Here is the content of 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. For the both usages the html result is the same, as if it doesn't recognize the parameter locationType. Why is that?
  3. 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?
有帮助吗?

解决方案

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

<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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top