Could not open ServletContext resource [/WEB-INF/petstore-servlet.xml]
这是我第一次试验spring第一个页面跳转时候碰到的问题,其实一个拼写错误的问题,
在xml中
<servlet>
<servlet-name>petstore</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/petstore-servlet.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>petstore</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> 一开始servlet打错了,打成了sevlet ,在xml中当然是没有办法识别的。
还有applicationContext.xml文件配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>到这里配置就都是对的了。
然后在试验的时候,还碰到了一个404问题。那么一定是servlet配置的时候出了问题。
我的petstore-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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- VIEW DEFINITIONS -->
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="shop/index.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="catalog/Main"/>
</bean>
</beans>后来改了bean name为:"/shop/index.do" 就对了,总之记录一下这个问题,以后希望不要再在项目一开始的时候就出问题。
还有提一点,之前对
<bean name="shop/index.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="catalog/Main"/>
</bean> 这一段里的property 的name属性不太理解,因为只要改掉了就会不能运行,之前也没好好看什么教程。后来看了一下别的成功例子,想了想,终于明白了。
这个name对应上面引用的class里面声明的对象,viewName这个对象不出我的所料的话,应该就是用于直接访问指定url的jsp页面的时候使用的。
之后如果bean是处理别的类型的请求而不再是页面跳转的时候,应该自己写类来实现,上面引用的class也要改成自己写的类。
本文详细阐述了在使用Spring MVC进行项目开发时遇到的两个关键问题:servlet配置错误导致无法识别资源,以及出现404错误。通过分析XML配置文件中的错误,并给出修正方案,作者分享了如何正确配置servlet和解决404错误的方法,以确保项目的顺利进行。
1025

被折叠的 条评论
为什么被折叠?



