【问题标题】:spring dispatcher-servlet.xml not workingspring dispatcher-servlet.xml 不工作
【发布时间】:2013-01-16 18:07:26
【问题描述】:

如果我有线条

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

在我的 dispatcher-servlet.xml 中,我会从我的 tomcat 中得到这个错误消息:

WARN "http-bio-8080"-exec-1 servlet.PageNotFound:1108 - No mapping found for HTTP request with URI [/inso/home] in DispatcherServlet with name 'dispatcher'

但是,如果我的 web-application-config 中有这两行,则它可以正常工作。 此外,InternalViewResolver 在 dispatcher-servlet-xml 中不起作用,但在 web-application-config 中起作用。 为什么会这样?

我得到了以下 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">

    <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/web-application-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

这是我的 dispatcher-servlet.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/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
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-lazy-init="true">
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

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

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

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

这是我的 web-application-config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

  <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgrsql://localhost:8080/open"/>
    <property name="username" value="po"/>
    <property name="password" value="ha"/>
  </bean>

  <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
        </props>
    </property>
  </bean> 
</beans>

【问题讨论】:

  • 根据错误信息,似乎找不到任何到inso/home 的映射。你有一个带有@Controller 注释的控制器和一个带有inso/home 注释的@RequestMapping 控制器吗?

标签: java spring spring-mvc


【解决方案1】:

也许你真的想在你的 web.xml 中做这样的事情:

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

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<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/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

【讨论】:

  • 嗯...你可能是对的! ;-) 但是……真的有必要两者兼得吗?一个 web-application-config.xml 和一个 dispatcher-servlet.xml ?
  • 仅当您想要两者中的信息时! ;) 您可以随时组合它们,但同时拥有应用程序范围和调度程序特定的上下文配置会很有帮助...
  • 为了澄清这个答案:OP 创建了dispatcher-servlet.xml 但从未引用它。相反,她/他引用了 web-application-config.xml,它没有类路径扫描。
【解决方案2】:

好吧,我认为您尚未在包中定义任何控制器 servlet,因此它无法为任何 URL 找到任何类型的映射。

@Controller
public class HelloController{
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
     public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
     }
}

您也可以参考这个Tutorial

【讨论】:

    【解决方案3】:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
    
           <mvc:annotation-driven></mvc:annotation-driven>
    
        <context:component-scan base-package="com.neel" />
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    
    <mvc:resources location="/WEB-INF/resources/" mapping="/resources/**"/>
    </beans>
    

    【讨论】:

    • 请注意,不鼓励使用“仅代码答案” - 您最好添加一些解释!
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2014-03-23
    相关资源
    最近更新 更多