在spring mvc app中,下面的下拉列表没有填充任何值,即使sel_word.concepts
集合中有很多值:
<form:select path="conceptSelected" size="5" style="min-width:600px">
<form:options items="${sel_word.concepts}"/>
</form:select>
要确认已填充sel_word.concepts
列表并将其发送到视图,我:
1.) printed out the size of sel_word.concepts in the controller and
2.) populated a data table in the jsp with the contents of sel_word.concepts.
为了明确地显示,视图中的下表使用sel_word.concepts的值正确填充:
<datatables:table id="drugconcepts" data="${sel_word.concepts}" cdn="true" row="concept" theme="bootstrap2"
CssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 650px;" align="left" >
<datatables:column title="cui" cssStyle="width: 100px;" display="html">
<c:out value="${concept.rxcui}"/>
</datatables:column>
<datatables:column title="Names" cssStyle="width: 500px;" display="html">
<c:out value="${concept.str}"/>
</datatables:column>
</datatables:table>
因此sel_word.concepts
中有值,这些值实际上是发送到jsp的。那么为什么这些值不会填充form:select
下拉列表?如何更改代码以使下拉列表正确填充?
供参考,我已在文件共享网站上发布了model
,view
和controller
的完整代码,您可以点击以下链接访问该网站。
您可以查看model
by clicking on this link的代码
您可以查看模型by clicking on this link的DrugWord
实体部分的代码
您可以查看view
by clicking on this link的完整代码
您可以查看controller
by clicking on this link的完整代码。
根据CodeChimp的请求,我发布了实际发送到浏览器的html,如下所示。这是我在浏览器中点击view source
时得到的结果:
<form:form modelAttribute="medication" method="post" class="form-horizontal" >
<div class="control-group">
Drug Concepts:
<form:select path="conceptSelected" size="5" style="min-width:600px">
<form:options items="[1163058, 1163059, 1163060, 1163061, 1163062, 1163063, 1163064, 190521, 213460, 213461, 221052, 242679, 242680, 284620, 285028, 307650, 329484, 329485, 364596, 368153, 368253, 370502, 370503, 374813, 476143, 539824, 573348, 573349, 575077, 602392, 602393, 602394, 602395, 614534] itemValue="rxcui"/>
</form:select>
</div>
<div class="control-group">
Dosage Text:
<form:input path="dosagetext" />
</div>
<div class="control-group">
Route:
<form:input path="route" />
</div>
<div class="control-group">
Frequency:
<form:select path="freqSelected" items="[twice daily, as directed, as required, once a day, in the morning, once a day, once a day, before bed, four times daily, every other day, once a day, in the evening, three times daily]" size="5" style="min-width:600px"/>
</div>
<div class="control-group">
Duration:
<form:input path="duration" />
</div>
<div class="control-group">
Start Date:
</div>
<div class="control-group">
Status:
<form:input path="status" />
</div>
<form:hidden path="id"/>
<div class="form-actions">
<button type="submit">Add this medication</button> <h3>Link to delete will go here.</h3>
</div>
</form:form>
相比之下,服务器上JSP表单的代码如下:
<c:set var="method" value="post"/>
<c:url value="/patientscodes.html" var="actUrl"/>
<form:form modelAttribute="medication" method="${method}" class="form-horizontal" >
<div class="control-group">
Drug Concepts:
<form:select path="conceptSelected" size="5" style="min-width:600px">
<form:options items="${sel_word.concepts} itemValue="rxcui"/>
</form:select>
</div>
<div class="control-group">
Dosage Text:
<form:input path="dosagetext" />
</div>
<div class="control-group">
Route:
<form:input path="route" />
</div>
<div class="control-group">
Frequency:
<form:select path="freqSelected" items="${freqs}" size="5" style="min-width:600px"/>
</div>
<div class="control-group">
Duration:
<form:input path="duration" />
</div>
<div class="control-group">
Start Date:
</div>
<div class="control-group">
Status:
<form:input path="status" />
</div>
<form:hidden path="id"/>
<div class="form-actions">
<button type="submit">Add this medication</button> <h3>Link to delete will go here.</h3>
</div>
</form:form>
根据CodeChimp的请求,mvc-view-resolver.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- DispatcherServlet application context for knowledgemanager's web tier.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
- The ContentNegotiatingViewResolver delegates to the InternalResourceViewResolver and BeanNameViewResolver,
- and uses the requested media type (determined by the path extension) to pick a matching view.
- When the media type is 'text/html', it will delegate to the InternalResourceViewResolver's JstlView,
- otherwise to the BeanNameViewResolver.
-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="cnManager"/>
<property name="viewResolvers">
<list>
<!-- Default viewClass: JSTL view (JSP with html output) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Used here for 'xml' and 'atom' views -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
</list>
</property>
</bean>
<!-- Simple strategy: only path extension is taken into account -->
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true"/>
<property name="defaultContentType" value="text/html"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="atom" value="application/atom+xml" />
</map>
</property>
</bean>
</beans>
答案 0 :(得分:0)
按如下所示更改jsp,以包含itemValue和itemLabel:
<form:select path="conceptSelected" size="5" style="min-width:600px">
<form:options items="${sel_word.concepts}" itemValue="rxcui" itemLabel="str"/>
</form:select>