Spring display of action form on the controller

I am new to Spring framework and created controller using 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 extraction method is called to get contact information when the user clicks a link to jsp

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

Then it successfully returns the contact object and displays on the user's screen for editing and saving. My jsp form tag is as follows

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

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

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

then how should it be

/myapp/app/contacts/add.html

I know that there is something that I am not doing right, but what exactly I cannot understand. Evaluate if any of you can help me solve the problem.

Thanks aa

+6
source share
3 answers

Using

 <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 recommended that you use c:url in each application, rather than directly using the URL in the <a> tag.

+7
source

Use the /contacts/add.html attribute in action
Edit

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

to

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

Use the servletRelativeAction attribute to map to the desired controller action. I assume that your desired controller appears as '/ contacts / add' not 'add.html'. You want to click on the controller not in appearance.

0
source

Source: https://habr.com/ru/post/925382/


All Articles