Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mapping form's action to the controller

Tags:

spring

I am new to the Spring Framework and I have created a controller with the method

@RequestMapping("/fetch/{contactId}")
public String getContact(@PathVariable("contactId") Long contactId,
        Map<String, Object> map, HttpServletRequest request,
        HttpServletResponse response) {

    Contact contact = contactService.get(contactId);
    map.put("contact", contact);
    return "contact";
}

This fetch method is called to retrieve the contact details when the user clicks on the link on a jsp

<td><a href="fetch/${contact.id}" class="edit">Edit</a></td>

It then successfully returns the contact object and displays on the screen for the user to change and save. The form tag of my jsp is like this

<form:form method="post" action="add.html" commandName="contact"
        id="contact" onsubmit="return validateContact(this)">

Now the problem is when I try to submit the page to another method in the same controller the URL changes to

/myapp/app/contacts/fetch/add.html

whereas it should be

/myapp/app/contacts/add.html

I know there is something which I am not doing correctly but what exactly I am not able to figure out. Appreciate if any one of you could help me resolve the issue

Thanks AA

like image 341
Ashish Abrol Avatar asked Sep 14 '12 05:09

Ashish Abrol


People also ask

What does @RequestMapping do in Spring?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.

What is Spring form commandName?

The commandName attribute is the most important attribute in the form tag, which specifies the model attribute name that contains a backing object and the properties of this object will be used to populate the generated form.

How does Spring BindingResult work?

[ BindingResult ] is Spring's object that holds the result of the validation and binding and contains errors that may have occurred. The BindingResult must come right after the model object that is validated or else Spring will fail to validate the object and throw an exception.


2 Answers

Use

<c:url var="addUrl" value="/contacts/add.html"/>
<form:form method="post" action="${addUrl}" commandName="contact"
      id="contact" onsubmit="return validateContact(this)">

In general, it is recomended to use c:url in every application internal instead of direct use of the url in a <a> tag.

like image 189
Ralph Avatar answered Oct 09 '22 03:10

Ralph


<form:form method="post" servletRelativeAction="/contacts/add" commandName="contact"
        id="contact" onsubmit="return validateContact(this)">

Use attribute servletRelativeAction to map to desired controller action. I assume that your desired controller is mapped as '/contacts/add' not 'add.html'. You want to hit the controller not the view.

like image 38
Sten Avatar answered Oct 09 '22 04:10

Sten