1.cms管理系统的基本框架和主要功能
2.csm中用到的主要技术
ssm基本框架
GridManager展示表的内容和发送高级查询的请求
使用ueitor富文本编辑器加在模态框中
使用页面静态化技术生产静态页面
使用spring-mvc的文件上传来保存图片
使用cookie保存账户和密码
3.cms中的难点
(1)页面静态化技术:模板+数据=静态页面
先封装一个页面静态化的工具类
public class FreemakerUtils {
public static String Freemarker1(String path,String name,Object obj,String suffix) {
FileWriter out = null;
try {
//获取模板对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
File file = new File(path);
configuration.setDirectoryForTemplateLoading(file);
configuration.setDefaultEncoding("UTF-8");
Template template = configuration.getTemplate(name);
//准备数据
//生成静态资源
String url = System.currentTimeMillis()+suffix;
File file2 = new File(file, url);
out = new FileWriter(file2);
template.process(obj, out);
return url;
} catch (Exception e) {
// TODO: handle exception
}finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
生产静态页面
public void save(Article article,HttpServletRequest req) {
String path = req.getServletContext().getRealPath("/static/template");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String url = FreemakerUtils.Freemarker1(path, "article.ftl", article, ".html");
article.setUrl(url);
if (article.getId()==null) {
mapper.add(article);
} else {
//根据id删除原来的静态html文件
Article idArticle=mapper.selectById(article.getId());
String url2 = idArticle.getUrl();
System.err.println(url2);
File file2 = new File(path, url2);
if (file2.exists()) {
file2.delete();
}
mapper.update(article);
}
}
(2)文件上传
public void save(Slide slide, MultipartFile photo, HttpServletRequest req) throws Exception {
if (photo!=null) {
//获取输入流
InputStream input = photo.getInputStream();
//获取uoload的路径
String realPath = req.getServletContext().getRealPath("/static/upload");
File file = new File(realPath);
if (!file.exists()) {
file.mkdirs();
}
//获取图片名称
String filename = photo.getOriginalFilename();
String suffix=filename.substring(filename.lastIndexOf("."));
String name=System.currentTimeMillis()+suffix;
//获取输出流
FileOutputStream output= new FileOutputStream(new File(file, name));
//文件上传的核心代码
IOUtils.copy(input, output);
//关流
output.close();
input.close();
//保存到对象中
slide.setName(name);
slide.setPath("/static/upload/"+name);
System.out.println(slide);
}
//根据Id有无执行添加方法或者修改方法
if (slide.getId()==null) {
mapper.add(slide);
}else{
mapper.update(slide);
}
}
(3)后台表的展示
jsp代码
<!-- 列表展示 -->
<div class="row app-title">
<div class="col-md-12">
<!-- 存放table列表 -->
<table id='table-demo-ajaxPageCode'></table>
</div>
</div>
</main>
js代码
document.querySelector('#table-demo-ajaxPageCode').GM({
gridManagerName: 'demo-ajaxPageCode',
ajaxData: '/system/article/selectAll',
ajaxType: 'POST',
supportAjaxPage: true,
sizeData: [5,10,15,20],
pageSize: 5,
currentPageKey: "currentPage",
pageSizeKey: "pageSize",
height: "100%",
columnData: [
{
key: 'title',
align:'center',
text: '文章名'
},{
key: 'type',
align:'center',
text: '文章类型',
template: function(cell, row, index, key){
return cell.name;
}
},{
key: 'url',
align:'center',
text: 'url'
},{
key: 'clickCount',
align:'center',
text: '点击量'
},{
key: 'content',
align:'center',
text: '文章内容'
},{
key: 'createDate',
align:'center',
text: '创建时间'
},{
key: 'enable',
align:'center',
text: '是否启用',
template: function(cell, row, index, key){
return cell?'启用':'禁用';
}
},{
key: 'id',
align:'center',
text: '操作 <a id="add" href="javascript:;">添加</a>',
template: function(cell, row, index, key){
return '<a data-id="'+cell+'" style="color:red" href="javascript:;">删除</a> '+"<a data-row='"+JSON.stringify(row)+"'style='color:blue' href='javascript:;''>修改</a>"
}
}
]
});
表的展示模型
(4)登录和注销功能
登录的controller层
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public AJaxResult login(HttpSession session,String username,String password,Integer remember,HttpServletRequest req,HttpServletResponse resp) {
try {
User user=service.login(username,password);
session.setAttribute("USER_IN_SESSION", user);
if (remember!=null) {
Cookie c1 = new Cookie("username", username);
Cookie c2 = new Cookie("password", password);
//设置路径
c1.setPath("/");
c2.setPath("/");
//设置生命周期
c1.setMaxAge(7*24*3600);
c2.setMaxAge(7*24*3600);
//将cookie添加到响应中
resp.addCookie(c1);
resp.addCookie(c2);
}else{
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username") || cookie.getName().equals("password")) {
cookie.setMaxAge(0);
cookie.setPath("/");
resp.addCookie(cookie);
}
}
}
return new AJaxResult();
} catch (Exception e) {
return new AJaxResult(false, e.getMessage());
}
}
登录的service层
public User login(String username, String password) throws Exception {
User user=mapper.selectByUsername(username);
if (user==null) {
throw new Exception("用户名不存在!!");
}else {
if (!user.getPassword().equals(password)) {
throw new Exception("密码输入错误!!!!!");
}else {
return user;
}
}
}
注销功能
@RequestMapping("/logout")
public String logout(HttpSession session) {
session.invalidate();
return "login";
}