Spring mvc submit form not found error 404

2019-09-10 06:59发布

问题:

I am using spring mvc and I have created a jsp page.

redmn.jsp

<form id="forma" name="forma" action="something" method="post" enctype="multipart/form-data" >
    //some fields
 <input type="submit" value="Valider" />

  </form>

Then I have created one controller to handle my request:

@Controller
public class SecondController{


@RequestMapping(value="/something", method= RequestMethod.POST)
public String addRes( HttpServletRequest req,
        BindingResult result,
        ModelMap model,
        @RequestParam("file") MultipartFile file){
 // some treatements
 return "redmn"
}

When I am clicking the submit button, I am getting 404 error. Please someone help to resolve this issue.

回答1:

I think you are missing context path. In order to get correct request path you should append context path.

if you are using spring tag lib

<spring:url var="something" value="/something"></spring:url>

If you are using jstl

<c:url var="something" value="/something"/>

and then in your form tag specify action

 action="${something}"


回答2:

When you have POST method, in the JSP you should add modelAttribute

<form:form method="post" modelAttribute="something" action="${something}">

In your controller the things should be this way:

public String saveOrUpdateUser(@ModelAttribute("userForm") User user,
    BindingResult result, Model model) {
//...
}

Further information about handling you could find in this tutorial http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-example/