spring Null ModelAndView 返回给 DispatcherServlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11214143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:12:09  来源:igfitidea点击:

Null ModelAndView returned to DispatcherServlet

springhibernatetiles

提问by dtrunk

I'm trying to run my web application using Spring, Hibernate and Apache Tiles. It seems the code has no errors, but I'm just getting a 404 page.

我正在尝试使用 Spring、Hibernate 和 Apache Tiles 运行我的 Web 应用程序。代码似乎没有错误,但我只是得到一个 404 页面。

/var/log/tomcat7/catalina.out:

/var/log/tomcat7/catalina.out:

DEBUG: org.springframework.web.servlet.DispatcherServlet - Servlet 'dispatcher' configured successfully
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/example/index.html]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /index.html
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/index.html]
DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/index.html] are [/**]
DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/index.html] are {}
DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/index.html] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@32645ccb] and 1 interceptor
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/example/index.html] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request

Servlet:

小服务程序:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/META-INF/spring/dispatcher-servlet.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>

context:

语境:

<mvc:annotation-driven />
<tx:annotation-driven />

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <value>
            /WEB-INF/**/tiles.xml
        </value>
    </property>
</bean>

<context:component-scan base-package="com.example.controller" />
<resources mapping="/resources/**" location="/resources/" />
<default-servlet-handler />
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.example.model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

Maven dependencies:

Maven 依赖项:

<properties>
    <java.version>1.6</java.version>
    <spring.version>3.1.1.RELEASE</spring.version>
    <slf4j.version>1.6.4</slf4j.version>
</properties>

<dependencies>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>runtime</scope>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.1.Final</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Apache Tiles -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>2.2.2</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging-api</artifactId>
             </exclusion>
        </exclusions>
    </dependency>

    <!-- JSR 303 with Hibernate Validator -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.1.0.Final</version>
    </dependency>

</dependencies>

Before adding all the hibernate stuff I tried the web app and it worked just great. But what's a webapp without a database?

在添加所有休眠的东西之前,我尝试了网络应用程序,它工作得很好。但是没有数据库的 webapp 是什么?

I already spent hours on this and just can't find the problem...

我已经花了几个小时在这上面,只是找不到问题...

UPDATE

更新

Controller:

控制器:

package com.example.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class BlogController {
    @RequestMapping("/index.html")
    public String posts(Map<String, Object> map) {
        return "posts";
    }
}

采纳答案by dtrunk

I solved it.

我解决了。

There are two contexts: root (application wide) and servlet context I defined viewResolver, tilesConfigurer, tx:annotation-drivenand mvc:annotation-drivenin the root context.

有两个上下文:根(应用宽)和I定义的servlet上下文viewResolvertilesConfigurertx:annotation-drivenmvc:annotation-driven在根上下文。

Moving it to the servlet context solved the problem.

将它移动到 servlet 上下文解决了这个问题。

UPDATEDI've moved viewResolver, tilesConfigurer, tx:annotation-drivenand mvc:annotation-drivendefinitions from applicationContext.xmlto contextConfigLocation(dispatcher-servlet.xml)

已更新我感动viewResolvertilesConfigurertx:annotation-drivenmvc:annotation-driven从定义applicationContext.xmlcontextConfigLocationdispatcher-servlet.xml

回答by Narendra Kumar Achari

Try sending ModelAndView instead of String Or

尝试发送 ModelAndView 而不是 String Or

You can send map object as parameter to ModelandView method(which is already overloaded)

您可以将地图对象作为参数发送到 ModelandView 方法(已重载)

@Controller 
@RequestMapping("/") 
public class BlogController {
    @RequestMapping("/index.html")
    public ModelAndView posts(Map<String, Object> map) {
      return new ModelAndView("/index.jsp", "status", status);
    } 
}