如何将请求映射到方法之上没有显式注释的方法?例如,以下请求:
http://somedomain:8080/SampleSpring/access/loginFailed
应该映射到
“AccessController”的“loginFailed”方法,无需对方法进行显式注释,例如:
@RequestMapping("/access/loginFailed")
这是我的弹簧配置:
<context:component-scan base-package="com.robikcodes.samplespring"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="basePackage" value="com.robikcodes.samplespring.controller"/>
<property name="caseSensitive" value="true"/>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
这是我的控制器:
@Controller
public class AccessController{
@RequestMapping(method = RequestMethod.GET)
public void login(ModelMap m) {}
@RequestMapping(method = RequestMethod.GET)
public String loginFailed(ModelMap m) {
m.addAttribute("error", "true");
return "access/login";
}
@RequestMapping(method = RequestMethod.GET)
public String logout(ModelMap m) {
m.addAttribute("logoutStatus","true");
return "access/login";
}
}
我收到以下错误(似乎只有登录方法被正确映射):
org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'accessController' bean method
public java.lang.String com.robikcodes.samplespring.controller.AccessController.logout(org.springframework.ui.ModelMap)
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'accessController' bean method
public void com.robikcodes.samplespring.controller.AccessController.login(org.springframework.ui.ModelMap) mapped.