2014-02-25 66 views
0

我有這樣的代碼:映射多個URL至相同的控制器,但不同的方法

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

該servlet上下文:

<?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.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd""> 

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

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 
    <mvc: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> 
<beans:bean class="com.goatsoft.helpin.Controllers.LoginController"></beans:bean> 

<context:component-scan base-package="com.goatsoft.helpin" /> 

根上下文(空):

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

<!-- Root Context: defines shared resources visible to all other web components --> 

而我在回到Home.jsp有兩個按鈕,一個是這樣的:

<form action="/usuario/getbyid" method="post"> 
<!--some texts and stuff --> 
<input type="submit" value="Login"> 
</form> 

而且anoher一個是這樣的:

<form action="/usuario/adduser" method="post"> 
<input type="submit" value="adduser"> 
</form> 

這是LoginController中:

@Controller 

public class LoginController{ 

String name; 
String pass; 
//@Autowired 
//UsuarioService usuarioService; 




@RequestMapping("usuario/adduser") 
public String addUsuario(){ 
    System.out.println("url adduser"); 
    return name; 

} 

@RequestMapping("usuario/getbyid") 
public String getById(){ 
    System.out.println("url getbyid"); 
    return name; 
} 

} 

有了這個,我越來越這堆棧跟蹤:

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'loginController' bean method 
public java.lang.String com.goatsoft.helpin.Controllers.LoginController.addUsuario() 
to {[/usuario/adduser],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'com.goatsoft.helpin.Controllers.LoginController#0' bean method 
public java.lang.String com.goatsoft.helpin.Controllers.LoginController.addUsuario() mapped. 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
at javax.servlet.GenericServlet.init(GenericServlet.java:160) 
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) 
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) 
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:744) 
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'loginController' bean method 
public java.lang.String com.goatsoft.helpin.Controllers.LoginController.addUsuario() 
to {[/usuario/adduser],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'com.goatsoft.helpin.Controllers.LoginController#0' bean method 
public java.lang.String com.goatsoft.helpin.Controllers.LoginController.addUsuario() mapped. 
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:183) 
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:147) 
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:109) 
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initApplicationContext(AbstractHandlerMethodMapping.java:89) 
at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119) 
at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72) 
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73) 
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:117) 
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:92) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1448) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
... 32 more 

我試圖從servlet-context.xml中刪除bean,因爲我已經掃描它並獲得兩個實例。這樣,顯示的是home.jsp,但如果我按下任何按鈕,則控制器中沒有任何系統出現(方法未調用)。如果我刪除,我沒有得到任何錯誤,但沒有顯示home.jsp。刪除兩者都不會導致錯誤,但不會顯示home.jsp。

我不知道如何將兩個URL映射到同一個控制器。它不應該那麼困難。任何幫助?謝謝。

回答

1

試試這個。

@Controller 
@RequestMapping("/usuario") 
public class LoginController{ 

String name; 
String pass; 

@RequestMapping("/adduser") 
public String addUsuario(){ 
    System.out.println("url adduser"); 
    return name; 

} 

@RequestMapping("/getbyid") 
public String getById(){ 
    System.out.println("url getbyid"); 
    return name; 
} 

或使用您的配置,只需加上 「/」

@Controller 
public class LoginController{ 

String name; <-- Take care with those instance variables. 
String pass; 

@RequestMapping(value = "/usuario/adduser", method = RequestMethod.POST) 
public String addUsuario(){ 
    System.out.println("url adduser"); 
    return name; 

} 

@RequestMapping(value = "/usuario/getbyid", method = RequestMethod.POST) 
public String getById(){ 
    System.out.println("url getbyid"); 
    return name; 
} 

} 
0

好了,從servlet-context.xml中移除的LoginController,增加了@RequestMapping URL斜槓,並從家裏取出.jsp做到了。

相關問題