我使用jstl来创建自定义标记。这是位置的内容。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>
这里我使用的位置标签:
<h:location id="a" locationType="ADDRESS"></h:location>
<h:location id="b"></h:location>
- 由于某些原因,生成的元素id没有前缀
<c:out value="${param.id}" />
。例如,在location.tag中,我写了<input type="text" id="<c:out value="${param.id}" />_addressHouseNumberInput"/>
,但两种用法的结果都是:<input type="text" id="_addressHouseNumberInput"/>
(它忽略了c:out
。怎么了 - 对于这两种用法,html结果是相同的,就好像它不识别参数locationType一样。为什么
- 我这里有很多代码重复。例如,所有的id前缀:
<c:out value="${param.id}" />
。有什么方法可以减少代码量吗
您使用的param
变量有一个隐式创建的客户端请求参数映射,该映射被传递到jsp。标签文件中定义的属性没有任何前缀,因此使用
<c:out value="${id}" />
应该足以输出正确的值。
如果您支持的jsp版本至少是2.0,那么您也可以省略c:out
标记,直接在text或attributess中使用el表达式。如果您需要值的xml转义,则需要c:out
,但由于您似乎控制了id
的值,因此这应该不是问题。