使用 Spring MVC 未发现映射错误
合:
我希望你能帮助我。当涉及到在已经配置和设置的应用程序中工作时,我非常擅长,但另一方面,我显然不太擅长设置新应用程序 - 特别是当涉及到 Spring MVC 时。我正在尝试设置一个非常简单的应用程序,以便稍后可以在它的基础上进行构建。但当我运行它时,我收到 404 错误。一切都部署正常并编译正常,但日志显示错误:警告:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [/SpringTemplate/hello.htm] 的 HTTP 请求的映射。有人可以向我解释一下我做错了什么吗?我已经插入了文件 web.xml、dispatcher-servlet.xml、一些记录的条目和我的 HelloController.java 类。
web.xmldispatcher
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<ctx:annotation-config />
<ctx:component-scan base-package="greenthorn.com" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
成功部署后记录条目
INFO: FrameworkServlet 'dispatcher': initialization started
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Nov 10 20:51:43 EST 2010]; parent: Root WebApplicationContext
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@17dca7b8: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,helloController,org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,urlMapping,viewResolver,indexController]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@2674b0ba
INFO: Mapped URL path [/hello] onto handler [greenthorn.com.controller.HelloController@3bbd0836]
INFO: Mapped URL path [/hello/*] onto handler [greenthorn.com.controller.HelloController@3bbd0836]
INFO: Mapped URL path [/index.htm] onto handler [org.springframework.web.servlet.mvc.ParameterizableViewController@64cbad03]
INFO: FrameworkServlet 'dispatcher': initialization completed in 454 ms
INFO: Loading application SpringTemplate at /SpringTemplate
INFO: SpringTemplate was successfully deployed in 4,749 milliseconds.
HelloController.java
package greenthorn.com.controller;
import login.Passwordproperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import greenthorn.com.service.HelloService;
import greenthorn.com.service.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
private HelloService helloService;
public HelloController() {}
/**
* @param helloService the helloService to set
*/
@Autowired
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
@RequestMapping("/hello.htm")
public ModelAndView onSubmit(@ModelAttribute("command") Name command) throws Exception {
ModelAndView mv = new ModelAndView("helloView");
mv.addObject("helloMessage", helloService.sayHello(command.getValue()));
mv.addObject("greeting", "Thanks for dropping by...");
return mv;
}
}
All:
I hope that you can help me. I am pretty good when it comes to working in an application that has already been configured and setup, but the flip side is that I am apparently not very good in setting up a new application - especially when it comes to Spring MVC. I am trying to set up a very simple application so that I can build upon it later. I get a 404 error though when I run it. Everything deploys OK and compiles OK but the log shows an error of: WARNING: No mapping found for HTTP request with URI [/SpringTemplate/hello.htm] in DispatcherServlet with name 'dispatcher'. Can someone please explain to me what I have done wrong? I have inserted the files web.xml, dispatcher-servlet.xml, some logged entries, and my HelloController.java class.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<ctx:annotation-config />
<ctx:component-scan base-package="greenthorn.com" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
Logging entries upon successful deployment
INFO: FrameworkServlet 'dispatcher': initialization started
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Nov 10 20:51:43 EST 2010]; parent: Root WebApplicationContext
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@17dca7b8: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,helloController,org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,urlMapping,viewResolver,indexController]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@2674b0ba
INFO: Mapped URL path [/hello] onto handler [greenthorn.com.controller.HelloController@3bbd0836]
INFO: Mapped URL path [/hello/*] onto handler [greenthorn.com.controller.HelloController@3bbd0836]
INFO: Mapped URL path [/index.htm] onto handler [org.springframework.web.servlet.mvc.ParameterizableViewController@64cbad03]
INFO: FrameworkServlet 'dispatcher': initialization completed in 454 ms
INFO: Loading application SpringTemplate at /SpringTemplate
INFO: SpringTemplate was successfully deployed in 4,749 milliseconds.
HelloController.java
package greenthorn.com.controller;
import login.Passwordproperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import greenthorn.com.service.HelloService;
import greenthorn.com.service.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
private HelloService helloService;
public HelloController() {}
/**
* @param helloService the helloService to set
*/
@Autowired
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
@RequestMapping("/hello.htm")
public ModelAndView onSubmit(@ModelAttribute("command") Name command) throws Exception {
ModelAndView mv = new ModelAndView("helloView");
mv.addObject("helloMessage", helloService.sayHello(command.getValue()));
mv.addObject("greeting", "Thanks for dropping by...");
return mv;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在当前配置中,由于
ControllerClassNameHandlerMapping
,HelloController.onSubmit()
映射到/hello/hello.htm
。如果想映射到
/hello.htm
,需要将ControllerClassNameHandlerMapping
替换为DefaultAnnotationHandlerMapping
(通常默认使用这个映射,但是在您的情况下,不应用默认值,因为您手动声明处理程序映射)。In your current configuration
HelloController.onSubmit()
is mapped to/hello/hello.htm
due toControllerClassNameHandlerMapping
.If you want to map it to
/hello.htm
, you need to replaceControllerClassNameHandlerMapping
withDefaultAnnotationHandlerMapping
(usually this mapping is used by default, but in your case defaults are not applied because you declare handler mappings manually).失败的原因可能是参数“@ModelAttribute(“command”) Name command”。
这种类型的参数通常用于post方法。
The cause of the failure may be the parameter, "@ModelAttribute("command") Name command".
this type of parameter is usually used for post method.