首页 文章 精选 留言 我的

精选列表

搜索[Spring项目],共10000篇文章
优秀的个人博客,低调大师

在java Web项目中处理前后端日期字段自动匹配绑定

在java开发中,经常会遇到前后端交互日期格式,一般我们都会用日期格式化来处理,但时间久了,你是否感觉到繁琐而且冗余呢,本文给出一种全新的解决思路。通过正则提取出日期数字,自动将前端传的日期字符串绑定到对象的日期格式属性上去,省去一切繁琐步骤。闲话少说,文归正传。 定义BaseController public class BaseController implements ServletContextAware{ /** * 日志 **/ protected final Logger log = LoggerFactory.getLogger(BaseController.class); public HttpServletRequest request; public HttpServletResponse response; public HttpSession session; public ServletContext application; /** * 文件 **/ @Autowired(required = false) protected MultipartResolver multipartResolver; /** * @param code * @return */ public static String respJson(CodeEnum code) { return respJson(code, null); } /** * @param code * @param data * @return */ public static String respJson(CodeEnum code, Object data) { JSONObject jsono = new JSONObject(); jsono.put("code", code.getCode()); jsono.put("message", code.getMessage()); if (data != null) { jsono.put("data", data); } return jsono.toJSONString(); } /** * * @param code 错误码 * @param message 内容 * @return */ public static String respJson(String code , String message ) { return respJson(code,message,null); } /** * * @param code * @param message * @param data * @return */ public static String respJson(String code , String message , Object data){ JSONObject jsono = new JSONObject(); jsono.put("code",code); jsono.put("message",message); if (data != null) { jsono.put("data", data); } return JSON.toJSONString(jsono); } @ModelAttribute public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) { this.request = request; this.response = response; this.session = request.getSession(); } public void setServletContext(ServletContext arg0) { this.application = arg0; } /** * 获取传递过来的参数 */ public String getParameter(String key) { return request.getParameter(key); } /** * 分页对象 */ public <T> Page<T> getPage(Class<T> clazz) { Page<T> page = new Page<T>(); String pageNo = getParameter("pageNo"); String pageSize = getParameter("pageSize"); pageNo = pageNo == null || "".equals(pageNo) ? "1" : pageNo; pageSize = pageSize == null || "".equals(pageSize) ? "10" : pageSize; page.setSearchCount(true); page.setSize(Integer.valueOf(pageSize)); page.setCurrent((Integer.valueOf(pageNo))); return page; } protected String redirectOuter(String url) { return "redirect:" + url; } /** * binder用于bean属性的设置 */ @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new DateEditor()); binder.registerCustomEditor(String.class, new StringEscapeEditor()); } /** * 获取传递过来的str参数,并给默认值 */ public String getStrParameter(String key, String defaultValue) { String str = getParameter(key); if (org.apache.commons.lang3.StringUtils.isEmpty(str)) { return defaultValue; } return str.trim(); } } 上述代码中initBinder方法上添加了一个@InitBinder注解,该方法会在请求时被调用,改方法中有个DateEditor类,日期的处理就是在这个里处理完的。 DateEditor类 class DateEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.isEmpty(text)) { return; } text = text.trim(); Pattern pattern = Pattern.compile("[^0-9]"); Matcher matcher = pattern.matcher(text); text = matcher.replaceAll(""); int length = text.length(); Date date; switch(length){ case 14: date = DateTime.parse(text,DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate(); break; case 12: date = DateTime.parse(text,DateTimeFormat.forPattern("yyyyMMddHHmm")).toDate(); break; case 10: date = DateTime.parse(text,DateTimeFormat.forPattern("yyyyMMddHH")).toDate(); break; case 8: date = DateTime.parse(text,DateTimeFormat.forPattern("yyyyMMdd")).toDate(); break; case 6: date = DateTime.parse(text,DateTimeFormat.forPattern("yyyyMM")).toDate(); break; case 4: date = DateTime.parse(text,DateTimeFormat.forPattern("yyyy")).toDate(); break; default: return; } setValue(date); } } 如何使用 上面准备完成后,在需要用到的controller中继承BaseController即可! 一般网上的例子仅支持如下格式的日期: yyyyMMdd HH:mm:ss yyyyMMdd HH:mm yyyyMMdd HH yyyyMMdd yyyyMM yyyy 本文的方法,支持包含14、12、10、8、6、4个数字的所有字符串。

优秀的个人博客,低调大师

关于项目自动化测试架构的改良计划 - DataProviderEngine架构

为了基于上文我们的xml文件的架构,对其进行解析,我自己写了一个DataProviderEngine作为解析xml文件的引擎,它主要的架构如下图: 具体细节如下,为了节省时间,我直接复制我的英文邮件内容了: Step 1: The XMLReader read the raw test suite xml file (now we adopt the xml file ,since it is not binary file and easy to edit and version control) Step 2: After reading ,XMLReader store the xml string into a string variable named “ originalXmlString “, this is our starting point. Step 3: The XIncludeConverter read the originalXmlString ,and resolve the XInclude namespace ,when it resolve this file ,it will replace all the <xi:include href> element with the actual xml fragments one by one. Step 4: After replacing all <xi:include> ,XIncludeConverter will store the new xml string into string variable named “convertedXmlString”. Step 5: The XMLManipulator read the convertedXmlString ,and remove all the user configuration part from such as <add_elements>,<update_elements>,<remove_elements> ,since these information is used for letting test case end user configure how to operate the base data to meet special requirements,so these part shouldn’t exist in final data. Step 6:After removing all config elements ,The XMLManipulator store the xml string into a new string variable named “ removeAllConfXmlString” Step 7:Since the configuration information are removed in Step 5-6 ,but they still exist in original xml string ,so XMLModifyInfoExtractor read all the <add_elements>,<update_elements>,<remove_elements> part from original xml string ,and smartly match which corresponding testcase data it will operate . Step 8:After analyzing all the user modification requirement ,the XMLModifyInfoExtractor store these modification info into 3 arraylists , addElementInfoList,updateElementInfoList,removeElementInfoList. Step 9:Now it is time to begin modifying the xml ,so the XMLManipulator read the “removeAllConfXmlString” in Step 6 . Step 10:Then XMLManipulator use all the modification info in Step 8 ,and do modification operation one by one : addElementToXML() ,updateElementFromXML(),removeElementFromXML() Step 11: after finish all the modification ,XMLManipulator store the final xml string into string variable named “modifiedXmlString” Step 12: Finally it convert the xml formatted string to a jsonobject and it now can be used as a data provider which can be consumed in each test case. 本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/1221708,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码