1. 程式人生 > >springMVC 不掃描 controller 中的方法

springMVC 不掃描 controller 中的方法

最近把之前的一個Maven專案在一個新的電腦環境上匯入Eclipse,啟動時卻發現不掃描 controller 中的方法

下面是正確的 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: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-4.0.xsd">
	<mvc:annotation-driven />
<mvc:resources location="/image/" mapping="/image/**" /> <mvc:resources location="/Content/" mapping="/Content/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 --> <context:component-scan base-package="com.aven.weixiao.controller" /> <!--避免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> <!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 --> </list> </property> </bean> <!-- 定義跳轉的檔案的前後綴 ,檢視模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這裡的配置我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 --> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置檔案上傳,如果沒有使用檔案上傳可以不用配置,當然如果不配,那麼配置檔案中也不必引入上傳元件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 預設編碼 --> <property name="defaultEncoding" value="utf-8" /> <!-- 檔案大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 記憶體中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
那我遇到這個問題的原因是什麼呢?

是因為新配置的環境,缺少很多 jar 包,所以專案匯入Eclipse之後,  這個檔案就報 “<mvc:annotation-driven />” 這一句有錯了,

有錯,我也沒多想就先把它給刪除了。

好吧,問題就這樣產生了。

小結

在匯入一個專案之後,可能會提示有很多錯誤,但針對一些配置檔案,解決的方式不應該是刪除或修改檔案中的內容,

而應該先解決依賴等問題,不然像我這種之前沒有問題的專案,就因為換了環境就產生怪問題。

======================文件資訊===========================

署名(BY) :testcs_dn(微wx笑)

文章出處:[無知人生,記錄點滴](http://blog.csdn.net/testcs_dn)