0
votes

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

  1. Spring MVC model attribute value not displaying
  2. How to bind multiple dropdown to model - spring mvc
  3. 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:

enter image description here

2
Your codes work.I suspect that there are probably 2 methods returning the same view, the only difference is that, another one doesn't get the list initialized . On your picture the form is requested with http://localhost:8080/cmsbackend/downloaddcrcontent while it should be requested with http://localhost:8080/showEnvironmentsFevly Pallar
I tried your suggestion, but I see no impact. The variables are not rendered.Kartik

2 Answers

2
votes

So the problem was in my web.xml. My configuration shows that I was addressing 2.3 XSD. As a result, EL tags were ignored.

By changing the web.xml to version 2.4, the changes worked.

<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>
1
votes

in your jsp change the code

<form:select path="environment" class="form-control" tabindex="0">

to

<form:select path="serverEnvironments" class="form-control" tabindex="0">