Question

I have simple application where I am using validation (which works perfectly) however I have issues passing error message back to form. In examples I have seen it looks like it is "magically" passed back to form without manually setting any attribute or whatsoever.

This is my controller code :

@RequestMapping(value = "/createMember", method = RequestMethod.GET)
public ModelAndView showContacts() {

    return new ModelAndView("createMember", "command", new Member());
}

@RequestMapping(value = "/createMember", method = RequestMethod.POST)
public ModelAndView addContact(@ModelAttribute("member")
                        @Valid Member member, BindingResult result) {

    if(result.hasErrors()) {
        return new ModelAndView("createMember", "command", member);
    }     
    mba.addMember(member);         
    return getPages();
}   

And this is my createMember.jsp :

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

<html>
<head>

<script src="<c:url value="/resources/core/jquery.1.10.2.min.js" />"></script>
<script src="<c:url value="/resources/core/jquery.autocomplete.min.js" />"></script>
<link href="<c:url value="/resources/core/main.css" />" rel="stylesheet">

</head>
<body>
<form:form method="post" action="createMember.html">

    <table>
    <tr>
        <td><form:label path="firstName">First Name</form:label></td>
        <td><form:input path="firstName" value="${member.firstName}" /></td> 
        <form:errors path="firstName" cssclass="error"></form:errors>
    </tr>    
    <tr>
        <td><form:label path="lastName">Last Name</form:label></td>
        <td><form:input path="lastName" value="${member.lastName}" /></td>
        <form:errors path="lastName" cssclass="error"></form:errors>
    </tr>
    <tr>
        <td><form:label path="email1">Email</form:label></td>
        <td><form:input value="${member.email1}" path="email1" /></td>
        <form:errors path="email1" cssclass="error"></form:errors>
    </tr>

    <tr>
        <td colspan="2">
            <input type="submit" value="Add Contact"/>
        </td>
    </tr>
</table>  

</form:form>

<a href="${pageContext.servletContext.contextPath}"> Back to main page </a> 

</body>
</html>

I am using Spring 3.2.2, Tomcat 7, hibernate-validator

Validation class :

import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
public class Member {

    private int memberID;
    private int gender;

    @Size(min=2, max=30)
    private String firstName;

    @Size(min=2, max=30)
    private String lastName;

    @NotEmpty @Email
    private String email1;

    //...getters and setters here
}
Was it helpful?

Solution

Try @ModelAttribute("command") instead of @ModelAttribute("member") in your controller class.

OTHER TIPS

Sorry I can't solve this directly but here is a log4j config file that might help. Just make sure it gets deployed to WEB-INF/classes inside your WAR file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
     <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p: %c{1} - %m%n" />
        </layout>
    </appender>

      <!--  Log levels:
          ALL,
          TRACE,
          DEBUG,
          INFO,
          WARN,
          ERROR,
          FATAL
       -->

    <!-- 3rdparty Loggers -->
  <logger name="org.springframework">
      <level value="TRACE" />
  </logger>

  <logger name="org.springframework.context">
      <level value="TRACE" />
  </logger>

  <logger name="org.springframework.http">
      <level value="TRACE" />
  </logger>

  <logger name="org.springframework.web">
      <level value="TRACE" />
  </logger>

    <logger name="org.springframework.core">
        <level value="TRACE" />
    </logger>

  <root>
    <priority value ="TRACE" /> 
    <appender-ref ref="console" />
  </root>

</log4j:configuration>

Also examine your pom.xml or gradle config file. Spring version mismatches can cause all sorts of weird problems.

Your form has not backed with the model (To do validation Spring must know which field in models are belongs to which fields in the view). To do that you need to add modelAttribute attribute is your Spring form. So these are the lines you need to worry.

In controller

@RequestMapping(value = "/createMember", method = RequestMethod.POST)
public ModelAndView addContact(@ModelAttribute("member") @Valid Member member, BindingResult result)

The value you gives to @ModelAttribute("member") should be mentioned in the form.

In view

<form:form method="post" modelAttribute="member" action="createMember.html">

If you don't want mentions the modelAttribute="whateveryournamehere" you should use @ModelAttribute("command") as, in the Controller. Then you don't want to use modelAttribute="command".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top