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