配置仍然不变
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true"> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter"> <property name="enableJsonConfigSupport" value="true"/> <!-- 打开map转换,嵌套转换 --> <property name="convertAllMapValues" value="true"/> </bean> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter" ref="jsonWriter"/> </bean> </beans>
代码含义解析
//日期转换问题 @InitBinder public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); binder.registerCustomEditor(Date.class,new CustomDateEditor(format, true)); JsonWriterConfiguratorTemplateRegistry registry = JsonWriterConfiguratorTemplateRegistry.load(request); registry.registerConfiguratorTemplate(new SojoJsonWriterConfiguratorTemplate() { @Override public SojoConfig getJsonConfig() { SojoConfig config = new SojoConfig(); //String[] excludes1 = {"allCategories","role","password"}; //String[] excludes2 = {"role","password"}; config.setExcludedProperties(excludes1); return config; } }); }
首先,这里我对bean中的日期进行了格式化,其次要需要过滤一些字段!
注意:this.getHibernateTemplate().find(from User);
这样的语句会把user全部查询出来,并得到一个集合,这时如果使用excludes2,将会把role和password两个字段过滤掉。
第二种情况,this.getHibernateTemplate().find(from User);我也得到一个List,但是我又将这个list保存到一个Map.put("allCategories",list);然后将这个Map返回到控制层,控制层又将这个返回的map放进另外一个Map后返回jsonView,这样使用excludes2,你会发现数据根本没有过滤掉,不要着急!!淡定
因为spring默认只处理一层数据,至于我刚才说的是2层了,所以没有过滤掉,换成excludes1就ok了,原因是第一个allCategories搜索到后找平级的role和password没有找到,但是发现还有一曾,在进入下一层的时候发现ok找到了!!!
时间关系,有问题请留言讨论