我开始关注本教程http://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html。
我创建了虚拟HelloController并在webapp-servlet.xml中定义了bean:
<bean name="/hello.htm" class="com.planner.HelloController"></bean>
但是当我尝试访问时 /planner/hello.htm 我明白了:
HTTP Status 500 - No adapter for handler [com.planner.HelloController@187cd18]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
我在No adapter for handler exception找到了类似的问题。他们尝试将注释驱动的映射与BeanNameUrlHandlerMapping一起使用,并获得了类似的异常。解决方案是将SimpleControllerHandlerAdapter bean添加到spring上下文中,但在我的情况下它没有帮助(不应该是必要的,因为BeanNameUrlHandlerMapping应该是默认的映射,对吧?)。我还添加了BeanNameUrlHandlerMapping bean以防万一,但它仍然是相同的。
以下是我的配置:
规划器-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<bean name="/hello.htm" class="com.planner.HelloController"></bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
</beans>
的web.xml
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>planner</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>planner</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
HelloController.java:
package com.planner;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
public class HelloController {
protected final Logger logger = LoggerFactory.getLogger(getClass());
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
logger.info("Returning hello view");
//default view resolver will just forward to this url
return new ModelAndView("hello.jsp");
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.planner</groupId>
<artifactId>Planner</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<javax.annotation.version>1.0</javax.annotation.version>
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- <spring-framework.version>3.0.0.RELEASE</spring-framework.version> -->
<hibernate.version>4.2.1.Final</hibernate.version>
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<junit.version>4.10</junit.version>
<javax.servlet.version>2.5</javax.servlet.version>
</properties>
<dependencies>
<!-- JSR-250 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>${javax.annotation.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
请帮助:)我显然缺少一些东西:)