我有以下错误。我不明白为什么。
[INFO] javax.servlet.UnavailableException:org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'的bean时出错:bean的初始化失败;嵌套异常是java.lang.IllegalStateException:无法将处理程序[mapController]映射到URL路径[/map.jsp]:已经映射了处理程序[com.application.myGoogleAppEngine.controller.MapController@978068]。
你有解决方案吗?
的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">
<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>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
弹簧servlet.xml中
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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">
<context:component-scan base-package="com.application.myGoogleAppEngine.controller" />
<bean id="helloBean" class="com.application.myGoogleAppEngine.HelloWorld">
<property name="name" value="Mkyong" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="fr" />
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
<property value="classpath:MessageBundle" name="basename"/>
<property value="UTF-8" name="defaultEncoding"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property value="org.springframework.web.servlet.view.JstlView" name="viewClass"/>
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
IndexController.java
package com.application.myGoogleAppEngine.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/index.jsp")
public class IndexController {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView initForm(HttpServletResponse res){
try{
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
catch(IOException ex){}
return null;
//return new ModelAndView("address");
}
}
MapController.java:
package com.application.myGoogleAppEngine.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
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("/map.jsp")
public class MapController {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView initForm(HttpServletResponse res){
try{
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
catch(IOException ex){}
return null;
//return new ModelAndView("address");
}
}
答案 0 :(得分:0)
尝试进行以下更改
在XML中,添加注释驱动元素:
<context:component-scan base-package="com.application.myGoogleAppEngine.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
</bean>
在web.xml中,更改网址格式
<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>
所以,你可以像这样使用你的控制器:
@Controller
@RequestMapping("/map")
public class MapController {
...
}