0

我正在编写我的第一个 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'

这是选择操作控制器:

@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";
4

2 回答 2

0

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

于 2012-11-13T21:47:19.473 回答
0

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

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

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

String CHOOSE_OPERATION_PAGE = redirect:chooseOps/chooseOperation;
于 2012-11-14T00:33:03.383 回答