Spring 配置時一些問題


在今天看學弟們的一些作業的時候,發現了他們在web.xml中只是配置了

<!--中央控制器  -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
只是配置了這些還有一個springmvc-servlet.xml文件就可以實現spring注入式開發

下面是他們的springmvc-servlet.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">


	<context:component-scan base-package="cn.itcast.springmvc"/>
	
 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

裏面只有一個掃描註解和視圖解析器就可以實現簡單的登錄,然後我試了一下我的代碼更改成這樣竟然不行之後我又仔細的看了一遍,發現,我的掃描註解在spring.xml中視圖解析器在spring-mvc.xml中,然後我的web.xml裏面寫了配置屬性文件和添加spring監聽器下面是我的spring.xml等的源碼

spring.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 引入屬性文件 -->
	<!-- <context:property-placeholder location="classpath:config.properties" /> -->
	<!-- mvc註解驅動 -->
	<mvc:annotation-driven />
	<!-- 一旦有掃描器的定義mvc:annotation-driven就不需要寫了,因爲掃描器已經有了註解驅動的功能 -->
	<!-- spring可以自動去掃描base-pack下面或者子包下面的java文件,如果掃描到有@Component @Controller@Service等這些註解的類,則把這些類註冊爲bean -->
	<!-- <context:component-scan base-package="www.csdn.net"/> -->
	<context:component-scan base-package="com.csdn.www">
		<!-- 排除某個註解 -->
		<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> -->
	</context:component-scan>
   
</beans>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

	<!-- 避免IE執行AJAX時,返回JSON出現下載文件 -->
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>

	<!-- 前綴+ viewName +後綴 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- webroot到某一指定的文件夾的路徑 (前綴)-->
		<property name="prefix" value="/WEB-INF/view/"></property>
		<!-- 後綴 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置springmvc的上傳功能   id必須是multipartResolver -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- maxUploadSize:文件上傳的最大值以byte爲單位 -->
		<property name="maxUploadSize" value="102400000"></property>
	</bean>
</beans>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name></display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param>
	<filter>
		<description>字符集過濾器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<description>字符集編碼</description>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<listener>
		<description>spring監聽器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
		<!-- spring mvc servlet -->
	<servlet>
		<description>spring mvc servlet</description>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>spring mvc 配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>


	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.png</url-pattern>
	</servlet-mapping>
</web-app>

然後就是大家來找茬了唄

我發現他所有的spring的註解掃包和視圖解析器在一個xml文件中,叫springmvc-servlet.xml文件中,然後我把他的springmvc-servlet.xml copy到我的項目中

並且把

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param>

和分發器中的

<init-param>
			<description>spring mvc 配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>

還有監聽器

<listener>
		<description>spring監聽器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

註釋掉了,發現啓動沒有問題,但是一執行跳轉出錯了,後臺說找不到一個叫做springMvc-servlet.xml文件的錯誤,可是我明明都沒有配置這個東西怎麼會出現這個錯誤呢?

然後重新啓動了一下,啓動依舊沒有問題,然後我看了一下錯誤日誌,原來是當有請求經過攔截器的時候攔截器會去加載一個默認的配置文件,可是他的叫springmvc-selvlet.xml文件都行我的爲什麼不行,仔細查看配置文件發現,他的分發器中的servlet-name寫的是springmvc而我的是springMvc,感覺就是他搞得事情,改一下,改成springmvc就可以了,總結出來如果你沒有

<init-param>
			<description>spring mvc 配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>

這個配置,那麼你需要在WEN-INF下面建立一個以servlet-name開頭加上-servlet.xml結尾的一個配置文件,裏面需要添加spring需要的一些屬性配置

<servlet>
		<description>spring mvc servlet</description>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
 

這個配置文件感覺有點繞。。記錄下來,以防止以後再被同樣的問題難住


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章