如何更改Spring MVC默认控制器名称及其位置

时间:2012-11-30 02:11:16

标签: spring spring-mvc

我是Java和Spring Framework的新手。我使用Spring STS 3.1.0.RELEASE和tomcat 7.我使用以下指示创建Spring MVC项目。文件> Spring模板项目> Spring MVC项目。 Follwoing是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-contxt.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.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">

    <!-- 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="org.dave.foo" />



</beans:beans>

我想要更改控制器的默认名称和位置。为此,我创建了新的包org.dave.bar并将IndexController移动到此包。我还在servlet-context.xml文件中进行了如下所述的更改:

<context:component-scan base-package="org.dave.bar" />

但是在重新启动Web服务器并在此URL上运行代码(http:// localhost:8080 / bar /)后,我收到404错误。有人可以告诉我我做错了什么以及如何纠正它。

提前致谢

1 个答案:

答案 0 :(得分:0)

首先,在web.xml中,您需要定义您希望Spring MVC Dispatcher Servlet处理的URL映射(通过控制器中的请求映射方法)。 根据我在您的问题中的理解,您将需要设置URL映射如下(在web.xml中)

   <servlet>  
     <servlet-name>spring</servlet-name>  
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/*</url-pattern>  
   </servlet-mapping>  

其次, 在org.dave.foo下,您需要放置Spring MVC控制器(使用@Controller和URL映射方法注释的类)。 在您的控制器中,您将需要一个映射到spring dispatcher servlet URL映射的根的方法(在web.xml DispatcherServlet中定义): 因此,对于上面的定义,您需要一个这样的方法:

@RequestMapping(value = "/")  
 public ModelAndView index(HttpServletRequest request,  
         HttpServletResponse response) {  
        ...
  }

请注意,一般来说,最具体的请求映射“获胜”。这意味着如果除了“/”请求映射之外在控制器中有“/ abc”请求映射,那么请求localhost / myapp / abc将“赢”localhost / myapp /

您可以在此处查看更详细的示例: http://www.commonj.com/blogs/2012/11/30/spring-mvc-default-controller-name/

希望它有所帮助。