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