1. Spring MVC 雙請求問題
viewresolver一定要放在servlet-dispatcher.xml里,否則會導致在請求成功后以后渲染頁面,然后又發一次請求的狀況,最后導致頁面無法顯示.例如以下Controller方法
@RequestMapping("loginPage") public String loginPage() { return "loginView"; }
會造成如下log輸出
00:31:30.836 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'web' processing GET request for [/loginPage]
00:31:30.836 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /loginPage
00:31:30.836 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.qunar.scoresystem.controller.LoginController.loginPage()]
00:31:30.836 [http-bio-8080-exec-8] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'loginController'
00:31:30.836 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/loginPage] is: -1
00:31:30.836 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'loginView'; URL [loginView]] in DispatcherServlet with name 'web'
00:31:30.837 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [loginView] in InternalResourceView 'loginView'
00:31:30.837 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'web' processing GET request for [/loginView]
00:31:30.837 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /loginView
00:31:30.837 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/loginView]
00:31:30.837 [http-bio-8080-exec-8] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/loginView] in DispatcherServlet with name 'web'
00:31:30.837 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
00:31:30.837 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
最后發送了兩次http請求
2. Idea Web目錄建Module加載問題
在idea中必須要將web目錄設為"web"目錄, 就是在配置中要將其加入到Module中,加入后在web目錄圖標中會有一個小地球,否則里面的資源會找不到,而且不會給你任何提示
3. Spring從3.2版本開始默認使用URL后綴來判斷reponse類型, 例如對於@Responsebody來說,/test/list.json 則會返回content-type為json的數據, 而/test/list.xml則會返回xml數據.這點對於寫rest api可能會有問題, 因為你期待返回json數據(accept頭為application/json), 而url后綴叫別的(例如.htm),那么程序就會自動將返回類型修改為application/html, 導致返回406(返回的數據類型與期待的accept類型對不上)
我們可以通過關閉自動根據url后綴判斷內容類型來修正這一點.
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> <property name="favorParameter" value="false" /> </bean>
要注意的是此處 需要制定xsi:schemaLocation為Spring3.2的doc,在XML文件首指定:
xsi:schemaLocation="http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
詳情請參見Spring doc
這樣可以解決返回406的問題
另外說一句, 某項目的URL全都以htm結尾, 這是導致這個問題的原因,而且這本身就是不正確的命名方法, 以htm結尾會導致所有頁面內容被瀏覽器緩存, 這樣html頁面修改后瀏覽器也只能強制刷新緩存才能顯示最新的頁面. 總之, 返回json的請求就不應該叫.htm, 而應該叫 .json. 或者說沒有后綴
4. Jedis 2.2 與 Spring-data-jedis 1.1.0-RELEASE 不兼容問題, 詳見
redis release a new version 2.2.0 and is not compatible with spring data redis. org.springframework.data.redis.connection.jedis.JedisConnection referred redis.clients.jedis.BinaryTransaction and this class has been removed.
5. 要注意自動spring配置文件中schema的聲明, idea補全經常出錯, 例如把mvc補全成cache