4

我正在学习春天,但遇到了一个问题。我尝试了很多,也google'd分配。我知道很多人都问过这个问题,他们解决了问题,我也对他们进行了尝试,但我仍然遇到同样的错误。因此,我在这里发布我的文件,请帮助我。我太努力了,但没有输出。

我的 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">
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>spring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

我的 spring-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:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframew ork.org/schema/context">

   <mvc:resources location="/images/" mapping="/images/**"/>
   <mvc:resources location="/jsp/js/" mapping="/jsp/js/**"/>
   <context:component-scan base-package="org.vinay.spring_mvc.mycontroller"></context:component-scan>
   <context:annotation-config/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/employee">myController</prop>
    </props>
    </property>
     </bean>
    <bean id="myController" class="org.vinay.spring_mvc.mycontroller.MyController"></bean>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    </beans>

我的控制器看起来像:

package org.vinay.spring_mvc.mycontroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;



@Controller
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public class MyController {


public ModelAndView helloWorld()
{
    System.out.println("Hello World");
    return new ModelAndView("/hello");
    //return new ModelAndView("helloWorld.jsp");
}
}

Iam 发送请求的 JSP 是 web.xml 的欢迎文件。即 index.jsp 。看起来像:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()path+"/";
%>
  <%System.err.print("Path : "+request.getContextPath()); %>

 <html>
<head>
 <title>Solution Center</title>
    <h5>Welcome To Solution Center</h5>
</head>
<body>
<a href="employee">hello</a>
</body>
</html>

谁能帮助我为什么我收到以下错误:

javax.servlet.ServletException: No adapter for handler   [org.vinay.spring_mvc.mycontroller.MyController@18c8aea]: The DispatcherServlet configuration needs  to include a HandlerAdapter that supports this handler
    org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter    (DispatcherServlet.java:1128)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:903)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

我也尝试使用基于注释的处理程序适配器,但得到了同样的错误。对此的任何帮助都会有所帮助。

我只是发现我得到这个是因为包含了 mvc:resources 标签。如果我评论他们我的项目工作正常。对他们的任何帮助都会非常有帮助。我已经尝试了很多其他方法,请为我​​提供分步解决方案。我无法在过去 4 小时内解决此问题。

4

1 回答 1

11

从您的 spring-servlet.xml 中删除以下内容

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/employee">myController</prop>
        </props>
    </property>
 </bean>
 <bean id="myController" class="org.vinay.spring_mvc.mycontroller.MyController"></bean>
 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

添加<mvc:annotation-driven />它将自动配置您的应用程序上下文以与带注释的控制器一起使用。

有关更多信息,请参阅http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable

于 2013-07-12T20:56:39.197 回答