0

我是 Java 和 Spring 框架的新手。我正在使用 Spring STS 3.1.0.RELEASE 和 tomcat 7。我使用以下说明创建 Spring MVC 项目。文件 > Spring 模板项目 > Spring MVC 项目。以下是 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 错误。有人可以指导我我做错了什么以及如何纠正它。

提前致谢

4

1 回答 1

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/

希望它有所帮助。

于 2012-11-30T13:07:55.633 回答