RequestMappings根本不工作

时间:2012-09-28 15:14:41

标签: java spring tomcat spring-mvc

web.xml:

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

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

我已将此附加到web.xml

 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>utf-8</page-encoding>
        </jsp-property-group>
    </jsp-config>

现在applciationContext.xml包含此

<mvc:annotation-driven/>
<context:component-scan base-package="com.myCorectBasePackage"/>

和控制器看起来像这样:

@RequestMapping(value = "/Home", method = RequestMethod.GET)
    public String getHome(Model model) {
        model.addAttribute("data", "myData");
        System.out.println("wtf");
        return "index";
    }

但网址正在恢复到404:

http://localhost:8080/myWar/Home

FAIL!

服务器日志好坏

INFO: Deploying configuration descriptor ws.xml from C:\Users\myProfile\.IntelliJIdea11\system\tomcat\Unnamed_myWar_2\conf\Catalina\localhost
Sep 28, 2012 4:21:18 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 28, 2012 4:21:18 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Sep 28, 2012 4:21:18 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3324 ms
Connected to server

这里的@Vacumn是自动生成的部署描述符

<Context antiJARLocking="true" docBase="E:\jetbrains-workspace\myWar\target\myWar" path="/myWar" />

2 个答案:

答案 0 :(得分:0)

将您的XML配置文件定义为context-param,而不是init-param

<context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

我希望您在Spring配置中添加InternalResourceViewResolver条目:

<bean id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
           <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
           <property name="suffix">
              <value>.jsp</value>
           </property>
        </bean>

答案 1 :(得分:0)

如果你有注释驱动的控制器,你可能会这样做

<context:component-scan base-package="com.package1, com.package2"/>