Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context root not coming while submitting Spring form - issues with url pattern

Below are my contoller , web xml and jsp page which uses spring form.

Controller

@Controller
@RequestMapping(value = {"/*", "/login"})
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld(final Model model) {
        model.addAttribute("bodyPage", "body.jsp");
        User user = new User();
        Address address = new Address();

        user.setAddress(address);

        model.addAttribute("myUser", user);
        System.out.println("hello world");
        return "login";
    }


    @RequestMapping(value = "/submitDetails", method = RequestMethod.POST)
    public ModelAndView submitDetais(final Object command) {

        System.out.println("inside submitDetais ");
        User user = (User) command;
        return new ModelAndView("result", "user", user);
    }
}

Web Xml

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

Jsp Page

<form:form commandName="myUser" modelAttribute="myUser" method="POST" action="submitDetails.htm" >
    <table>
        <tr>
            <td>First Name:</td>
            <td><form:input path="firstName" /></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><form:input path="lastName" /></td>
        </tr>

        <tr>
            <td>Address 1:</td>
            <td><form:input path="address.address1"  /></td>
        </tr>

        <tr>
            <td>Address 2:</td>
            <td><form:input path="address.address2"  /></td>
        </tr>

        <tr>
            <td colspan="2"><input type="submit" value="Save Changes" /></td>
        </tr>
    </table>
</form:form>

Dispathcher servlet has only view resolver. Now as per the class level url pattern defined in Contoller if I navigate to the below url http://localhost:9080/ExampleSpring or http://localhost:9080/ExampleSpring/

controll is going to my jsp page and asking for details. After entering details if I submit the form with the form action as action="submitDetails.htm" as mentioned in action , its throwing 404 error. And the url in the address bar is coming as localhost:9080/submitDetails.htm which is missing the Context root for the project. My question is what am I doing wrong in the requehst mapping url section ?

As per my understanding

1.the form is submitted using post which mathes the pattern 2. /submitDetails.htm is matching the pattern /* in the class level url pattern mapping so the request should enter the controller, 3. and lastly the method level mapping in controller's method is having same url pattern amd request method as the action amd methood in jsp so it should enter the controllers method and print in the console--- which is not happeing.

Could anyone please help me understand this?

like image 896
Sambuddha Avatar asked Mar 17 '23 01:03

Sambuddha


1 Answers

From my understanding you are missing context path in your form's action attribute. Try to define it as following:

<form:form commandName="myUser" modelAttribute="myUser" method="POST" action="${pageContext.servletContext.contextPath}/submitDetails.htm" >
like image 167
Stan Avatar answered Apr 26 '23 19:04

Stan