Sunday, 2 June 2013

Spring 3 MVC extra @RequestMapping value getting appended to returned view

Spring 3 MVC extra @RequestMapping value getting appended to returned view

I have a view 'hi.jsp' with user name and password text fields. I need to submit 'hi.jsp' to 'LoginController.java'. If there is any error in the data submitted then 'LoginController.java' must redirect the request back to 'hi.jsp' (with text fields retaning the entered data) with respective error messages. After changing data and re-submitting 'hi.jsp' I get 404 error.
So the first submission is successful however problem occurs during second submission. The source code of files is mentioned below:
hi.jsp
<%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix="c" %>
  <%@ taglib uri=http://www.springframework.org/tags/form prefix="s" %>
  <%@ page session="false" %>
  <html>
      <body>   
        <s:form method="POST" modelAttribute="loginObj" action="login/validatelogin">
           <label for="userName">UserName</label>
           <s:input path="userName" id="userName" size="15"/><br>
           <div style="{color:red}"> <s:errors path="userName"></s:errors></div>

           <label for="password">Password</label>
           <s:input path="password" id="password" size="15" /><br>
           <s:errors path="password"></s:errors>

           <input name="submit" type="submit" value="login"/>     
    </s:form>
   </body>
</html>
LoginController.java
package rajeev.spring.spitter.mvc.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/login")
public class LoginController {

    @RequestMapping(value="/validatelogin", method=RequestMethod.POST)
    public String validateLogin(@Valid @ModelAttribute("loginObj") LoginBean loginObj, BindingResult bindingResult)
    {
        System.out.println(bindingResult.hasErrors());
        if(bindingResult.hasErrors())
        {
            return "hi";
        }
        return "home";
    }
}
spitter-servlet.xml (spring configuration)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=http://www.springframework.org/schema/beans
    xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
    xmlns:mvc=http://www.springframework.org/schema/mvc
    xmlns:context=http://www.springframework.org/schema/context
    xmlns:jdbc=http://www.springframework.org/schema/jdbc
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="rajeev.spring.spitter.mvc.controller"></context:component-scan>
     <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix"

No comments:

Post a Comment