freemarker模板引擎

一、创建模板


来客姓名: ${informationMap.VISITOR_NAME}

到访时间: ${informationMap.ARRIVEVISIT_TIME}

同行人数: ${informationMap.PEER_NUMBER}

来客职务: ${informationMap.DUTY_NAME}

行政级别: ${informationMap.ADMINISTRATIVE_LEVEL_LABEL}

联系人: ${informationMap.CONTACTS}

联系电话: ${informationMap.CONTACTS_TEL}

任务内容

任务内容: ${taskMap.TASK_CONTENT_LABEL}

到站方式: ${taskMap.ARRIVE_STATION_LABEL}

交通工具: ${taskMap.ARRIVE_INFORMATION1},${taskMap.ARRIVE_INFORMATION2},${taskMap.ARRIVE_INFORMATION3}

出发地: ${taskMap.ARRIVE_PLACE}

离站方式: ${taskMap.LEAVE_STATION_LABEL}

交通工具: ${taskMap.LEAVE_INFORMATION1},${taskMap.LEAVE_INFORMATION2},${taskMap.LEAVE_INFORMATION3}

目的地: ${taskMap.LEAVE_PLACE}

上传下达

执行部门/人: ${signMap.IMPLEMENT_ORG_USER}

执行领导: ${signMap.IMPLEMENT_LEADER}

周知部门/人: ${signMap.KNOWN_ORG_USER}

贵宾室: ${signMap.POSTDUTY_PLACE}

接待人员: ${signMap.RECEPTION_USER}

二、java

在执行保存功能时,把模板保存到数据库

//更新模板
VisitorImportant visitorImportant = baseInfoService.getObjectById(VisitorImportant.class, id);
//进行保存模板操作
createVisitorImportantDetail(visitorImportant, "visitorImportantPC.ftl");
	/**
	 * 
	 * @Description 保存重要来客可查看信息的模板
	 * @param visitorImportant
	 * @param templatesName
	 * @return ReturnDatas
	 * @throws Exception
	 *
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public VisitorImportantDetail createVisitorImportantDetail(VisitorImportant visitorImportant,String templatesName) throws Exception {
		ReturnDatas returnDatas = new ReturnDatas();
		String id_visitor_important = visitorImportant.getId();
		VisitorImportantDetail visitorImportantDetail = visitorImportantDetailService.getVisitorImportantDetailByInstId(id_visitor_important);
		String informationSql = "select  " +
    			"t.id as id , " + //主键id
				"t.visitor_name as visitor_name," + //来客姓名
    			"t.arrivevisit_time as arrivevisit_time, " + //到访时间
    			"t.peer_number as peer_number, " + //同行人数
    			"t.duty_name as duty_name, " + //来客职务
    			"t.administrative_level_label as administrative_level_label, " + //行政级别
    			"t.contacts as contacts ,"+ //联系人
    			"t.contacts_tel as contacts_tel " + //联系电话
    			" from v_visitor_important t  " + 
    			" where t.id = '"+id_visitor_important+"'";
		List> informationMap = baseInfoService.listMapQueryBySql(informationSql);
		LinkedHashMap linkMap = new LinkedHashMap();
		VisitorImportantDetailPo visitorImportantDetailPo = new VisitorImportantDetailPo();
    	if(informationMap!=null && informationMap.size()>0) {
    		Map map = informationMap.get(0);
    		//替换jsonArray中的值
    		for(String key : map.keySet()) {
    			linkMap.put(key, map.get(key));
    		}
    		visitorImportantDetailPo.setInformationMap(linkMap);
    	}
    	
    	String hql = "select u from VisitorImportant u where id = '"+id_visitor_important+"'";
    	VisitorImportant v = (VisitorImportant) baseInfoService.getObjectByHql(hql);
    	String arriveType = v.getDict_arrive_station();
    	String arrive_information1 = "t.arrive_station_transportation";
    	String arrive_information2 = "t.arrive_station_contact";
    	String arrive_information3 = "t.arrive_station_othertime";
    	String arrive_place = arrive_information2;
    	if("0".equals(arriveType)) {
    		arrive_information1 = "t.arrive_station_train";
    		arrive_information2 = "t.arrive_station_seat";
    		arrive_information3 = "t.arrive_station_place";
    		arrive_place = arrive_information3;
		}else if("1".equals(arriveType)) {
			arrive_information1 = "t.arrive_station_car";
    		arrive_information2 = "t.arrive_station_park";
    		arrive_information3 = "t.arrive_station_cartime";
    		arrive_place = arrive_information2;
		}
    	String leaveType = v.getDict_leave_station();
    	String leave_information1 = "t.leave_station_transportation";
    	String leave_information2 = "t.leave_station_contact";
    	String leave_information3 = "t.leave_station_othertime";
    	String leave_place = leave_information2;
    	if("0".equals(leaveType)) {
    		leave_information1 = "t.leave_station_train";
    		leave_information2 = "t.leave_station_seat";
    		leave_information3 = "t.leave_station_place";
    		leave_place = leave_information3;
		}else if("1".equals(leaveType)) {
			leave_information1 = "t.leave_station_car";
    		leave_information2 = "t.leave_station_park";
    		leave_information3 = "t.leave_station_cartime";
    		leave_place = leave_information2;
		}
    	String taskSql = "select  " +
    			"t.id as id, " + //主键id
				"t.task_content_label as task_content_label," + //任务内容
    			"t.arrive_station_label as arrive_station_label, " + //到站方式
    			arrive_information1+" as arrive_information1, " + //交通工具
    			arrive_information2+" as arrive_information2, " + //交通工具
    			arrive_information3+" as arrive_information3, " + //交通工具
    			arrive_place+" as arrive_place, " + //出发地
    			"t.leave_station_label as leave_station_label, " + //离站方式
    			leave_information1+" as leave_information1, " + //交通工具
    			leave_information2+" as leave_information2, " + //交通工具
    			leave_information3+" as leave_information3, " + //交通工具
    			leave_place+" as leave_place " + //目的地
    			" from v_visitor_important t  " + 
    			" where t.id = '"+id_visitor_important+"'";
		List> taskMap = baseInfoService.listMapQueryBySql(taskSql);
		LinkedHashMap taskLinkMap = new LinkedHashMap();
    	if(taskMap!=null && taskMap.size()>0) {
    		Map map = taskMap.get(0);
    		//替换jsonArray中的值
    		for(String key : map.keySet()) {
    			taskLinkMap.put(key, map.get(key));
    		}
    		visitorImportantDetailPo.setTaskMap(taskLinkMap);
    	}
    	
    	String signSql = "select  " +
    			"t.id as id , " + //主键id
				"t.implement_org_user as implement_org_user," + //执行部门/人
    			"t.implement_leader as implement_leader, " + //执行领导
    			"t.known_org_user as known_org_user, " + //周知部门/人
    			"t.known_leader as known_leader, " + //周知领导
    			"t.postduty_place as postduty_place, " + //贵宾室
    			"t.reception_user as reception_user " + //接待人员
    			" from v_visitor_important t  " + 
    			" where t.id = '"+id_visitor_important+"'";
		List> signMap = baseInfoService.listMapQueryBySql(signSql);
		LinkedHashMap signLinkMap = new LinkedHashMap();
    	if(signMap!=null && signMap.size()>0) {
    		Map map = signMap.get(0);
    		//替换jsonArray中的值
    		for(String key : map.keySet()) {
    			signLinkMap.put(key, map.get(key));
    		}
    		visitorImportantDetailPo.setSignMap(signLinkMap);
    	}
    	
    	//字符流
        StringWriter stringWriterPC = new StringWriter();
        //StringWriter stringWriterAPP = new StringWriter();
        try {
			//Map map = JSON.parseObject(detailStr);
			//创建配置实例 
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
			//设置编码
			configuration.setDefaultEncoding("UTF-8");
			//空值
			configuration.setClassicCompatible(true);
			//ftl模板文件
			configuration.setClassForTemplateLoading(VisitorImportantServiceImpl.class, "/templates/");
			String tempPc = "visitorImportantPC.ftl";
			//String tempApp = "taskApp.ftl";
			//获取PC模板 
			Template templatePC = configuration.getTemplate(tempPc);
			//替换
			templatePC.process(visitorImportantDetailPo, stringWriterPC);
		} catch (Exception e) {
			LogUtil.error(e.getMessage(),e);
            returnDatas.setStatus(ReturnDatas.ERROR);
            returnDatas.setMessage("freeMarker替换模板出错");
		}
        
        System.out.println(stringWriterPC.toString());
    	//转化为JSON
    	String detailStr = JSON.toJSONString(visitorImportantDetailPo);
    	
    	String contentPC = stringWriterPC.toString();
     	String contentAPP = "";
     
    	//封装emergencyInsyDetail对象
     	visitorImportantDetail.setId_visitor_important(id_visitor_important);
     	visitorImportantDetail.setInst_content(detailStr);
     	visitorImportantDetail.setInst_content_pc(contentPC);
     	visitorImportantDetail.setInst_content_app(contentAPP);
        //保存静态Html对象
     	visitorImportantDetail = visitorImportantDetailService.saveVisitorImportantDetail(visitorImportantDetail);
三、VisitorImportantDetailPo实体类
public class VisitorImportantDetailPo{

	private LinkedHashMap informationMap;
	private LinkedHashMap taskMap;
	private LinkedHashMap signMap;
	public LinkedHashMap getInformationMap() {
		return informationMap;
	}
	public void setInformationMap(LinkedHashMap informationMap) {
		this.informationMap = informationMap;
	}
	public LinkedHashMap getTaskMap() {
		return taskMap;
	}
	public void setTaskMap(LinkedHashMap taskMap) {
		this.taskMap = taskMap;
	}
	public LinkedHashMap getSignMap() {
		return signMap;
	}
	public void setSignMap(LinkedHashMap signMap) {
		this.signMap = signMap;
	}
	
}

四、数据库效果如下

freemarker模板引擎_第1张图片

freemarker模板引擎_第2张图片

从数据库读取到数据后替换页面即可

你可能感兴趣的:(freemarker模板引擎)