I am trying to display the select options on a JSP page. The options are added to the model object, but they are not being displayed, and I can't figure out why. My controller code is given below
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";
}
My model bean is given below:
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();
}
}
My JSP page.
<%@ 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>
I find that rendered JSP does not recognize the serverEnvironments
attribute that I added to the Spring's model object. I have gone through the following posts
- Spring MVC model attribute value not displaying
- How to bind multiple dropdown to model - spring mvc
- how to bind select option value in spring mvc
I even tried the passing the object to ModelAndView, but to no avail. My code for handling ModelAndView is given below and is based on this article
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;
}
I tried displaying the serverAttributes
using <c:out value="${serverAttributes}" />
statement, but the output is displayed as it is.
The generated JSP file is:
http://localhost:8080/cmsbackend/downloaddcrcontent
while it should be requested withhttp://localhost:8080/showEnvironments
– Fevly Pallar