【问题标题】:Spring MVC configurationSpring MVC 配置
【发布时间】:2014-06-08 18:33:38
【问题描述】:

我正在使用 eclipse 和 Jboss 编写一个简单的 Spring MVC 应用程序。 我创建了一个企业应用程序,并在 application.xml 中将上下文根设置为 myAppWeb。

我的 web.xml 详细信息:

<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

我的 springspringapp-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<!-- the application context definition for the springapp DispatcherServlet -->

<bean name="/a.htm" class="com.init.servlet.test.HelloController" />

</beans>

现在当我输入 URL 为

"http://localhost:8080/myAppWeb/a.htm" 

我收到 404 错误,日志消息为:

" servlet.PageNotFound OO                 noHandlerFound() OO No mapping found for HTTP request with URI [/myAppWeb/a.htm] in DispatcherServlet with name 'springapp'".

我将 springspringapp-servlet.xml 中的配置更改为

<bean name="/myAppWeb/a.htm" class="com.init.servlet.test.HelloController" />

仍然无法正常工作。

有人可以帮我解决这个问题吗?

谢谢!

【问题讨论】:

  • 您认为&lt;bean&gt;name 属性有什么作用,为什么?
  • 你需要发布HelloController的代码

标签: java eclipse spring jakarta-ee spring-mvc


【解决方案1】:

您需要在 web.xml 中指定上下文加载器侦听器,方法是将 bean 配置文件作为上下文参数传递,如下所示

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/SpringAppServlet.xml</param-value> </context-param>

<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

【讨论】:

  • 你认为这会改变什么。
【解决方案2】:

对于我的项目,'*-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

</beans>

'web.xml' 文件有一个上下文监听器,用于在 spring 应用程序启动时读取配置:

<context-param>
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/SpringAppServlet.xml</param-value>
</context-param>

<listener>
    <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
</listener>

然后,上下文监听器指向的文件('/WEB-INF/SpringAppServlet.xm')包含你的配置:

<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd">

<!-- the application context definition for the springapp DispatcherServlet -->

<bean name="/hello.htm" class="com.init.servlet.test.HelloController" />

</beans>

【讨论】:

  • 如果没有在 DispatcherServlet 中明确指定,上下文实现将从调度程序 servlet 的命名空间构建一个默认位置,即在这种情况下为 /WEB-INF/springapp-servlet.xml。为什么你认为你需要一个听众来解决这个问题?
  • 根据我对错误的理解是:在查找 Controller 类时,它也将上下文根用于搜索键。\n其次,如果我使用 ContextLoaderlistener,那么我该如何进行 URL 映射?