CMS系统学习(四)

阅读: 评论:0

CMS系统学习(四)

CMS系统学习(四)

一、模板技术

    • 1.freemarker:是比较流行的一个模板技术【ftl】
    1. 使用freemarker的步骤(以后只需要百度查找即可)
      1.导包(freemarker是一个小框架)
      2.创建一个配置对象Configuration(加个版本)
      3.设置加载路径
      4.设置字符集(默认)
      5.创建模板(准备一个ftl模板)
      6.准备数据(Map,对象)
      7.数据+模板=输出文件(Writer)
package s.freemarker;import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.junit.Test;plate.Configuration;
plate.Template;public class FreemarkerTest {/***  2.创建一个配置对象 (传递一个版本 )3.设置模板加载路径4设置一个模板编码5.获取一个模板对象6获取一个数据7生成文件* @throws Exception*/@Testpublic void testMap() throws Exception {//1.创建一个配置对象 (传递一个版本 )Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);//2.设置模板的加载路径(就是找到模板的目标的路径)// Directory:目录  Template:模板  Loading:加载String path = "H:/eclipse-jee-neon-2-win32-x86_64/eclipse-workspace/cms/src/main/webapp/template";cfg.setDirectoryForTemplateLoading(new File(path));//3.设置模板的编码 UTF-8cfg.setDefaultEncoding("UTF-8");//4.获取一个模板对象Template template = Template("my.ftl");//5.准备相应的数据(Map,对象)Map dataMap = new HashMap<>();dataMap.put("username", "飞天猪");dataMap.put("age", 45);dataMap.put("sex", false);List<MyUser> userList = Arrays.asList(new MyUser("游戏币",23),new MyUser("人民币",23),new MyUser("金币",23),new MyUser("QQ币",23));//把集合放到map中去dataMap.put("userList", userList);//6.生成文件//数据(dataMap)+模板(template) = 输出文件(out)File newFile = new File(path,"mytest.html");FileWriter out = new FileWriter(newFile);template.process(dataMap, out);out.flush();}@Testpublic void testMap02() throws Exception {//1.创建一个配置对象 (传递一个版本 )Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);//2.设置模板的加载路径(就是找到模板的目标的路径)// Directory:目录  Template:模板  Loading:加载String path = "H:/eclipse-jee-neon-2-win32-x86_64/eclipse-workspace/cms/src/main/webapp/template";cfg.setDirectoryForTemplateLoading(new File(path));//3.设置模板的编码 UTF-8cfg.setDefaultEncoding("UTF-8");//4.获取一个模板对象Template template = Template("my2.ftl");//5.准备相应的数据(Map,对象)Map dataMap = new HashMap<>();//  准备数据mapMap userMap = new HashMap<>();userMap.put("username", "飞天猪");userMap.put("age", 45);userMap.put("sex", "男");dataMap.put("userMap", userMap);//6.生成文件//数据(dataMap)+模板(template) = 输出文件(out)File newFile = new File(path,"mytest2.html");FileWriter out = new FileWriter(newFile);template.process(dataMap, out);out.flush();}@Testpublic void testObj() throws Exception {//1.创建一个配置对象(给它一个版本)Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);//2.设置模板的路径的加载String path = "H:/eclipse-jee-neon-2-win32-x86_64/eclipse-workspace/cms/src/main/webapp/template";cfg.setDirectoryForTemplateLoading(new File(path));//3.设置默认编码cfg.setDefaultEncoding("UTF-8");//4.获取模板Template template = Template("my.ftl");//5.准备数据MyUser user = new MyUser();user.setUsername("跑地猪");user.setAge(12);//6.把数据,模板进行输出File file = new File(path,"mytest2.html");FileWriter out = new FileWriter(file);template.process(user, out);out.flush();}}

模板:my.ftl

<html>
<head><meta charset="UTF-8"><title>我是一个测试</title>
</head>
<body>${username}, 你好啊!!! ============ ${age} ======<#if sex>男<#else>女</#if><#list userList as user>${user.username},${user.age}</#list></body>
</html>

模板:my2.ftl

<html>
<head><meta charset="UTF-8"><title>我是一个测试</title>
</head>
<body>Map是否可以循环???<#list userMap?keys as key>${key} ==== ${userMap[key]}</#list>	
</body>
</html>

3.案例一:代码生成器(半成品)

package s.freemarker;import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.sql.DataSource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import st.context.ContextConfiguration;
import st.context.junit4.SpringJUnit4ClassRunner;plate.Configuration;
plate.Template;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("l")
public class CreateDomainTest {@Autowiredprivate DataSource dataSource;String tableName = "t_user";String className = "User";@Testpublic void testCreate() throws Exception {//1.拿到连接对象Connection connection = Connection();//2.确定找的是哪一张表PreparedStatement ps = connection.prepareStatement("select * from "+tableName);//3.拿到结果集ResultSet rs = ps.executeQuery();//3.拿到参数的原数据ResultSetMetaData metaData = rs.getMetaData();//4.拿到当前表有多少个字段int count = ColumnCount();//5.遍历(循环)相应的字段// 准备list,里面会装咱们的所有字段List<Map> list = new ArrayList<>(); for(int i=1;i<=count;i++){//System.out.ColumnTypeName(i));String type = ColumnClassName(i).replaceFirst("java.lang.", "");String name &#ColumnName(i);//把name的第一个首字母变成大写的字母 NameString upperName = name.substring(0, 1).toUpperCase() + name.substring(1);Map<String,Object> map = new HashMap<>();map.put("type", type);map.put("name", name);map.put("upperName", upperName);list.add(map);}//准备好模板技术的代码//1.创建一个配置对象 (传递一个版本 )Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);//2.设置模板的加载路径(就是找到模板的目标的路径)// Directory:目录  Template:模板  Loading:加载String path = "H:/eclipse-jee-neon-2-win32-x86_64/eclipse-workspace/cms/src/main/webapp/template";cfg.setDirectoryForTemplateLoading(new File(path));//3.设置模板的编码 UTF-8cfg.setDefaultEncoding("UTF-8");//4.获取一个模板对象Template template = Template("domain.java");//5.准备相应的数据(Map,对象)Map dataMap = new HashMap<>();dataMap.put("className", className);dataMap.put("filedList", list);//6.生成文件//数据(dataMap)+模板(template) = 输出文件(out)File newFile = new File(path,className+".java");FileWriter out = new FileWriter(newFile);template.process(dataMap, out);out.flush();}}

模板:domain.java

package s.domain;public class ${className} {<#list filedList as filed>private ${pe} ${filed.name} ;</#list><#list filedList as filed>public ${pe} get${filed.upperName}() {return ${filed.name};}public void set${filed.upperName}(${pe} ${filed.name}) {this.${filed.name} = ${filed.name};}</#list>}

4.案例二:动态网页静态化(添加,修改)

	修改的时候需要把原生的那个静态化页面删除删除数据的时候也要把静态化页面删除需要把路径记录下来,才可能去找到这个静态化页面
//添加或者修改的跳转@RequestMapping("/save")public String save(Jobs jobs,HttpServletRequest req) throws Exception{//如果这个页面已经存在,那么我们就要把它给干掉(删除)String realPath = ServletContext().getRealPath("/statichtml");String htmlUrl = Htmlurl();if(htmlUrl!=null){File jobFile = new File(realPath,htmlUrl);// 判断这个文件是否存在ists()){(); //垃圾回收jobFile.delete();}}//1.拿到配置对象Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);//2.加载路径cfg.setDirectoryForTemplateLoading(new File(realPath));//3.设置字符编码cfg.setDefaultEncoding("UTF-8");//4.获取模板Template template = Template("details.ftl");//5.准备数据(数据就是jobs)Map dataMap = new HashMap<>();dataMap.put("job", jobs);//6.输出数据//  6.1 准备一个html的名称(要保证唯一性)String uuidHtml = UUID.randomUUID().toString() +".html";File file = new File(realPath,uuidHtml);FileWriter out = new FileWriter(file);// 6.2 把数据+模板=输出template.process(dataMap, out);out.flush();out.close();//把相应的名称放到jobs中去jobs.setHtmlurl(uuidHtml);Id()!=null){jobsService.update(jobs);}else{jobsService.save(jobs);}return "redirect:/jobs/query";}//添加或者修改的跳转@RequestMapping("/delete/{id}")public String delete(@PathVariable("id") Integer id,HttpServletRequest req){//拿到相应的地址String realPath = ServletContext().getRealPath("/statichtml");Jobs jobs = jobsService.findOne(id);String htmlUrl = Htmlurl();//创建相应的文件File file = new File(realPath,htmlUrl);ists()){();file.delete();}jobsService.delete(id);return "redirect:/jobs/query";}

本文发布于:2024-01-28 15:54:45,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064284888549.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:Use
下一篇:高级API复习
标签:系统   CMS
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23