为什么我的Spring Controller会把我引向错误的页面?

时间:2012-11-13 14:24:28

标签: spring-mvc

我正在编写我的第一个Spring 3.0.5 MVC应用程序,并且很困惑为什么我的控制器映射没有按照我的预期进行。

我有一个VerifyPasswordController,在用户尝试通过输入他的名字和密码登录后调用。

// 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;
 }

我可以在调试器中验证是否正在调用此方法,但之后会显示verifyPassword页面而不是chooseOperation页面。 WebLogic的控制台输出似乎表明我的映射是正确的:

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'

这是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
}

我欢迎任何建议,特别是用于调试此类映射问题的“通用”技术。顺便说一句,我也试图“重定向”到所需的页面,但得到了相同的结果。

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>

常数:

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

2 个答案:

答案 0 :(得分:0)

请记住,您返回的字符串是jsp的名称(请参阅您的内部资源视图),如果您想重定向到其他控制器,您应该返回“redirect:controllerPath”,您应该检查哪个是nextPage的最终值如果是CHOOSE_OPERATION_PAGE值。

答案 1 :(得分:0)

尝试指定其他控制器的映射,即ChooseOperationController

@Controller
@RequestMapping(/chooseOps)
@SessionAttributes("leaveRequestForm")
public class ChooseOperationController implements PageIfc, AttributeIfc {
        //your controller logic
    }

然后当您指向成功视图时,指定完整路径以了解它必须引用不同的控制器。

String CHOOSE_OPERATION_PAGE = redirect:chooseOps/chooseOperation;