Why does my Spring Controller direct me to the wrong page?

Posted by kc2001 on Stack Overflow See other posts from Stack Overflow or by kc2001
Published on 2012-11-13T14:24:28Z Indexed on 2012/11/14 17:01 UTC
Read the original article Hit count: 251

Filed under:

I am writing my first Spring 3.0.5 MVC app and am confused about why my controller mappings aren't doing what I expect.

I have a VerifyPasswordController that is called after a user tries to log in by entering his name and password.

// Called upon clicking "submit" from /login
@RequestMapping(value = "/verifyPassword", method = RequestMethod.POST)
@ModelAttribute("user")
public String verifyPassword(User user,
        BindingResult result) {

    String email = user.getEmail();
    String nextPage = CHOOSE_OPERATION_PAGE; // success case
    if (result.hasErrors()) {
        nextPage = LOGIN_PAGE;
    } else if (!passwordMatches(email, user.getPassword())) {
        nextPage = LOGIN_FAILURE_PAGE;
    } else {
        // success
    }
    return nextPage;
 }

I can verify in the debugger that this method is being called, but afterwards, the verifyPassword page is displayed rather than the chooseOperation page. The console output of WebLogic seems to show that my mapping are correct:

INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/chooseOperation] onto handler 'chooseOperationController'
INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/chooseOperation.*] onto handler 'chooseOperationController'
INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/chooseOperation/] onto handler 'chooseOperationController'

Here is the ChooseOperationController:

@Controller
@SessionAttributes("leaveRequestForm")
public class ChooseOperationController implements PageIfc, AttributeIfc {

    @RequestMapping(value = "/chooseOperation")
    @ModelAttribute("leaveRequestForm")
    public LeaveRequest setUpLeaveRequestForm(
            @RequestParam(NAME_ATTRIBUTE) String name) {
        LeaveRequest form = populateFormFromDatabase(name);
        return form;
    }

    // helper methods omited
}

I welcome any advice, particularly "generic" techniques for debugging such mapping problems. BTW, I've also tried to "redirect" to the desired page, but got the same result.

servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.engilitycorp.leavetracker" />

    <beans:bean id="leaveRequestForm"
          class="com.engilitycorp.leavetracker.model.LeaveRequest" />

</beans:beans>

The constants:

String LOGIN_FAILURE_PAGE = "loginFailure";
String LOGIN_PAGE = "login";
String CHOOSE_OPERATION_PAGE = "chooseOperation";

© Stack Overflow or respective owner

Related posts about spring-mvc