Hot questions for Spring Forms

Top 10 Java Open Source / Spring / Spring Forms

Question:

When I'm using a Spring MVC controller and I submit a form by setting value to the modelAttribute I lose some of the model's fields which are not mapped in the form. In this example, because I didn't map age and address, these user's fields are lost and so they have null value. I don't want to let the user change their age and the address, that's why these fields are not in the form.

Method controller to post user edit form:

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editionUser(Model model, @ModelAttribute("accountForm") User user,
  BindingResult bresult,
  RedirectAttributes redirectAttributes) {
  //user.getAge() and user.getAddress are null           

  //Save the information...
}

Method controller to get edit user page:

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String initEdit(Model model) {
  // the user fields are all filled
  User user = userService.getUserById(...);
  model.addAttribute("accountForm", user);
  return "edition";
}


class User {
  Long id;
  String email;
  String age;
  String address;
  String nickname;

  // ...
}

edition.jsp

<form:form class="form" method="POST" action="edition" modelAttribute="accountForm">
  <form:label path="email">email</form:label>
  <form:input path="email"  name='email'/>
  <form:label path="nickname">nickname</form:label>
  <form:input path="nickname"  name='nickname'/>
  <form:hidden path="id" />
  <input name="submit" type="submit"    value="valid" />  
</form:form>

What are the best solution to not lose the value of these fields ? (age and address)

Use form hidden path for each field ?? Store the user in session before redirect to the edit page and set retreive the user session in post method controller to only change the modified fields ? Is there a generic method?


Answer:

I'd go with session for one simple reason: hidden form fields can be easily manipulated client-side before submitting the form, therefore they do not satisfy your requirements.

Note that without saving previously entered values somewhere, you can't even detect if they were tampered with on client-side, so hidden fields are definitely not an option. Use the session.

Question:

I am using Spring's form:input as below :

<form:input type="number" .....>

in my jsp but when I check the html that is rendered on the browser it shows like :

type="number" type="text"

i.e., two type attributes are generated in the html.

On the other hand, if I check using inspect element option in the browser, it shows correct - only type="number" as expected. Edit- My Question: Why am I getting two type attributes in generated html (type="number" type="text") ? How to get it resolved?


Answer:

Spring form:input tag doesnt have any attribute named type and the type=number used in your code belongs to html5 input tag

Also have a look at HTML Text Input allow only Numeric input

Spring form tld lists the valid attributes of form:input element here

Question:

Why is spring not binding the values on my nested object?

The SecurityQuestion object on the RegistrationBean is set with question and answer as null, null, respectively, despite setting then in form using bean notation in the view.

Beans:

public class SecurityQuestion {
  SecurityQuestionType type;
  String answer;
}

public class RegistrationBean {
  @Valid
  SecurityQuestion securityQuestion;
  String name;


  public SecurityQuestionType[] getSecurityQuestionOptions() {
    return SecurityQuestionType.values();
  }
}

View:

<form:form modelAttribute="registrationBean" method="POST">
  <form:select id="securityQuestion" path="securityQuestion.question">
    <c:forEach var="securityQuestionOption" items="${securityQuestionOptions}">
      <form:option value="${securityQuestionOption}">${securityQuestionOption</form:option>
    </c:forEach>
  </form:select>

  <form:input id="securityAnswer" path="securityQuestion.answer" />
  <form:input id="name" path="name" />
</form:form>

Controller:

@RequestMapping(value = URL_PATTERN, method = RequestMethod.POST)
public ModelAndView submit(@Valid final RegistrationBean registrationBean) {
  // registrationBean.getSecurityQuestion().getQuestion() == null
  // registrationBean.getSecurityQuestion().getAnswer() == null
}

Solution

All beans have to have getters/setters for all fields. Spring uses the default constructor and then uses the setters to mutate the object from the view.


Answer:

Can you try giving the RegistrationBean an appropriate getter/setter.

public class RegistrationBean {
  @Valid
  SecurityQuestion securityQuestion;
  String name;

  public SecurityQuestion getSecurityQuestion(){
        return securityQuestion;
  }
  public void setSecurityQuestion(SecurityQuestion q){
      this.securityQuestion = q;
  }    
  public SecurityQuestionType[] getSecurityQuestionOptions() {
    return SecurityQuestionType.values();
  }
}

Question:

I looked almost all answers related this problem on the web but could not figure out the problem in my code.

Here is my JSP page.

<form:form method="POST" commandName="category" modelAttribute="category" action="search_category">
    <form:input path="category_name" /> 
    <input type="submit" value="Submit">  
</form:form>

When I delete

<form:input path="category_name" /> 

It works fine. I can communicate with my controller. So the problem is related to this line.

@Controller
public class SearchCategory {

    @Autowired      
    private CategoryService categoryService;

    @RequestMapping(value = "/search_category",  method = RequestMethod.POST)
    public @ResponseBody String searchCategoryFromDatabase(@ModelAttribute("category") Category category, BindingResult result){        

        return "something";
    }
}

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

     <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/applicationContext.xml</param-value>  
    </context-param>  

    <filter>  
        <filter-name>hibernateFilter</filter-name>  
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>hibernateFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>      

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

This is my servlet-context.xml

<!--  Set the default page as index.jsp -->
<mvc:view-controller path="/" view-name="index"/>

<!-- Map resources --> 
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" /> 

<!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
</bean>

And my applicationContext.xml

<!-- Enable @Controller annotation support -->
    <mvc:annotation-driven />

    <context:annotation-config/>    

    <!--  Set the default page as index.jsp -->
    <mvc:view-controller path="/" view-name="index"/>

     <!-- Map resources --> 
    <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" /> 

    <!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
    <context:component-scan base-package="com.XXXX"/>

    <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with 
         username root and blank password. Change below if it's not the case -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/XXXX"/>
        <property name="username" value="XXXX"/>
        <property name="password" value="XXXX"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <!-- Hibernate Session Factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="packagesToScan">
            <array>
                <value>com.XXXX</value>
            </array>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
        </property>     
    </bean>

    <!-- Hibernate Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Activates annotation based transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager"/>  

I am probably doing something wrong in my XML files. I am new for this Spring - Hibernate staff so waiting for your help. Thanks..

This is the exception getting thrown

Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'category' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    at org.apache.jsp.index_jsp._jspx_meth_form_005finput_005f0(index_jsp.java:208)
    at org.apache.jsp.index_jsp._jspx_meth_form_005fform_005f0(index_jsp.java:168)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:100)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Answer:

If you're getting to index.jsp through something like http://localhost:8080/yourapp, I'll assume you have a <welcome-file> for it.

This means that the index.jsp generates the HTML without any pre-processing by Spring. You're trying to render this

<form:form method="POST" commandName="category" modelAttribute="category" action="search_category">
    <form:input path="category_name" /> 
    <input type="submit" value="Submit">  
</form:form>

where <form:form> is from Spring's tag library. First, note that you are using both commandName and modelAttribute. This is redundant. Use one or the other, not both. Second, when you specify either of these, the tag implementation looks for a HttpServletRequest attribute with the name specified. In your case, no such attribute was added to the HttpServletRequest attributes. This is because the Servlet container forwarded to your index.jsp directly.

Instead of doing that, create a new @Controller handler method which will added an attribute to the model and forward to the index.jsp view.

@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcomePage(Model model) {
    model.addAttribute("category", new Category()); // the Category object is used as a template to generate the form
    return "index";
}

You can get rid of this

<!--  Set the default page as index.jsp -->
<mvc:view-controller path="/" view-name="index"/>

Also, move any mvc configuration from your applicationContext.xml file to your servlet-context.xml file. That's where it belongs. Here's why.

Question:

I Have

Class Shape {
      //Implementation
}
Class Round extends Shape{
      //Implementation
}

Controller I Have

@Requestmapping(value="/view/form")
public ModelAndView getForm(){
ModelAndView mav=new ModelAndView();
mav.addObject("shape",new Round());
}


@RequestMapping(value="/submit", method = RequestMethod.POST)    
public ModelAndView submitForm(@ModelAttribute("shape") Shape shape){
         if(shape instanceof Round){ //**Not giving positive result**

         }
    }

in Jsp

<form:form action="/submit" commandName="shape" method="post">

//form tags

</form:form>

when I submit the form with Round object. At controller side ModelAttribute is not giving instance of Round . its giving instance of shape only. How to do this


Answer:

this will never work

<form:form action="/submit" commandName="shape" method="post">

you are submitting a shape from the form and expecting a shape in the controller method

public ModelAndView submitForm(@ModelAttribute("shape") Shape shape)

which will never give you an Round object.

simply submit a Round object from the form and use that.

 <form:form action="/submit" commandName="round" method="post">
 public ModelAndView submitForm(@ModelAttribute("round") Round round)

edited :-

have a hiddenInput type in the form which will tell controller the type of Shape it is passing, you can change the value of hidden tag dynamically upon user request.

<input type="hidden" name="type" value="round">

get the value of the type inside contoller and use it to cast the Shape object

     public ModelAndView submitForm(
     @ModelAttribute("shape") Shape shape,
     @RequestParam("type") String type)
     {
     if(type.equals("round")){
      //if type is a round you can cast the shape to a round
      Round round = (Round)shape; 
       }
    }

Question:

@Component
@Entity
@Table(name="menu")
@Configurable
public class Menu implements Serializable{      
    ....        
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    private Set<VoceMenu> voceMenuList; 

    public Set<VoceMenu> getVoceMenuList() {
        return voceMenuList;
    }

    public void setVoceMenuList(Set<VoceMenu> voceMenuList) {
        this.voceMenuList = voceMenuList;
    }
    .....   
}

I print a form to edit the menu, and its relative VoceMenu objects, this way:

<form:form action="editMenu" method="post" commandName="menu"> 
     Menu id<form:input path="id" maxlength="11"/><br/>       
     ...... 
    <c:forEach items="${menu.voceMenuList}" varStatus="counter">            
        <form:input path="voceMenuList[${counter.index}].id" maxlength="11"/>
             .....
    </c:forEach>
    <input type="submit">
</form:form>

But, when I try to save the object Menu, I get this error:

Invalid property 'voceMenuList[0]' of bean class [com.springgestioneerrori.model.Menu]: Cannot get element with index 0 from Set of size 0, accessed using property path 'voceMenuList[0]'


Answer:

The elements of a Set cannot be accessed by index. You will need to add methods which return a List wrapping your set.

@Component
@Entity
@Table(name="menu")
@Configurable
public class Menu implements Serializable{      
    ....        
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    private Set<VoceMenu> voceMenus; 

    public Set<VoceMenu> getVoceMenus() {
        return voceMenus;
    }

    public void setVoceMenus(Set<VoceMenu> voceMenus) {
        this.voceMenus = voceMenus;
    }

    //bind to this
    public List<VoceMenu> getVoceMenusAsList(){
        return new ArrayList<VoceMenu>(voceMenus);
    }
    .....   
}

JSP:

<form:form action="editMenu" method="post" commandName="menu"> 
     Menu id<form:input path="id" maxlength="11"/><br/>       
     ...... 
    <c:forEach items="${menu.voceMenusAsList}" varStatus="counter">            
        <form:input path="voceMenusAsList[${counter.index}].id" maxlength="11"/>
             .....
    </c:forEach>
    <input type="submit">
</form:form>

Question:

I know there are many similar questions here, but none of them solved my problem.

I'm using Spring 4.0.3 and Hibernate Validator 5.1.0. The problem occurs when I try to omit the path attribute of the <form:errors/> tag, so:

<form:errors path="contato.nome" /> works <form:errors path="*" /> works <form:errors /> doesn't work

I don't know why it happens. Spring javadocs (org.springframework.web.servlet.tags.form.ErrorsTag) says it should work like that:

Field only - set path to the field name (or path) Object errors only - omit path All errors - set path to *

Can you help me, please?

The interested code is in the 'edicao.jsp' and in the method 'confirmarEdicao' of the ContatoController.java. Sorry if my english is bad.


ContatoController.java

@Controller
@RequestMapping("/contatos")
public class ContatoController {

    @Autowired
    private ContatoService contatoService;

    @Autowired 
    private MessageSource messageSource;

    @RequestMapping(value = "/confirmarEdicao", method = RequestMethod.POST)
    public String confirmarEdicao(@Valid Contato contato, BindingResult bindingResult) {

        if(bindingResult.hasErrors()) {
            return "contatos/edicao";
        }

        contatoService.save(contato);
        return "redirect:/contatos";
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView form(HttpServletRequest request) {
        String message = messageSource.getMessage("teste", null, new Locale("pt", "BR"));
        System.out.println(message);
        return new ModelAndView("contatos/listagem")
            .addObject("contatos", contatoService.list());
    }

    @RequestMapping("/remover/{id}")
    public String remover(Contato contato) {
        contatoService.delete(contato);
        return "redirect:/contatos";
    }

    @RequestMapping("/editar/{id}")
    public ModelAndView formEdicao(Contato contato) {
        contato = contatoService.find(contato.getId());
        return new ModelAndView("contatos/edicao")
            .addObject(contato);
    }


    @RequestMapping(value = "/cadastrar")
    public String formCadastro() {
        return "contatos/cadastro";
    }

    @RequestMapping(value = "/confirmarCadastro", method = RequestMethod.POST)
    public String confirmarCadastro(@Valid Contato contato, BindingResult bindingResult, 
            RedirectAttributes redirectAttributes) {        

        if (bindingResult.hasFieldErrors()) {
            return "contatos/cadastro";
        }

        contatoService.save(contato);
        redirectAttributes.addFlashAttribute("mensagem", "Contato cadastrado com sucesso.");
        return "redirect:/contatos";
    }   

    @ResponseBody
    @RequestMapping(value = "/pesquisar/{nome}", method = RequestMethod.GET, 
        produces="application/json")
    public List<Contato> pesquisar(@PathVariable String nome) {
        return contatoService.findByName(nome);
    }

}

edicao.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Editar contato</title>
</head>
<body>

<c:set var="context">${pageContext.request.contextPath}</c:set>
<script type="text/javascript">var context = "${context}";</script>
<script src="${context}/resources/js/jquery-2.1.0.min.js"></script>
<script src="${context}/resources/js/contatos/edicao.js"></script>

<form:form commandName="contato" action="${context}/contatos/confirmarEdicao" method="post">
<form:errors/>
    <table>
        <form:hidden path="id"/>
        <tr>
            <td>Nome:</td> 
            <td><form:input path="nome" /></td>
        </tr>
        <tr>
            <td>Telefone:</td>
            <td><form:input path="telefone"/></td>
        </tr>
        <tr>
            <td><input type="button" value="Voltar" id="btn_voltar"/><input type="submit" value="Salvar"/></td>         
        </tr>
    </table>
</form:form>

</body>
</html>

Contato.java

package com.handson.model;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

public class Contato {

    private Long id;

    @Size(min = 3, message = "Nome deve ter no mínimo 3 caracteres")
    @NotEmpty(message = "O nome deve ser preenchido")
    private String nome;

    private String telefone;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public Contato withId(Long id) {
        setId(id);
        return this;
    }

    public Contato withTelefone(String telefone) {
        setTelefone(telefone);
        return this;
    }

    public Contato withNome(String nome) {
        setNome(nome);
        return this;
    }

    @Override
    public String toString() {
        return "Contato [id=" + id + ", nome=" + nome + ", telefone="
                + telefone + "]";
    }

}

Answer:

There are some keywords which should be defined:

  • path - EL-like path to an object or to a field of an object (e.g. foo, foo.bar or foo.bar.baz)
  • nested path - current path context stored as nestedPath request attribute (new paths are relative to this path)
  • object error - error connected with object itself (e.g. path is equal to foo)
  • field error - error connected with object field (e.g. path is foo.bar)

The tag <form:form commandName="foo"> defines nested path as nestedPath=foo. When you write <form:errors path="bar"> it tries to find errors defined for path foo.bar.

Lets say that you have errors connected with foo (object error), foo.bar and foo.bar.baz (nested field error). What this means:

  • if you enter <form:errors>, only errors bound to foo path are displayed => 1 message
  • if you enter <form:errors path="bar">, only errors bound to foo.bar path are displayed => 1 message
  • if you enter <form:errors path="*">, errors bound to foo and its child paths are displayed => 3 messages
  • if you enter <form:errors path="bar.*">, only child errors for foo.bar are diplayed => 1 message
  • if you enter <form:errors path="bar*">, errors bound to foo.bar and its child paths are diplayed => 2 messages

Checking on Errors class JavaDoc might give you additional insight.

Question:

I am trying to Validate Spring form which includes:

  1. Normal input fields
  2. Radio buttons whose options are set in controller (later may come from database)
  3. Check box whose options are also set in controller
  4. Select field which is also set in controller

Method 1 approach is followed.

Model:

public class Student {
    private Long id;

    @NotEmpty
    private String name;

    @NotEmpty
    private String address;

    @NotEmpty
    private String contactNumber;

    @NotEmpty
    private String grade;

    @NotEmpty
    private String gender;

    @NotEmpty
    @Email
    private String email;

    @NotEmpty
    private List<String> extraSubjects = new ArrayList<String>();

    /****  Corresponding getter setters ****/

}

Controller:-

@Controller
@RequestMapping("/student")
public class StudentController {
    private Logger logger = Logger.getLogger(StudentController.class);

    @RequestMapping("/home")
    public String getHomePage() {
        return "home";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addStudentForm(Model model) {
        Student student = new Student();
        model.addAttribute("studentForm", student);

        LinkedHashMap<Integer, String> gradeList = new LinkedHashMap<Integer, String>();
        gradeList.put(1, "Class 1");
        gradeList.put(2, "Class 2");
        gradeList.put(3, "Class 3");
        gradeList.put(4, "Class 4");
        gradeList.put(5, "Class 5");
        model.addAttribute("gradeList", gradeList);

        List<String> genderList = new ArrayList<String>();
        genderList.add("Male");
        genderList.add("Female");
        model.addAttribute("genderList", genderList);
        student.setGender("Male");

        List<String> extraSubjects = new ArrayList<String>();
        extraSubjects.add("Math II");
        extraSubjects.add("Population");

        List<String> extraSubjectsStd = new ArrayList<String>();
        extraSubjectsStd.add("Math II");
        extraSubjectsStd.add("Population");
        student.setExtraSubjects(extraSubjectsStd);

        extraSubjects.add("Computer");
        model.addAttribute("extraSubjects", extraSubjects);
        return "addstudent";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addStudent(
            @Valid @ModelAttribute("studentForm") Student student,
            BindingResult result) {

        if (result.hasErrors()) {
            logger.info("Returning add page with errors");
            return "addstudent";
        }
            //Stuffs to save in database 
        return "redirect:add";
    }
}

JSP Page:-

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
<style>
.error {
    color: #ff0000;
}

.errorblock {
    color: #000;
    background-color: #ffEEEE;
    border: 3px solid #ff0000;
    padding: 8px;
    margin: 16px;
}
</style>
</head>
<body>
    <form:form action="add" method="post" commandName="studentForm">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <table border="0">
            <tr>
                <td colspan="3" align="center"><h3>Add Student</h3></td>
            </tr>
            <tr>
                <td><form:label path="name">
                        <spring:message code="label.studentname" />
                    </form:label></td>
                <td><form:input path="name" /></td>
                <td><form:errors path="name" cssClass="error" /></td>
            </tr>
            <tr>
                <td><form:label path="address">
                        <spring:message code="label.address" />
                    </form:label></td>
                <td><form:input path="address" /></td>
                <td><form:errors path="address" cssClass="error" />
            </tr>
            <tr>
                <td>Contact Number:</td>
                <td><form:input path="contactNumber" /></td>
                <td><form:errors path="contactNumber" cssClass="error" />
            </tr>
            <tr>
                <td><form:label path="grade">
                        <spring:message code="label.grade" />
                    </form:label></td>
                <td><form:select path="grade">
                        <form:option value="" label="--- Select ---" />
                        <form:options items="${gradeList}" />
                    </form:select></td>
                <td><form:errors path="grade" cssClass="error" />
            </tr>
            <tr>
                <td>Gender:</td>
                <td><form:radiobuttons path="gender" items="${genderList}" />
                </td>
                <td><form:errors path="gender" cssClass="error" />
            </tr>
            <tr>
                <td>Extra Subjects:</td>
                <td><form:checkboxes items="${extraSubjects}"
                        path="extraSubjects" /></td>
                <td><form:errors path="extraSubjects" cssClass="error" />
            </tr>
            <tr>
                <td>Email Address:</td>
                <td><form:input items="${email}" path="email" /></td>
                <td><form:errors path="email" cssClass="error" />
            </tr>
            <tr>
                <td></td>
                <td align="center"><input type="submit" value="Save" /></td>
                <td></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

Now, When I run my application, the forms get loaded while I perform get operation. The value in the radio buttons, select fields, and checkboxes are loaded and selected as well. If I submit my form with no validation errors it works fine but when I send my form with validation errors, I get following errors:

StackTrace:-

Jun 26, 2014 4:32:00 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalArgumentException: 'items' must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag.setItems(AbstractMultiCheckedElementTag.java:85)
    at org.apache.jsp.WEB_002dINF.pages.addstudent_jsp._jspx_meth_form_005fradiobuttons_005f0(addstudent_jsp.java:810)
    at org.apache.jsp.WEB_002dINF.pages.addstudent_jsp._jspService(addstudent_jsp.java:360)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

DEBUG org.springframework.web.servlet.DispatcherServlet- Error rendering view [org.springframework.web.servlet.view.JstlView: name 'addstudent'; URL [/WEB-INF/pages/addstudent.jsp]] in DispatcherServlet with name 'mvc-dispatcher'
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/pages/addstudent.jsp at line 62

59:             </tr>
60:             <tr>
61:                 <td>Gender:</td>
62:                 <td><form:radiobuttons path="gender" items="${genderList}" />
63:                 </td>
64:                 <td><form:errors path="gender" cssClass="error" />
65:             </tr>    

Stacktrace:
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.IllegalArgumentException: 'items' must not be null
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag.setItems(AbstractMultiCheckedElementTag.java:85)
        at org.apache.jsp.WEB_002dINF.pages.addstudent_jsp._jspx_meth_form_005fradiobuttons_005f0(addstudent_jsp.java:810)
        at org.apache.jsp.WEB_002dINF.pages.addstudent_jsp._jspService(addstudent_jsp.java:360)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
        ... 42 more
    Jun 26, 2014 4:32:00 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/sms] threw exception [An exception occurred processing JSP page /WEB-INF/pages/addstudent.jsp at line 62

    59:             </tr>
    60:             <tr>
    61:                 <td>Gender:</td>
    62:                 <td><form:radiobuttons path="gender" items="${genderList}" />
    63:                 </td>
    64:                 <td><form:errors path="gender" cssClass="error" />
    65:             </tr>


    Stacktrace:] with root cause
    java.lang.IllegalArgumentException: 'items' must not be null
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag.setItems(AbstractMultiCheckedElementTag.java:85)
        at org.apache.jsp.WEB_002dINF.pages.addstudent_jsp._jspx_meth_form_005fradiobuttons_005f0(addstudent_jsp.java:810)
        at org.apache.jsp.WEB_002dINF.pages.addstudent_jsp._jspService(addstudent_jsp.java:360)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

I am able to figure out that the List that I have used for specifying genders, gradeList, extraSubjects are appear to be NULL after post request.

  • How can I render those values again in form after post if validation fails?
  • Should I have to set the value in lists on POST method too as in GET method?

Answer:

In you get part, you populate you model, not only with studentForm but also with genderList, gradeList and extraSubjects. And you use all in your jsp.

In the post part, you have a studentForm initialized by the parameters in the request. But your never initialize the lists. So in the JSP, ${gradeList}, ${genderList} and ${extraSubjects} and all null, so the error.

You have to initialize the three lists in addStudent before forwarding to the view if validation fails. You can do that either as proposed by Neil McGuigan, ie by having spring to automatically populate the model, or by populating it yourself. For example, you could have :

void initLists(Model model) {
    LinkedHashMap<Integer, String> gradeList = new LinkedHashMap<Integer, String>();
    gradeList.put(1, "Class 1");
    gradeList.put(2, "Class 2");
    gradeList.put(3, "Class 3");
    gradeList.put(4, "Class 4");
    gradeList.put(5, "Class 5");
    model.addAttribute("gradeList", gradeList);

    List<String> genderList = new ArrayList<String>();
    genderList.add("Male");
    genderList.add("Female");
    model.addAttribute("genderList", genderList);

    List<String> extraSubjects = new ArrayList<String>();
    extraSubjects.add("Math II");
    extraSubjects.add("Population");
    extraSubjects.add("Computer");
    model.addAttribute("extraSubjects", extraSubjects);
}

and call initLists both in addStudentForm and in addStudent. Example code for last :

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addStudent(
        @Valid @ModelAttribute("studentForm") Student student,
        BindingResult result, Model model) {

    if (result.hasErrors()) {
        logger.info("Returning add page with errors");
        initLists(model);
        return "addstudent";
    }
        //Stuffs to save in database 
    return "redirect:add";
}

Of course, all list initialization should be removed from addStudentForm once you call initLists from there ...

Question:

I am pretty new in Spring MVC and I have some difficulties to understand how exactly works the tag.

So I have the following situation.

Into a controller I have this method:

@RequestMapping(value = "/consultazioneMinisteriale", method = RequestMethod.GET)
public String consultazione(Locale locale, Model model) {

    List<Twb1012Regione> listaRegioni = geograficaService.getListaRegioni();

    System.out.println("Numero regioni: " + listaRegioni.size());

    model.addAttribute("listaRegioni", listaRegioni);

    return "utenteMinisteriale/consultazione";
}

As you can see this method retrieve a List of Twb1012Regione object and put it into the model object so it will be available into the consultazione.jsp page.

So the Twb1012Regione class is a model object like this:

@Entity
@Table(name="anagrafiche.TWB1012_REGIONE")
@NamedQuery(name="Twb1012Regione.findAll", query="SELECT t FROM Twb1012Regione t")
public class Twb1012Regione implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="COD_REG")
    private String codReg;

    @Column(name="DES_REG")
    private String desReg;

    .....................................
    .....................................
    OTHER FIELDS
    .....................................
    .....................................
}

Where the codReg field univocally identify the object and the desReg contain the value that I want to show as value into the tag.

Finnaly this is the code of my consultazione.jsp view:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ page session="false"%>
<%@  taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html>
<html>
    <head>
        <title>Home</title>
    </head>

    <body class="azure">
        <h1>Hello World</h1>

        <!--  <div> ${listaRegioni} </div> -->


        <div>
            <label>Regioni:</label>
            <form:select path="listaRegioni" items="${listaRegioni}"/>
        </div>

    </body>
</html>

The problem is that doing in this way I obtain the select dropdown but it show the reference of all my Twb1012Regione objects and not the name of the desReg field.

This is the HTML rendered output:

<select>
    <option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@5a259924">it.myCompany.myProject.anagrafiche.Twb1012Regione@5a259924</option>
    <option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@4a87c8d3">it.myCompany.myProject.anagrafiche.Twb1012Regione@4a87c8d3</option>
    <option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@815b53a">it.myCompany.myProject.anagrafiche.Twb1012Regione@815b53a</option>
    .................................................
    .................................................
    .................................................
</select>

Why? What am I missing? How can I shoe the value of the desReg field of each Twb1012Regione instead the reference of the objects?

EDIT-1:

I tryied to change into:

<form:select path="regioni">
   <form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>

But now when the page is rendered I obtain this error message into my stacktrace:

12:44:52,112 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/WIFIPNSD].[jsp]] (http-localhost/127.0.0.1:8080-4) JBWEB000236: Servlet.service() for servlet jsp threw exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'regioni' available as request attribute

Why? What is wrong? How can I solve it?


Answer:

  • In <form:select> tag you must put in the path the name of the bean attribute will keep the id of the selected item.
  • In <form:options>:
    • items: the bean attribute containing the list of selectable items
    • itemLabel: the description to be shown in the dropbox
    • itemValue: the field to be saved (usually id) in the path of the <form:select>

<form:select path="regioni">
    <form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>

Will show you a dropbox with all descriptions (desReg) of the regions, and will keep the the codReg of the selected item in the bean attribute regioni

Question:

I entered in Spring Web MVC Framework not long ago thus I am a complete beginner. Right now I am implementing a basic form for my first web application.

In the same time I would like to say that I've been seeking a solution for my problem for whole day. I apolgize in advance if the solution for similar problem was already published.

Source code:

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="si.src.controllers" />
    <context:component-scan base-package="si.src.validators" />

    <bean id="viewResolver" 
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>            
</beans>

index.jsp

<!-- language: lang-jsp -->
<html>
<head>
    <title>Spring 3.0 MVC Series - Index</title>
</head>
<body>
    <br>
    <div align='center'>
        <p>
        <h1>Example - Spring Application</h1>
        The "index.jsp" is the entry point for our application.
        This is my first test. Work!            
        <p>
        <a href="login.html">Welcome! Click Here to Login</a>           
    </div>      
</body>
</html>

login.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>
    <title>Spring Sample - Login Page</title>
</head>
<body>
    <h3>Login Page</h3><br>
    <form:form id="form" method="post" commandName="loginForm">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <table>
            <tr>
                <td><form:label path="username">Username</form:label></td>
                <td><form:input path="username" /></td>
                <td><form:errors path="username" cssClass="error"/></td>
            </tr>
            <tr>
                <td><form:label path="username">Password</form:label></td>
                <td><form:input path="password" /></td>
                <td><form:errors path="password" cssClass="error"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Sign in" /></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

LoginFormController.java

package si.src.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;

import si.src.logic.Login;
import si.src.validators.LoginValidator;

@Controller
@RequestMapping(value="/login")
public class LoginFormController extends SimpleFormController{

    public LoginFormController(){
        setCommandClass(Login.class);   //Form's values will store into the Login object    
        setCommandName("loginForm");    //If HTML form action value with named "loginForm" is sumbitted, Spring will forward request to this form controller
    }

    @RequestMapping(method=RequestMethod.POST)
    protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors) throws Exception{

        LoginValidator validator = new LoginValidator();        
        Login userLogin = (Login) command;
        validator.validate(userLogin, errors.getBindingResult());

        if(errors.hasErrors()){
            userLogin.setUsername("");
            userLogin.setPassword("");
            System.out.println("Ne");
            return new ModelAndView("login","loginForm", userLogin);

        }
        else{
            System.out.println(userLogin);
            System.out.println(userLogin.getUsername() + " " + userLogin.getPassword());    
            return new ModelAndView("success","userLogin", userLogin);
        }
    }

    protected Object formBackingObject(HttpServletRequest request) throws Exception {   
        //Initialize the values in the form. Not necessary
        Login userLogin = new Login();
        userLogin.setUsername("Admin");
        return userLogin;
    }

}

LoginValidator.java

package si.src.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.ValidationUtils;

import si.src.logic.Login;

public class LoginValidator implements Validator{

    public boolean supports(Class aClass) {
        //just validate the Login instances
        return Login.class.isAssignableFrom(aClass);
    }

    public void validate(Object obj, Errors errors) {
        Login login = (Login) obj;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username","required-username", "Enter username");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password","required-password", "Enter password");    
    }   
}

Login.java

package si.src.logic;

public class Login {

    private String username;
    private String password;

    public Login(){}

    public void setUsername(String username){
        this.username=username;
    }

    public String getUsername(){
        return username;
    }

    public void setPassword(String password){
        this.password=password;
    }

    public String getPassword(){
        return password;
    }   
}

I suppose the error messages obtained by LoginValidator object are not correctly "binded" with my login.jsp file. I guess that is the reason why the error messages i.e "Enter username" and "Enter password" don't show up when my form is poorly fulfilled.

My question is: Why the error messages are not showing up in a login.jsp?


Answer:

I have similar problem. Also I have changed the type of return, but I have the need to trasfert date from controller to JSP. I use object model, but I have the same result of object modelandview.

How can I transfer data without losing the display of form:errors?

@RequestMapping(value="/addCaregiver", method= RequestMethod.POST)
public String addCaregiver(@ModelAttribute("caregiver") @Valid  
Caregivers caregiver, BindingResult result,Principal principal, Model model){


caregiverValidator.validate(caregiver, result);
if(result.hasErrors()){

model.addAttribute("caregivers",pp.getCaregiverses(principal.getName()));
model.addAttribute("error", "true");
model.addAttribute("caregiver", new Caregivers());
return "caregiver";
        }
return "intro";
}

I solved, don't create a new istance, I delete this row:

model.addAttribute("caregiver", new Caregivers());

Question:

I have problems with getting my JSP view right. What I intend to do is to send a List that contains questions and each question object is a text field and a List with alternatives.

My intention is to be able to edit multiple questions (both to be able to edit the text/name of the question and edit the containing alternatives).

My backing object is now sending an List question.

Here is my JSP which are failing with invalid property of bean class.

        <form:form commandName="question">
        <form:errors path="*">
            <fieldset class="stdframe">
                <legend>Question</legend>
            </fieldset>
        </form:errors>

        <div class="stdframe">
            <c:forEach var="q" items = "${question}" varStatus = "s">           
                <p><b>Question:</b></p>
                <p><form:input size="67" path="${q.text}"/></p>
                <br/>
                ${q.text}
                <ul>
                    <c:forEach var="alternative" items = "${q.alternatives}" varStatus = "t">
                        ${alternative.text}
                        <li><form:input path = "${alternative[$t.index].text}" /></li>
                    </c:forEach>
                </ul>
                <br/>
            </c:forEach>
                <input type="submit" class="submit" value="Save" />
                <input type="button" class="button" onClick="back()" value="Back"/>
        </div>
</form:form>

I have tried both ${q.text} and ${q[$s.index].text}. When I just print ${q.text} it shows the correct text for the question object. Same goes for alternative.

What can I do to correctly bind the form to the objects?

In addition when I store an object which contains a list of other object, will the list be stored itself in the database?


Answer:

You may need to wrap your List in a simple object with the List as a field:

class MyListWrapper { List questions; } // etc.

If you use that as your command/form object, you should be able to do something like this in the JSP:

<form:form commandName="wrapper">
// ... 
        <c:forEach var="q" items = "${wrapper.questions}" varStatus = "s">
           <p><b>Question:</b></p>
            <p><form:input size="67" path="questions[${s.index}].text"/></p>
// ... 
               <c:forEach var="alternative" items = "${q.alternatives}" varStatus = "t">
                    ${alternative.text}
                    <li><form:input path = "questions[${s.index}].alternatives[${t.index}].text" /></li>