简体   繁体   English

Spring MVC配置

[英]Spring MVC configuration

I am using eclipse and Jboss for my coding of a simple Spring MVC application. 我将eclipse和Jboss用于简单Spring MVC应用程序的编码。 I have created an enterprise application and I set the context-root as myAppWeb in application.xml. 我创建了一个企业应用程序,并在application.xml中将上下文根设置为myAppWeb。

My web.xml details: 我的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>

My springspringapp-servlet.xml 我的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>

Now when I am entering the URL as 现在,当我输入网址为

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

I am getting and 404 error and the log message as: 我得到和404错误,日志消息为:

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

I changed the config in my springspringapp-servlet.xml as 我将springspringapp-servlet.xml中的配置更改为

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

Still it is not working. 仍然无法正常工作。

Could anyone help me out regarding this? 有人可以帮我这个忙吗?

thanks! 谢谢!

For my projects, the '*-servlet.xml' file is empty: 对于我的项目,“ *-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>

The 'web.xml' file has a context listener to read the configuration when spring application is booting: spring应用程序启动时,“ web.xml”文件具有上下文侦听器以读取配置:

<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>

Then, the file pointed by the context listener ('/WEB-INF/SpringAppServlet.xm')contains your configuration: 然后,上下文侦听器指向的文件('/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>

You need to specify Context loader listener in your web.xml by passing bean configuration file as context param as below 您需要通过将bean配置文件作为上下文参数传递,在web.xml中指定Context loader侦听器,如下所示

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM