不同网址的控制器相同

时间:2014-02-25 00:24:53

标签: spring spring-mvc

我使用Spring开发应用程序。我想将一些不同的URL映射到同一个控制器,一个UserController,我有一个添加用户的方法,另一个方法是从数据库中检索用户。所以,我做了一个看起来像这样的控制器:

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

}

我在root-context.xml中以这种方式映射了url:

<bean name="logincontroller" class="com.desarrollo.helpin.Controllers.LoginController"/>
<!--  <bean name="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/newuserform">newuserformcontroller</prop>
            <prop key="/logincontroller">logincontroller</prop>
            <prop key="/quees">queescontroller</prop>
             <prop key="/adduser">logincontroller</prop> 
            <prop key="/getbyid">logincontroller</prop>
        </props>

    </property>
    </bean>-->

我在home.jsp中有两个按钮,一个是url“/ usuario / getbyid”,另一个是url“/ usuario / adduser”。

这样做,控制器的两个方法中的system.out.println都没有显示出来。我怎么能实现这个目标呢?

谢谢。

编辑:

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>

如果我没有错,DispatcherServlet会加载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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    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
    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>

<context:component-scan base-package="com.development.testing" />

1 个答案:

答案 0 :(得分:2)

您使用的@Controller@RequestMapping无效SimpleUrlHandlerMapping。在@RequestMapping中指定URL并再次在配置中定义它们也不是没有意义。

而不是SimpleUrlHandlerMapping<mvc:annotation-driven />添加到您的上下文中。请注意,这必须在DispatcherServlet加载的上下文中完成,而不是ContextLoaderListener加载的上下文。 DispatcherServlet正在使用它自己的应用程序上下文(而不是ContextLoaderListener中的父级来配置它自己)。