1

I am using 3.1.1 here.

This works fine, the form is dislayed correctly

@RequestMapping(value={"/universities"}, params="new", method=RequestMethod.GET)
public String addUniversity(Model model) {
    model.addAttribute("addForm", AddForm.newUniversity());
    return "page/add-university";
}

When testing the validation error (by emptying the fields), this results in 500 internal server error, and displays stacktraces with the main message : Neither BindingResult nor plain target object for bean name 'addForm' available as request attribute

This is the method :

@RequestMapping(value={"/universities"}, method=RequestMethod.POST)
public String submitNewUniversity(@Valid AddForm form, BindingResult binding) {
    if (binding.hasErrors()) {
        return "page/add-university";
    }

    // do others here ...
}

Here is my JSP :

<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" 
    session="false" %>

<s:url var="action" value="/universities" />
<sf:form method="POST" modelAttribute="addForm" action="${action}">         
   <fieldset> 
   <table cellspacing="0">
      <tr>
         <th><sf:label path="name">University:</sf:label></th>
         <td><sf:input path="name" size="30" /> <br/>
             <sf:errors path="name" cssClass="error" />
         </td>
      </tr>
      .....
</sf:form>

So far what i've researched are these :

  • The bean name is correct, which is "addForm", both in the controller and the JSP tile.
  • The view exists, since displaying the form works fine.
  • The field names in the JSP are also correct, since displaying the form works fine.
  • The order of parameters are fine, BindingResult follows the command bean
  • I am sure that the submitNewUniversity is called by the output of the logs i added

I wonder what i did wrong here ?

1 Answer 1

1

Found it !

I changed from

@Valid AddForm form

to

@Valid @ModelAttribute("addForm") AddForm form

Although this works fine, i tried removing the annotation and changing the form name to be the same variable name as the request attribute :

@Valid AddForm addForm

and this still throws the same error.

I guess i'll just have to stick with @ModelAttribute from now on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.