java ee - Context root not coming while submitting Spring form - issues with url pattern -
below contoller , web xml , jsp page 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 view resolver. per class level url pattern defined in contoller if navigate below url http://localhost:9080/examplespring or http://localhost:9080/examplespring/
controll going jsp page , asking details. after entering details if submit form form action action="submitdetails.htm" mentioned in action , throwing 404 error. , url in address bar coming localhost:9080/submitdetails.htm missing context root project. question doing wrong in requehst mapping url section ?
as per understanding
1.the form submitted using post mathes pattern 2. /submitdetails.htm matching pattern /* in class level url pattern mapping request should enter controller, 3. , lastly method level mapping in controller's method having same url pattern amd request method action amd methood in jsp should enter controllers method , print in console--- not happeing.
could please me understand this?
your problem solved after changing /* / because, every url contains 2 parts
1)servlet path
2)path info
consider following code
<servlet-mapping> <servlet-name>exactmatchservlet</servlet-name> <url-pattern>/abc</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>pathinfoservlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>longpathinfoservlet</servlet-name> <url-pattern>/abc/*</url-pattern> </servlet-mapping> </servlet-mapping>
here if call url like
/abc call exactmatchservlet
servlet path -> /abc
path info -> null
/abc/pqr call pathinfoservlet
servlet path -> /abc
path info -> /pqr
/something/random call pathinfoservlet
servlet path -> null
path info -> /something/random
when replacing <url-pattern>/*</url-pattern>
<url-pattern>/</url-pattern>
and call /something/random then
servlet path -> /something/random
path info -> null
so changing /* / change mapping of url parts(servlet path , path info)
for region container can't find servlet url , give 404 error.
Comments
Post a Comment