问题描述
我试图使用Spring,Hibernate和Apache Tiles来运行我的Web应用程序。
看起来代码没有任何错误,但我只是得到一个404页面。
/var/log/tomcat7/catalina.out: p>
DEBUG:org.springframework.web.servlet.DispatcherServlet - 成功配置Servlet'dispatcher'
DEBUG:org.springframework。 web.servlet.DispatcherServlet - 名为'dispatcher'的DispatcherServlet处理[/example/index.html]的GET请求
DEBUG:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 查找处理程序方法for path /index.html
DEBUG:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 未找到[/index.html]
的处理程序方法DEBUG:org.springframework。 web.servlet.handler.SimpleUrlHandlerMapping - 请求[/index.html]的匹配模式为[/ **]
DEBUG:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 请求的URI模板变量[/ index .html]为{}
DEBUG:org.springframework。 web.servlet.handler.SimpleUrlHandlerMapping - 使用处理函数[org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@32645ccb]和1个拦截器
将[/index.html]映射到HandlerExecutionChain DEBUG:org.springframework.web.servlet .DispatcherServlet - [/example/index.html]的Last-Modified值为:-1
DEBUG:org.springframework.web.servlet.DispatcherServlet - 空ModelAndView以名称'dispatcher'返回给DispatcherServlet:假设HandlerAdapter已完成请求处理
DEBUG:org.springframework.web.servlet.DispatcherServlet - 成功完成请求
Servlet:
< servlet>
< servlet-name>调度程序< / 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>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name>调度程序< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>
上下文:
< mvc:annotation-driven />
< tx:annotation-driven />
< bean id =viewResolverclass =org.springframework.web.servlet.view.UrlBasedViewResolver>
< property name =viewClassvalue =org.springframework.web.servlet.view.tiles2.TilesView/>
< / bean>
< bean id =tilesConfigurerclass =org.springframework.web.servlet.view.tiles2.TilesConfigurer>
< property name =definitions>
<值>
/WEB-INF/**/tiles.xml
< /值>
< / property>
< / bean>
< context:component-scan base-package =com.example.controller/>
< resource mapping =/ resources / **location =/ resources //>
< default-servlet-handler />
< context:property-placeholder location =classpath:jdbc.properties/>
< bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =com.example.model/>
< property name =hibernateProperties>
<道具>
< 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>
< /道具>
< / property>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
Maven依赖:
<性状>
< java.version> 1.6< /java.version>
< spring.version> 3.1.1.RELEASE< /spring.version>
< slf4j.version> 1.6.4< /slf4j.version>
< / properties>
<依赖关系>
< dependency>
< groupId> cglib< / groupId>
< artifactId> cglib< / artifactId>
< version> 2.2.2< / version>
< /依赖关系>
<! - Spring - >>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> $ {spring.version}< / version>
<排除项>
<排除>
< groupId> commons-logging< / groupId>
< artifactId> commons-logging< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-tx< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
<! - 记录 - >
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-api< / artifactId>
< version> $ {slf4j.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> jcl-over-slf4j< / artifactId>
< version> $ {slf4j.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-log4j12< / artifactId>
< version> $ {slf4j.version}< / version>
< scope>运行时< / scope>
< /依赖关系>
< dependency>
< groupId> log4j< / groupId>
< artifactId> log4j< / artifactId>
< version> 1.2.17< / version>
< scope>运行时< / scope>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.1.1.Final< / version>
< /依赖关系>
< dependency>
< groupId> postgresql< / groupId>
< artifactId> postgresql< / artifactId>
< version> 9.1-901-1.jdbc4< / version>
< /依赖关系>
<! - Servlet - >
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> servlet-api< / artifactId>
< version> 2.5< / version>
< scope>提供< / scope>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< version> 1.2< / version>
< /依赖关系>
<! - Apache Tiles - >
< dependency>
< groupId> org.apache.tiles< / groupId>
< artifactId> tiles-jsp< / artifactId>
< version> 2.2.2< / version>
<排除项>
<排除>
< groupId> commons-logging< / groupId>
< artifactId> commons-logging-api< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> javax.validation< / groupId>
< artifactId> validation-api< / artifactId>
< version> 1.0.0.GA< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-validator< / artifactId>
< version> 4.1.0.Final< / version>
< /依赖关系>
< /依赖关系>
在添加所有的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){
returnposts;
}
}
I解析它。
有两个上下文:根(应用程序范围)和servlet上下文
我定义了 viewResolver , tilesConfigurer , tx:注解驱动和 mvc:注释驱动
将它移到servlet上下文中解决了这个问题。
strong>已更新
我已移动 viewResolver , tilesConfigurer , tx:注释驱动和 mvc:注释驱动定义来自 applicationContext.xml 到 contextConfigLocation ( dispatcher-servlet.xml )
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.
/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:
<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?
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"; } }
I solved it.
There are two contexts: root (application wide) and servlet contextI defined viewResolver, tilesConfigurer, tx:annotation-driven and mvc:annotation-driven in the root context.
Moving it to the servlet context solved the problem.
UPDATEDI've moved viewResolver, tilesConfigurer, tx:annotation-driven and mvc:annotation-driven definitions from applicationContext.xml to contextConfigLocation (dispatcher-servlet.xml)
这篇关于空ModelAndView返回到DispatcherServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!