Java custom template design

Source: Internet
Author: User

Java custom template design

Let's talk about the requirements first. A common web form is submitted. Results In the specified format are automatically generated based on the template. The advantage of form is formatting data, so that each attribute is displayed intuitively, allowing users to input data more easily and intuitively. However, the final result of the business cannot be form, so this requirement is met. The requirement is essentially similar to the replacement of el expressions, but the expression template is dynamically configured, rather than common xml static files.

To sum up the requirements, fill in the business attributes to the real-time setting template based on the user input to generate the final result.

It is not difficult to find several key points here.

 

The template must be configurable in real time. The db method is used here. User input behaviors mean that there are unstable factors, including special characters and null characters. However, because it is filled, special characters can be filtered out, as long as null is processed. (Null needs to be processed on demand) since it is a fill, two points must be guaranteed. First, the filling objects cannot be confused, and second, the filling order cannot be wrong. This template is used as a third-party jar dependency injection, so you must avoid any business factors. I have figured out the key points of the design. Let's take a look at the design and look at the class diagram first.

Entity layer: TemplateEl: el expression design. Because it is only a simple text template, you only need to care about the prefix and suffix of el. Here, the html configuration is used for front-end display and editing. PropertyMethod is a display expression considering that the getter and setter methods may be inconsistent between different systems and programmers. TemplateElFormat: there are certain format conventions for special el attributes. Currently, I only use the time format, which will be introduced later. Template: A common unique Template for group + unique code. TemplateElConfig: A common mapping table. ResultVo: return the specified VO based on business requirements. We recommend that you use an Abstract class to serve the template.

Service Layer:

TemplateFactory: A common factory class that obtains the specified generator.

TemplateGenerator: A general generator.

 

In the old saying, the structure jar is provided for implementation at the business layer. Including vo. The advantage is that jar is completely isolated from the business. The disadvantage is that every business system must be implemented once, and there is a risk of conflict.

The following describes how to implement the generator of the class. Other code is nothing special.

 

1. Exceptions are several types of exceptions encapsulated by the template jar. Because there is no business code, the caller's parameter passing cannot be controlled, and the template may not exist.

 

// 1. get Template template = 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 el configured in the template. If there is no configuration, warning. Read by seq to prepare for template splicing.
// 2. Read the template configuration List
 
  
TemplateElConfigs = this. templateElConfigLogic. findByTemplateId (template. getId (); if (null = templateElConfigs | templateElConfigs. isEmpty () {logger.info ("There's no express configuration for template :{}", template. getName (); return ;}
 
3. parse el configurations 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 method = null;Object propertyValue;if (el.getEl().contains("Date")) {method = (Method)datasource.getClass().getMethod(methodName);propertyValue = (Date)method.invoke(datasource);}else {method = (Method)datasource.getClass().getMethod(methodName);propertyValue = (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);datasourceValue = 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 here, so it can be placed in the jar. Each business system only needs to control how to structure the call.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.