@JsonFormat(pattern = DateUtil.WEB_FORMAT, shape = JsonFormat.Shape.STRING) @JsonProperty("expDate") private Date expDate; 日期格式化未生效
时间: 2024-06-06 21:09:48 浏览: 160
您好,很抱歉我不能理解您的问题。请问您需要我对 @JsonFormat(pattern = DateUtil.WEB_FORMAT, shape = JsonFormat.Shape.STRING) @JsonProperty("expDate") private Date expDate; 进行什么样的操作呢?
相关问题
优化这段代码: @Override public void exportExcel120(CertfInfoOrderQueryVo vo, HttpServletResponse response) { try { // 根据id查询数据 Page<DasymesCertfInfoPojo> page = new Page<>(1, 10); IPage<DasymesCertfInfoPojo> iPage = infoMapper.openQuery(page, vo); List<DasymesCertfInfoPojo> dataList = iPage.getRecords(); if (dataList == null || dataList.size() == 0) return; // 获取下载模板 InputStream template = DasymesQltyMpCertfInfoModServiceImpl.class.getClassLoader().getResourceAsStream("交付120厂产品合格证.xlsx"); if (template == null) return; DasymesCertfInfoPojo info = dataList.get(0); // 获取静态数据 Map<String, String> staticSource = getStaticSource(info); // 获取动态数据 List<DynamicSource> dynamicSourceList = getDynamicSourceList(info.getAttachedList()); Map<ByteArrayOutputStream, XSSFClientAnchor> imgMap = new HashMap<>(); ByteArrayOutputStream userByteArrayOut = new ByteArrayOutputStream(); File file = new File("D:\hegezhengtupian\"+ UserCache.getThreadLocalUser().getUserRealName() +".png"); if (file.canRead()) { bufferImg = ImageIO.read(file); ImageIO.write(bufferImg, "png", userByteArrayOut); XSSFClientAnchor userAnchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 13, 2, 14); userAnchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); imgMap.put(userByteArrayOut, userAnchor); } Workbook workbook = ExcelTemplateUtil.buildByTemplate(template, staticSource, dynamicSourceList, imgMap); ExcelTemplateUtil.save(workbook, "交付120厂产品合格证", response); // ExcelTemplateUtil.save(workbook, "D:\工作\文档\template.xlsx"); } catch (Exception e) { throw new RuntimeException("导出失败,请联系管理员"); } } // 编辑动态数据 private List<DynamicSource> getDynamicSourceList(List<DasymesQltyMpCertfInfoItemMod> itemModList) { List<Map<String, String>> mapList = new ArrayList<>(); if (itemModList == null || itemModList.size() == 0) { Map<String, String> map = new HashMap<>(); map.put("no", null); map.put("name", null); map.put("portion", null); mapList.add(map); return DynamicSource.createList("detail", mapList); } for (int i = 1; i <= itemModList.size(); i++) { DasymesQltyMpCertfInfoItemMod item = itemModList.get(i - 1); Map<String, String> map = new HashMap<>(); map.put("no", i + ""); map.put("name", item.getItemName()); map.put("portion", item.getPortion()); mapList.add(map); } return DynamicSource.createList("detail", mapList); }
1. 将方法拆分成更小的方法,提高可读性和可维护性。
2. 使用 try-with-resources 语句来自动关闭资源。
3. 对于文件路径,使用 File.separator 替代硬编码的路径分隔符。
4. 避免使用 magic number ,使用常量或枚举类型来代替。
5. 在使用集合时,使用 isEmpty() 方法代替 size() == 0。
6. 使用 stream() 方法来代替 for 循环。
7. 使用 lambda 表达式来代替匿名内部类。
8. 在捕获异常时,应该尽可能地捕获特定的异常。
优化后的代码如下:
```java
@Override
public void exportExcel120(CertfInfoOrderQueryVo vo, HttpServletResponse response) {
try (InputStream template = DasymesQltyMpCertfInfoModServiceImpl.class.getClassLoader().getResourceAsStream("交付120厂产品合格证.xlsx")) {
Page<DasymesCertfInfoPojo> page = new Page<>(1, PAGE_SIZE);
IPage<DasymesCertfInfoPojo> iPage = infoMapper.openQuery(page, vo);
List<DasymesCertfInfoPojo> dataList = iPage.getRecords();
if (dataList.isEmpty()) {
return;
}
DasymesCertfInfoPojo info = dataList.get(0);
Map<String, String> staticSource = getStaticSource(info);
List<DynamicSource> dynamicSourceList = getDynamicSourceList(info.getAttachedList());
Map<ByteArrayOutputStream, XSSFClientAnchor> imgMap = getImageMap();
Workbook workbook = ExcelTemplateUtil.buildByTemplate(template, staticSource, dynamicSourceList, imgMap);
ExcelTemplateUtil.save(workbook, "交付120厂产品合格证", response);
} catch (IOException e) {
throw new RuntimeException("导出失败,请联系管理员");
}
}
private Map<String, String> getStaticSource(DasymesCertfInfoPojo info) {
Map<String, String> staticSource = new HashMap<>();
staticSource.put("certfNo", info.getCertfNo());
staticSource.put("prodName", info.getProdName());
staticSource.put("spec", info.getSpec());
staticSource.put("prodNo", info.getProdNo());
staticSource.put("prodDate", info.getProdDate());
staticSource.put("expDate", info.getExpDate());
staticSource.put("prodUnit", info.getProdUnit());
staticSource.put("prodAddr", info.getProdAddr());
staticSource.put("qualityGrade", info.getQualityGrade());
staticSource.put("packing", info.getPacking());
staticSource.put("remark", info.getRemark());
return staticSource;
}
private List<DynamicSource> getDynamicSourceList(List<DasymesQltyMpCertfInfoItemMod> itemModList) {
List<Map<String, String>> mapList = new ArrayList<>();
if (itemModList.isEmpty()) {
Map<String, String> map = new HashMap<>();
map.put("no", null);
map.put("name", null);
map.put("portion", null);
mapList.add(map);
return DynamicSource.createList("detail", mapList);
}
return itemModList.stream()
.map(item -> {
Map<String, String> map = new HashMap<>();
map.put("no", String.valueOf(itemModList.indexOf(item) + 1));
map.put("name", item.getItemName());
map.put("portion", item.getPortion());
return map;
})
.collect(Collectors.collectingAndThen(Collectors.toList(), DynamicSource::createList));
}
private Map<ByteArrayOutputStream, XSSFClientAnchor> getImageMap() throws IOException {
Map<ByteArrayOutputStream, XSSFClientAnchor> imgMap = new HashMap<>();
ByteArrayOutputStream userByteArrayOut = new ByteArrayOutputStream();
File file = new File("D:" + File.separator + "hegezhengtupian" + File.separator + UserCache.getThreadLocalUser().getUserRealName() + ".png");
if (file.canRead()) {
BufferedImage bufferImg = ImageIO.read(file);
ImageIO.write(bufferImg, "png", userByteArrayOut);
XSSFClientAnchor userAnchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 13, 2, 14);
userAnchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
imgMap.put(userByteArrayOut, userAnchor);
}
return imgMap;
}
```
(function(vita) { var systemUserAdd = vita.Backbone.BizView.extend({ events : { "click #isSysUser" : "_isChecked", "click .close" : "_closePage" }, _initialize : function() { var widget = this, element = $(widget.el), global = widget.global, data = element.data("data"); //时间控件类型初始化 var datetime = widget.require("datetime"); var timeTxts = element.find("input[name='timeTxt']"); if (timeTxts.length) { timeTxts.each(function(i, timeTxt) { datetime.register($(timeTxt), { preset : widget.global.preset }); }); } var initEffDate = widget.getNowDate(new Date()); var initExpDate = "2999-12-31"; element.find("#effDate").val(initEffDate); element.find("#expDate").val(initExpDate); //获取登陆员工ID widget.model.set('loginStaffId',data.loginStaffId); }, global : { preset : 'date', chooseArea : "../sm/chooseArea", staffSelectList : "../sm/staffSelectList", selectOrg : "../sm/selectOrg" }, getNowDate : function (date) { var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? '0' + m : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; return y + '-' + m + '-' + d; }, _isChecked : function(e) { var widget = this, element = $(widget.el); var button = $(e.target).closest("input"); var id = button.attr("id") || ""; var idVal = "#"+id; var ischecked = $(idVal).prop('checked'); if(id=="isSysUser"){ if(ischecked){ widget.model.set({"isSysUser" : '1'}); }else{ widget.model.set({"isSysUser" : "0"}); } } }, _closePage : function() { var widget = this, element = $(widget.el); element.closest("[data-widgetfullname=vita-dialog]").dialog("close"); } }); vita.widget.register("systemUserAdd", systemUserAdd, true); })(window.vita);解释一下这些代码
这段代码是一个JavaScript文件,其中定义了一个名为systemUserAdd的Backbone.BizView对象,它包含了一些事件和方法。Backbone.BizView是一个基于Backbone.js的视图组件,它可以将数据模型和模板组合到一起,生成可复用的视图组件。
该对象包含了一些事件,如点击按钮、关闭页面等等,并且对时间控件类型进行了初始化。同时,该对象还定义了一些全局变量,如日期格式、选择区域、员工列表、选择组织等等。其中,getNowDate方法用于获取当前时间并转化为指定格式的日期。
isChecked方法用于判断是否勾选了系统用户选项,并且根据勾选情况设置了isSysUser属性的值。closePage方法用于关闭当前页面。
最后,该代码通过vita.widget.register方法将systemUserAdd注册为一个widget组件,以便在页面中进行调用。
阅读全文