当前位置: 首页 > 面试题库 >

Null ModelAndView返回给DispatcherServlet

谢俊英
2023-03-14
问题内容

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

/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>

上下文:

<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依赖项:

<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>

在添加所有hibernate内容之前,我尝试了该Web应用程序,它的运行效果非常好。但是什么是没有数据库的Web应用程序?

我已经花了几个小时才发现问题…

更新

控制器:

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";
    }
}

问题答案:

我解决了

有两个上下文:root(应用程序范围)和servlet上下文(我定义viewResolvertilesConfigurertx:annotation- driven以及mvc:annotation-drivenroot上下文。

将其移至Servlet上下文即可解决此问题。

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



 类似资料:
  • 问题内容: 我有3个文件main.php,action.js和ajax.php,我在javascript文件中通过ajax调用成功地将某些div单击的内容从main.php更改为ajax.php的内容。看起来像这样: 现在,我需要从action.js中的ajax.php中的函数返回值,因为我想迭代直到该值(请参见上面的代码)。如何将此值从ajax.php传递到action.js。我很困惑我需要什么

  • 我在用户和任务之间有一对多的关系(一个用户有很多任务)。 我在数据库中插入我的数据,其中每个任务都与用户ID有一个外键关联。在JPA中有没有可能本质上说: 这是我的简单回购

  • 问题内容: 例如我有一个功能: 我怎样才能返回AJAX后得到的? 问题答案: 因为请求是异步的,所以您无法返回ajax请求的结果(而同步ajax请求是一个 糟糕的 主意)。 最好的选择是将自己的回调传递给f1 然后,您将像这样致电:

  • 我已使用 startActivityForResult 从父活动启动了子活动。在子活动中执行所需功能后,我正在使用setResult设置结果。但是我没有从孩子活动中获得父母活动的结果。 这是我的代码。 下面是我如何从父活动中调用子活动。 这是我如何设置子活动的结果。 这是我的onActivityResult 这里是当我检查resultCode==活动时。RESULT_OK给出false。我还检查了

  • 然而,当我只使用console.log(body)时,它会给出正确的结果 …………………………。 ………………………… ……