Spring配置web.xml文件时遇到的问题

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也要改成自己写的类。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值