0
点赞
收藏
分享

微信扫一扫

SpringMVC大坑一枚:ContentNegotiatingViewResolver可能不利于SEO


广大站长都有关注自己网站被搜索引擎收录的习惯,最近用百度、360等搜索引擎,查看了自己网站的一些情况,使用命令“site:fansunion.cn”。

  我发现了一些异常信息,不止一次:

“http://fansunion.cn/service
{"page":{"totalCount":5,"totalPage":1,"pageNo":1,"pageSize":10,"orderBy":null,"order":null,"params":{},"rows":[{"content":" 拜小雷为师,...
fansunion.cn/service 2014-11-13  - 百度快照 - 评价 - 翻译此页”
“http://fansunion.cn/code
{"page":{"totalCount":7,"totalPage":1,"pageNo":1,"pageSize":10,"orderBy":"id","order":"desc","params":{},"rows":[{"content":" SSH框架...
fansunion.cn/code 2014-11-16  - ”“http://fansunion.cn/code
{ page :{ totalCount :8, totalPage :1, pageNo :1, pageSize :20, orderBy : id , order : desc , params :{}, rows :[{ content : , summary : 在线演示:小游戏-会飞的猪-FlappyPig , updat...
fansunion.cn/code 2014-11-09”


 
  很明显,这是JSON格式的数据。
  但是,我是用Freemarker直接渲染的页面,不可能是我主动响应JSON格式的数据。

 于是,很快联想到,项目中有SpringMVC如下配置:

<bean
 class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 <property name="defaultContentType" value="application/json" />
 <property name="mediaTypes">
  <map>
   <entry key="html" value="text/html" />
   <entry key="json" value="application/json" />
   <entry key="xml" value="application/xml" />
  </map>
 </property>
 <property name="defaultViews">
  <list>
   <bean
    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
   </bean>
   <bean id="marshallingView"
    class="org.springframework.web.servlet.view.xml.MarshallingView">
    <property name="marshaller">
     <bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
      <property name="autodetectAnnotations" value="true" />
     </bean>
    </property>
    <property name="contentType" value="application/xml" />
   </bean>
  </list>
 </property>
</bean>




 有了上述配置,一个请求,a.html动态渲染, a.json返回json格式的数据。



 公司项目中这么配置的,我觉得也挺有好处的,JSON格式的请求,只要在请求后面以.json格式就好了,后端不需要再手动写代码了。


比如下面的代码:


private void list(HttpServletResponse response,
  List<Map<String, Object>> list) {
 JSONObject jsonObject = new JSONObject();
 jsonObject.put("list", list);
 super.returnJsonObject(response, jsonObject);
}




总之一句话,公司项目boss采用这种配置,最大的目的就是,很方便地响应.html页面和.json数据,甚至是2者同时支持。


但是,实际情况证实,这种情况很可能 不利于百度-360等搜索引擎的优化。



最后,我决定全面废弃: ContentNegotiatingViewResolver。


如果需要JSON格式的数据,手动使用FastJSON响应。



实践出真知呀~


自己动手写网站,还是很锻炼人的~问题一大堆~ 


举报

相关推荐

0 条评论