1. 程式人生 > >SSM框架專案中的bug彙總

SSM框架專案中的bug彙總

這個是實習過程中,主要用來了解和學習公司框架的一個測評系統,主要是採用了SSM框架的maven的JAVA Web專案,是比較常見的資訊管理系統。在做系統時總是會遇到糾結很久但瞭解後卻很容易解決的bug,在此寫下這篇bug彙總,希望自己和剛剛學習的童鞋們節約時間成本。如果有些錯誤的地方,請各位熱情指出~

1、Ajax請求在後臺已執行,但是返回前臺404

原因:可能是後臺忽略了註解@ResponseBody,這個註解的作用是將函式返回值作為請求返回值。沒有這個註解的話,請求的響應應該是一個頁面,不需要頁面的話應該加上這個註解。(ps:返回ModelAndView物件時,不能加這個註解,因為,它返回的是包含了整個頁面的資訊)
解決:在controller方法中新增@ResponseBody

2、後臺接收不到前臺上傳的檔案,

原因:缺少檔案上傳相關包,或者springmvc中未配置關於multipartResolver。
解決:

<!-- 在springmvc配置檔案中新增-->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>  

<!-- 在pom.xml中新增依賴 -->  
     <dependency>  
        <groupId
>
commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId
>
<version>2.4</version> </dependency>

3、使用fileinput.js獲取其他外掛是奇怪的報錯(e錯誤,什麼function沒有啥的)

原因:首先確保呼叫正確路徑的情況下,有可能是將jquery.js放在了後面。
解決:正確順序應該是jquery在前。

4、訪問html,頁面報404錯誤,“the requested resource is not available”

原因:這是因為dispatcherServlet會對靜態資源進行攔截,導致了靜態資源無法訪問。
解決:我一般是在springmvc配置檔案中加入:
(ps:想了解其他方法的可以參考文章http://blog.csdn.net/wdehxiang/article/details/77619621)

5、Parameter ‘email’ not found. Available parameters are [0, 1, param1, param2]

這裡我們需要簡單瞭解一下mabatis的引數傳值機制。Mybatis現在可以使用的parameterType有基本資料型別和Java複雜型別。
基本資料型別:包含int,String,Date等。基本資料型別作為傳參,只能傳入一個。通過#{引數名}獲取。
複雜資料型別:包含Java實體類、Map。通過#{屬性名}或#{map的keyName}獲取。
原因:傳入多個值時,mybatis會自動將這些值轉成類似map形式的值,以0,1,2…的序列為keyName。
解決:

方案一、在Dao層mapper介面的方法中,為每個引數新增@Param註解

public User login(@Param(value="email") String email, @Param(value="password") String password);
<select id="login" resultType="User"  parameterType="String">
      select * from user where email=#{email} and password=#{password}
</select>

方案二:將多個值存入Map中

public User login(HashMap<String,String) map);

mapper.xml中就可以通過#{keyName}啦。

<select id="login" resultType="User"  parameterType="Map">
      select * from user where email=#{email} and password=#{password}
</select>

方案三:以傳遞引數的順序通過#{0},#{1}獲取

public User login(String email, String password);
<select id="login" resultType="User"  parameterType="String">
         select * from user where email=#{0} and password=#{1}
</select>