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

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-content-negotiation

这样可以解决返回406的问题

另外说一句, 某项目的URL全都以htm结尾, 这是导致这个问题的原因,而且这本身就是不正确的命名方法, 以htm结尾会导致所有页面内容被浏览器缓存, 这样html页面修改后浏览器也只能强制刷新缓存才能显示最新的页面. 总之, 返回json的请求就不应该叫.htm, 而应该叫 .json. 或者说没有后缀

 

4. Jedis 2.2 与 Spring-data-jedis 1.1.0-RELEASE 不兼容问题, 详见

https://jira.springsource.org/browse/DATAREDIS-237?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

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

 

posted on 2013-08-16 18:11  ZimZz  阅读(5197)  评论(0编辑  收藏  举报