0

我是春天的新手。我正在尝试将 Spring 集成到 Web 应用程序的一部分中。它必须与以下网址一起使用:

http://localhost:9080/myfolder/myspring

我的web.xml包括:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>    
    /WEB-INF/config/myspring-context.xml
  </param-value>
</context-param>


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

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/springviews</url-pattern>
    </servlet-mapping>

应用上下文.xml

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

        <!-- registers all of Spring's standard post-processors for annotation-based configuration -->
        <context:annotation-config />

        <tx:annotation-driven/>
        <tx:jta-transaction-manager/>

       <bean id="properties" class="springcop.pojo.TestObject">

        </bean>

    </beans>

我的myspring-context.xml是:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="springcop"/>

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven/>

 <!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/springviews/" />
    <property name="suffix" value=".jsp" />
</bean>



</beans>

这是MyController

package springcop;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {


    public MyController () {
         System.out.println("--->GestioneController");

    }


     @RequestMapping(value = "/test", method = RequestMethod.GET)
      public String test() {
         System.out.println("--->test");
         return "test";
     }

}

up运行没有错误。正如我在我的日志中打印的那样,Spring 似乎可以工作“--->GestioneController,它位于我的 COntroller 的构造函数中。

无论如何,当我在浏览器中打开

http://localhost:9080/myfolder/myspring/test

在 MyController 中执行测试方法我得到 404 错误。

我应该怎么做才能让它工作?谢谢。

4

1 回答 1

0

假设您的 webapp 的名称myfolder如问题中所列,则 url 将是...

http://localhost:9080/myfolder/springviews/test

问题是您将调度程序 servlet 映射到 springviews。您需要在 web.xml 中按如下方式映射调度程序 servlet...

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

编辑: 此外,您已将弹簧配置放在上下文参数中,但您没有添加侦听器来获取它。将以下内容添加到您的 web.xml....

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
于 2013-04-04T15:06:14.667 回答