我正在尝试在JSP页面上显示选择选项。选项被添加到模型对象,但它们没有显示,我无法弄清楚原因。我的控制器代码如下所示
List<String> buildServerEnvironments() {
List<String> environments = new ArrayList<>();
environments.add("cert");
environments.add("qa5");
return environments;
}
@RequestMapping(value = "/downloaddcrcontent", method = RequestMethod.GET)
String showEnvironmentForm(Model model) {
model.addAttribute("envObj", new Environment());
model.addAttribute("serverEnvironments", this.buildServerEnvironments());
return "environments";
}
我的模型bean如下:
public class Environment {
private String environment;
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return new StringBuilder(this.getClass().getSimpleName()).
append("(environment = ").append(this.environment).append(")").
toString();
}
}
我的JSP页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Used to import resources. --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<title>Select Environment</title>
<%@ include file="meta_head.jspf" %>
<%@ include file="bootstrap_import.jspf" %>
<!-- Custom style sheet. -->
<style>
body {
padding-top: 50px;
padding-bottom: 50px;
background-color: #EEE;
}
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="form-signin">
<p class="lead">Environment Selection.</p>
<form:form class="form-signin form-horizontal" method="POST" action="generatedcrreport" modelAttribute="envObj" >
<spring:bind path="environment">
<div class="form-group ${status.error ? 'has-error' : ''}">
<form:label path="environment">Environment</form:label>
<c:out value="${serverEnvironments}" />
<form:select path="environment" class="form-control" tabindex="0">
<form:option value="">Select an environment</form:option>
<c:forEach var="env" items="${serverEnvironments}" varStatus="index">
<form:option value="${env}"><c:out value="${env}" /></form:option>
</c:forEach>
</form:select>
</spring:bind>
</div>
<form:button type="submit" class="btn btn-lg btn-primary btn-block">Select Environment</form:button>
</form:form>
</div>
</div>
</div>
</body>
</html>
我发现渲染的JSP无法识别我添加到Spring模型对象的serverEnvironments
属性。我已经阅读了以下帖子
我甚至尝试将对象传递给ModelAndView,但无济于事。我的处理ModelAndView的代码如下所示,基于这篇文章
https://www.javacodegeeks.com/2013/07/spring-mvc-form-handling-vol-5-select-option-options-tags.html
@RequestMapping(value = "/showEnvironments", method = RequestMethod.GET)
ModelAndView showEnvironmentForm() {
ModelAndView view = new ModelAndView("environments");
view.addObject("envObj", new Environment());
view.addObject("serverEnvironments", this.buildServerEnvironments());
return view;
}
我尝试使用serverAttributes
语句显示<c:out value="${serverAttributes}" />
,但输出显示原样。
生成的JSP文件是:
答案 0 :(得分:1)
改变了代码
<form:select path="environment" class="form-control" tabindex="0">
到
<form:select path="serverEnvironments" class="form-control" tabindex="0">
答案 1 :(得分:1)
所以问题发生在我的web.xml中。我的配置显示我正在处理2.3 XSD。结果,EL标签被忽略了。
通过将web.xml更改为2.4版,更改工作正常。
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
. . . . .
</web-app>