使用 jsp 使用 optgroup 创建 select
Creating select with optgroup using jsp
我有一个使用 jsp 生成的 select,效果很好。这是我使用的代码:
<select class="heading4Black" name="DailyTasks" onchange="showreport(this)">
<%
for (int i = 0; i < links.size(); i++) {
elem = (Element) links.get(i);
%>
<option title='<%=elem.attributeValue("tooltip") %>' value='<%= elem.attributeValue("url")%>' <%= (i == 0) ? "selected" : "" %> ><%=elem.attributeValue("name")%></option>
<%
}
%>
</select>
这给了我想要的输出。现在,我需要将一个 optgroup 添加到 select 选项之一,而不是让 select 带有这样的选项:
option1
option2
option3
我需要将选项 2 设为 html optgroup,这样我的 select 应该如下所示:
option1
option2
subopt1
subopt2
option3
我一直在尝试使用 jstl 和 c:choose,但是我在这方面遇到了很多麻烦。这是我尝试使用的代码(及其变体):
<select class="heading4Black" name="DailyTasks" onchange=" somefunction(this)">
<%
for (int i = 0; i < links.size(); i++) {
elem = (Element) links.get(i);
%>
<c:choose>
<c:when test="'${elem[i].attributeValue("name")}'=='Facility Ticket'">
<optgroup label="Some String">
<option>One</option>
<option>Two</option>
</optgroup>
</c:when>
<c:otherwise>
<option title='<%=elem.attributeValue("tooltip") %>' value='<%= elem.attributeValue("url")%>' <%= (i == 0) ? "selected" : "" %> ><%=elem.attributeValue("name")%></option>
</c:otherwise>
</c:choose>
<%
}
%>
</select>
我得到的是 select,选项如下所示:
Some String
subopt1
subopt2
Option1
Some String
subopt1
subopt2
Option2
Some String
subopt1
subopt2
有人可以提供一些关于如何进行这项工作的指导吗?请记住,我真的是一个 javascript 人。我放弃了这个 jsp 项目,因为大约 10 年前我接触过它。
谢谢
问题 1:
您的输出表明 JSTL (choose,when,otherwise) 已被忽略。
对于每次迭代,您都会在 when
和 otherwise
中获取代码。
这是因为您可能忘记在 JSP:
前面添加这一行
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
通过添加以上行,您可以激活 JSP 标准标签库的核心评估。所以像 <c:tags>
这样的标签将被处理。否则它们将按原样发送到浏览器。
(您可以在浏览器(Chrome&Firefox)中按CTRL+U
查看html代码)
问题 2:
修复后,您可能会遇到编译错误。
c:when
行有问题:
<c:when test="'${elem[i].attributeValue("name")}'=='Facility Ticket'">
我可以看到多个问题:
- 不需要索引
[i]
,您已经通过 links.get(i)
: 访问了该元素
- 您尝试通过表达式语言
${elem...}
访问 elem
,但仅当将此 scriptlet 变量分配给属性时才有效。
使用小脚本的解决方案:
如果您在 c:when test
中使用 scriptlet,它可能看起来像:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<select class="heading4Black" name="DailyTasks" onchange=" somefunction(this)">
<%
for (int i = 0; i < links.size(); i++) {
// added Element type in front
Element elem = (Element) links.get(i);
%>
<c:choose>
<c:when test="<%=elem.attributeValue("name").equals("Facility Ticket")%>">
<optgroup label="Some String">
<option>One</option>
<option>Two</option>
</optgroup>
</c:when>
<c:otherwise>
<option title='<%=elem.attributeValue("tooltip") %>'
value='<%= elem.attributeValue("url")%>'
<%= (i == 0) ? "selected" : "" %>
>
<%=elem.attributeValue("name")%>
</option>
</c:otherwise>
</c:choose>
<%
}
%>
</select>
没有小脚本的解决方案。仅限 JSTL 和 JSP-EL:
您正在混合 scriptlet、EL 和 JSTL。可以统一的。
小脚本中的 for
可以替换为 <c:forEach>
。
所有JSP表达式(<%= ... %>
)都可以用EL(表达式语言)代替。
但首先,在准备 links
的地方,将它们设置为请求属性,如下所示:
request.setAttribute("links", links);
在那之后 JSP 可能看起来像:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<select class="heading4Black" name="DailyTasks" onchange=" somefunction(this)">
<c:forEach var="elem" items="${links}" varStatus="varStatus">
<c:choose>
<c:when test="${elem.attributeValue('name')=='Facility Ticket'}">
<optgroup label="Some String">
<option>One</option>
<option>Two</option>
</optgroup>
</c:when>
<c:otherwise>
<option title="${elem.attributeValue('tooltip')}"
value="${elem.attributeValue('url')}"
<c:if test="${varStatus.index == 0}"> selected</c:if>
>
${elem.attributeValue('name')}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
注意 varStatus.index
访问迭代索引的用法。
我有一个使用 jsp 生成的 select,效果很好。这是我使用的代码:
<select class="heading4Black" name="DailyTasks" onchange="showreport(this)">
<%
for (int i = 0; i < links.size(); i++) {
elem = (Element) links.get(i);
%>
<option title='<%=elem.attributeValue("tooltip") %>' value='<%= elem.attributeValue("url")%>' <%= (i == 0) ? "selected" : "" %> ><%=elem.attributeValue("name")%></option>
<%
}
%>
</select>
这给了我想要的输出。现在,我需要将一个 optgroup 添加到 select 选项之一,而不是让 select 带有这样的选项:
option1
option2
option3
我需要将选项 2 设为 html optgroup,这样我的 select 应该如下所示:
option1
option2
subopt1
subopt2
option3
我一直在尝试使用 jstl 和 c:choose,但是我在这方面遇到了很多麻烦。这是我尝试使用的代码(及其变体):
<select class="heading4Black" name="DailyTasks" onchange=" somefunction(this)">
<%
for (int i = 0; i < links.size(); i++) {
elem = (Element) links.get(i);
%>
<c:choose>
<c:when test="'${elem[i].attributeValue("name")}'=='Facility Ticket'">
<optgroup label="Some String">
<option>One</option>
<option>Two</option>
</optgroup>
</c:when>
<c:otherwise>
<option title='<%=elem.attributeValue("tooltip") %>' value='<%= elem.attributeValue("url")%>' <%= (i == 0) ? "selected" : "" %> ><%=elem.attributeValue("name")%></option>
</c:otherwise>
</c:choose>
<%
}
%>
</select>
我得到的是 select,选项如下所示:
Some String
subopt1
subopt2
Option1
Some String
subopt1
subopt2
Option2
Some String
subopt1
subopt2
有人可以提供一些关于如何进行这项工作的指导吗?请记住,我真的是一个 javascript 人。我放弃了这个 jsp 项目,因为大约 10 年前我接触过它。 谢谢
问题 1:
您的输出表明 JSTL (choose,when,otherwise) 已被忽略。
对于每次迭代,您都会在 when
和 otherwise
中获取代码。
这是因为您可能忘记在 JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
通过添加以上行,您可以激活 JSP 标准标签库的核心评估。所以像 <c:tags>
这样的标签将被处理。否则它们将按原样发送到浏览器。
(您可以在浏览器(Chrome&Firefox)中按CTRL+U
查看html代码)
问题 2:
修复后,您可能会遇到编译错误。
c:when
行有问题:
<c:when test="'${elem[i].attributeValue("name")}'=='Facility Ticket'">
我可以看到多个问题:
- 不需要索引
[i]
,您已经通过links.get(i)
: 访问了该元素
- 您尝试通过表达式语言
${elem...}
访问elem
,但仅当将此 scriptlet 变量分配给属性时才有效。
使用小脚本的解决方案:
如果您在 c:when test
中使用 scriptlet,它可能看起来像:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<select class="heading4Black" name="DailyTasks" onchange=" somefunction(this)">
<%
for (int i = 0; i < links.size(); i++) {
// added Element type in front
Element elem = (Element) links.get(i);
%>
<c:choose>
<c:when test="<%=elem.attributeValue("name").equals("Facility Ticket")%>">
<optgroup label="Some String">
<option>One</option>
<option>Two</option>
</optgroup>
</c:when>
<c:otherwise>
<option title='<%=elem.attributeValue("tooltip") %>'
value='<%= elem.attributeValue("url")%>'
<%= (i == 0) ? "selected" : "" %>
>
<%=elem.attributeValue("name")%>
</option>
</c:otherwise>
</c:choose>
<%
}
%>
</select>
没有小脚本的解决方案。仅限 JSTL 和 JSP-EL:
您正在混合 scriptlet、EL 和 JSTL。可以统一的。
小脚本中的 for
可以替换为 <c:forEach>
。
所有JSP表达式(<%= ... %>
)都可以用EL(表达式语言)代替。
但首先,在准备 links
的地方,将它们设置为请求属性,如下所示:
request.setAttribute("links", links);
在那之后 JSP 可能看起来像:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<select class="heading4Black" name="DailyTasks" onchange=" somefunction(this)">
<c:forEach var="elem" items="${links}" varStatus="varStatus">
<c:choose>
<c:when test="${elem.attributeValue('name')=='Facility Ticket'}">
<optgroup label="Some String">
<option>One</option>
<option>Two</option>
</optgroup>
</c:when>
<c:otherwise>
<option title="${elem.attributeValue('tooltip')}"
value="${elem.attributeValue('url')}"
<c:if test="${varStatus.index == 0}"> selected</c:if>
>
${elem.attributeValue('name')}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
注意 varStatus.index
访问迭代索引的用法。