Let's talk about demand first. A normal Web form submission that automatically generates results from the specified format based on the template. The advantages of the form in the format of data, so that the properties are very intuitive to display, the user can be more simple and intuitive input. But the end result of the business is not the form, so there is the need. The nature of the requirements is somewhat similar to the substitution of El expressions, but this expression template is dynamically configured rather than a common XML static file.
Summarize the requirements, as a general rule: populate the template with the business attributes into real-time settings based on the user's input to generate the final result.
It is not difficult to find several key points here.
- The template can be configured in real time, in DB mode.
- There is a user input behavior, which means there are unstable factors, including special characters, null and so on. However, because it is a fill, you can filter out special characters, as long as you handle null. (need to process null on demand)
- Since it is filled, make sure two points. One is that the filled object cannot confuse the disorder, and the second is that the order of the padding cannot be faulted.
- This template is injected as a third-party jar dependency, so you must circumvent any business factors.
Think clearly about the focus of the design, and then look at the design, first look at the class diagram.
Entity layer: The design of the Templateel:el expression. Because it's just a simple text template, so long as you care about the El prefix, suffix. Here the HTML configuration is for the foreground effect display, editing use. Propertymethod is expressed by considering that different systems and programmers may not be consistent in declaring getter and setter methods. Templateelformat: For special El Properties, there is a certain format convention. I currently only use the time format, which will be introduced later. Template: The common group+unique Code unique label templates. Templateelconfig: It's an ordinary mapping watch. Resultvo: Returns the specified VO according to business requirements. It is recommended to use an abstract class to serve the template.
Service Layer:
Templatefactory: General factory class, gets the specified generator.
Templategenerator: the regular generator.
Or the old adage that structure jars are provided, implemented at the business level. including Vo. The benefit is that the jar is completely isolated from the business. The downside is that every business system is written and implemented, and there is a risk of conflict.
The following complements the implementation of the generator implementation class, the other code is nothing special.
1. Exception is a few types of exceptions that the template jar encapsulates. Because there is no business code, there is no way to control the caller's arguments, and the template may not exist.
1. Gets the template templates, This.templateLogic.findByGroupAndCode (Groupcode, Templatecode), if (null = = template) { Logger.info ("Invalid template access. Group code:{}, template code:{} ", Groupcode, Templatecode); Result.setexception (New Templatenotexistexception (" Template not exists! Group code: "+ Groupcode +", template code: "+ Templatecode)", return;
2. Read the configuration of the template el. If there is no configuration, warning. This is read by SEQ, ready for template stitching.
2. Read template configuration list<templateelconfig> templateelconfigs = This.templateElConfigLogic.findByTemplateId (template.getid ()) if (null = = Templateelconfigs | | templateelconfigs.isempty ()) {Logger.info ("there ' s no express configuration for Temp late:{} ", Template.getname ()); return;}
3. Parse the EL configuration to generate the final string.
for (Templateelconfig templateelconfig:templateelconfigs) {templateel el = This.templateElLogic.findOne ( Templateelconfig.getelid ()); if (el = = null) {logger.info ("Missing el Config, Template el config id:{}", Templateelconfig.getid ()); continue;} String datasourcevalue = "";//3.1 string methodName = El.getpropertymethod (); if (Stringutils.isempty (MethodName)) { Logger.info ("Missing property method config for el:{}", El.getid ()); continue;} try {method = Null;object propertyvalue;if (El.getel (). Contains ("Date")) {method = (method) Datasource.getclass () . GetMethod (methodName);p ropertyvalue = (Date) Method.invoke (datasource);} else {method = (method) Datasource.getclass (). GetMethod (methodName);p ropertyvalue = (String) Method.invoke (datasource );} if (PropertyValue instanceof Date) {Templateelformat Elformat = this.templateElFormatLogic.findByElId (El.getid ()); String TimeFormat = default_date_format;if (null! = Elformat) {TimeFormat = Elformat.getformat ();} SimpleDateFormat format = new SimpledatefoRmat (timeformat);d atasourcevalue = Format.format (PropertyValue);} else if (propertyvalue instanceof String) {datasourcevalue = (String) propertyvalue;}} catch (Exception e) {logger.error ("No such method.", E); Result.setexception (New Illegaltemplateconfigexception ("No Such method. ", e)); return;} if (Stringutils.isnoneempty (Datasourcevalue)) {if (Stringutils.isnoneempty (El.getprefix ())) {Buffer.append ( El.getprefix ());} Buffer.append (Datasourcevalue); if (Stringutils.isnotempty (El.getsuffix ())) {Buffer.append (El.getSuffix ());} if (Stringutils.isnoneempty (El.gethtmlprefix ())) {Htmlbuffer.append (El.gethtmlprefix ());} Htmlbuffer.append (Datasourcevalue); if (Stringutils.isnotempty (El.gethtmlsuffix ())) {Htmlbuffer.append ( El.gethtmlsuffix ());}}}
There is no business logic, so it can be put inside the jar, each business system as long as the control of how to call the structure of the line.
Java Custom Template Design