JSPGen软件开发框架第四版 开发手册
9.2.1、需求分析:
项目名demo;
设计一个具有备忘录功能的模块,功能含:主题、内容、时间的登记、修改、删除;
9.2.2、模板设计略
9.2.3、数据表设计如下,表名为:memo:
DROP TABLE IF EXISTS `jspgen_ memo `; CREATE TABLE `jspgen_ memo ` ( `id` varchar(32) NOT NULL COMMENT 'Id', `subject` varchar(100) NOT NULL COMMENT '主题', `content` text NOT NULL COMMENT '内容', `time` bigint(13) DEFAULT NULL COMMENT '时间戳', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='备忘录';
9.2.4、数据模型
demo. entity. Memo.java文件:
package demo.entity; import fire.entity.Entity; /** * 实体类:备忘录 * @author JSPGen * @copyright (c) JSPGen.com * @created 2013年11月30 * @email jspgen@163.com * @address www.jspgen.com */ public class Memo extends Entity { private static final long serialVersionUID = 1L; public static final int SUBJECT_LENGTH = 80; // 主题长度(来自内容截取,单位字节) private String subject; // 主题 private String content; // 内容 private Long time; // 更新时间戳 public String getSubject() { return subject; } public void setSubject(String subject) { this.subject=subject; } public String getContent() { return content; } public void setContent(String content) { this.content=content; } public Long getTime() { return time; } public void setTime(Long time) { this.time=time; } @Override public String toString() { return this.getClass().getSimpleName()+"[id="+id+",subject="+subject+",content="+content+",time="+time+"]"; } }
demo.dao.MemoDao.java文件:
package demo.dao; import fire.dao.Dao; import demo.entity.Memo; /** * Dao接口:备忘录 * @author JSPGen * @copyright (c) JSPGen.com * @created 2013年08月19 * @email jspgen@163.com * @address www.jspgen.com */ public interface MemoDao extends Dao{ }
demo.dao. impl.MemoDaoImpl.java文件:
package demo.dao.impl; import java.util.Map; import fire.Compose; import fire.dao.impl.DaoImpl; import grapes.Grapes; import grapes.page.Pager; import demo.dao.MemoDao; import demo.entity.Memo; /** * Dao实现:备忘录 * @author JSPGen * @copyright (c) JSPGen.com * @created 2013年08月19 * @email jspgen@163.com * @address www.jspgen.com */ public class MemoDaoImpl extends DaoImplimplements MemoDao { }
demo. service.MemoService.java文件:
package demo.service; import fire.service.Service; import demo.entity.Memo; /** * Service接口:备忘录 * @author JSPGen * @copyright (c) JSPGen.com * @created 2013年08月19 * @email jspgen@163.com * @address www.jspgen.com */ public interface MemoService extends Service{ }
demo.service. Impl.MemoServiceImpl.java文件:
package demo.service.impl; import jspgen.action.ActionContext; import fire.FireImpl; import fire.service.impl.ServiceImpl; import demo.dao.MemoDao; import demo.entity.Memo; import demo.service.MemoService; /** * Service实体:备忘录 * @author JSPGen * @copyright (c) JSPGen.com * @created 2013年08月19 * @email jspgen@163.com * @address www.jspgen.com */ public class MemoServiceImpl extends ServiceImplimplements MemoService { private MemoDao memoDao; // 初始化 public MemoServiceImpl() { memoDao = (MemoDao) FireImpl.getDaoImpl("memoDaoImpl"); // 指定操作对象(默认) memoDao.setAccess(ActionContext.getFireAccess()); // 指定操作对象:数据源名称, 指定操作表【Map型须设定】 // memoDao.setAccess(ActionContext.getFireAccess(""), tableName); // 指定操作对象:数据源名称, 指定操作表【Map型须设定】, 表主键名称(类、表字段名),表主键类型(id、uuid) // memoDao.setAccess(ActionContext.getFireAccess(""), tableName, keyName, keyType); super.setDao(memoDao); // 指定操作Dao } }
9.2.5、业务设计及Action配置
memo. action.MemoAction.java文件:
package demo.action; import java.util.Map; import jspgen.action.Action; import demo.entity.Memo; import demo.service.MemoService; import fire.FireImpl; import grapes.Dates; import grapes.Grapes; /** * Action类:备忘录 * @author JSPGen * @copyright (c) JSPGen.com * @created 2013年03月 * @email jspgen@163.com * @address www.jspgen.com */ public class MemoAction extends Action { private Memo memo; private MemoService memoService = (MemoService) FireImpl.getServiceImpl("demo", "memoServiceImpl"); // ========================================================================================= /** * 查看 */ public String view() { // 输入判断 if(Grapes.isEmpty(getParameter("id"))) { addActionError(getMessageValue("global.id_not_empty")); // Id不允许为空! return ERROR; } // 加载数据 memo = memoService.get(id); if (memo == null) { addActionError(getMessageValue("global.data_not_exist")); // 该数据不存在! return ERROR; } setAttribute("memo", memo); // 页面赋值 return INPUT; // return VIEW; } /** * 添加 */ public String add() { return INPUT; } /** * 编辑 */ public String edit() { // 输入判断 if(Grapes.isEmpty(getParameter("id"))) { addActionError(getMessageValue("global.id_not_empty")); // Id不允许为空! return ERROR; } // 加载数据 memo = memoService.get(id); if (memo == null) { addActionError(getMessageValue("global.data_not_exist")); // 该数据不存在! return ERROR; } setAttribute("memo", memo); // 向页面模板赋值 return INPUT; } /** * 列表 */ public String list() { Mapparams = getParameterMap(false); // 接收来自页面的参数 params.put("orderBy", "time"); // params.put("order", "desc"); pager = memoService.findPager(params, pager); setAttribute("pager", pager); return LIST; } /** * 删除 */ public String delete() { // 删除 for(String id : ids){ memo = memoService.get(id); memoService.delete(memo); } // return json(true, getMessageValue("global.success")); return SUCCESS; } /** * 保存 */ public String save() { // 输入判断 String subject=null, content=null; if(Grapes.isEmpty(getParameter("content"))) { addActionError(getMessageValue("global.content_not_empty")); // 内容不允许为空! return ERROR; } content = getParameter("content"); subject = Grapes.substring(Grapes.text(content, true), Memo.SUBJECT_LENGTH, "…"); // 长度截取(一个汉字按两个字节计算) // 保存数据 memo = new Memo(); memo.setSubject(subject); memo.setContent(content); memo.setTime(Dates.getTimeMillis()); memo = memoService.save(memo); return SUCCESS; } /** * 更新 */ public String update() { // 输入判断 String subject=null, content=null; if(Grapes.isEmpty(getParameter("id"))) { addActionError(getMessageValue("global.id_not_empty")); // Id不允许为空! return ERROR; } if(Grapes.isEmpty(getParameter("content"))) { addActionError(getMessageValue("global.content_not_empty")); // 内容不允许为空! return ERROR; } content = getParameter("content"); subject = Grapes.substring(Grapes.text(content, true), Memo.SUBJECT_LENGTH, "…"); // 长度截取(一个汉字按两个字节计算) // 加载数据 memo = MemoService.get(id); if (memo == null) { addActionError(getMessageValue("global.data_not_exist")); // 该数据不存在! return ERROR; } // 更新数据 memo.setSubject(subject); memo.setContent(content); memo.setTime(Dates.getTimeMillis()); memoService.update(memo); return SUCCESS; } }
【Action配置】
打开jspgen-action.xml文件,在actions标签中增加一个action节点:
指定当前项目的action文件父级路径即可,内容如下:
<action space="/demo/" type="demo.action">
<!-- 如果采用模板路径自适应,以下内容可不用配置 -->
<result name="input">${space}/${action}_input.jsp</result>
<result name="list">${space}/${action}_list.jsp</result>
</action>
指定后列表访问地址为:/demo/memo!list.gen
9.2.6、模板嵌套
ui/demo/memo_list.jsp 列表模板:
<%@page trimDirectiveWhitespaces="true"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="gen" uri="http://help.jspgen.com/taglib/jstl"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>备忘录</title>
</head>
<body>
<div id="wrapper">
<div class="push">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left"><a href="memo!add.gen" class="button orange tiny add"><i></i>添加</a></td>
<td align="right"> </td>
</tr>
</table>
</div>
<div class="area">
<h2>
<i>备忘录</i>
</h2>
<div class="area-content">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table">
<tr>
<th width="5%" title="全选/取消"><input type="checkbox" name="check" for="ids" value="0"></th>
<th width="65%" class="border-left">主题</th>
<th width="15%" class="border-left">时间</th>
<th width="15%" class="border-left">操作</th>
</tr>
<gen:if test="${not empty pager.results}">
<c:forEach items="${pager.results}" var="map">
<tr class="border-top">
<td align="center"><input type="checkbox" name="ids" value="${map.id}"></td>
<td class="border-left"> <a href="memo!add.gen?id=${map.id}" class="edit" title="编辑">${gen:substring(map.subject, 80, '…')}</a></td>
<td align="center" class="border-left">${gen:dateTime(map.time, 'yyyy-MM-dd HH:mm:ss', config.timezoneid)}</td>
<td align="center" class="border-left"><a href="memo!add.gen?id=${map.id}" class="edit" title="编辑">编辑</a> | <a href="memo!delete.gen?id=${map.id}" class="delete">删除</a></td>
</tr>
</c:forEach>
<gen:else />
<tr class="border-top">
<td colspan="4" align="center" class="ignore">暂无数据</td>
</tr>
</gen:if>
<tr class="border-top">
<th colspan="4" class="pages">${pager.pageBar}</th>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
ui/demo/memo_input.jsp添加、修改、查看模板:
<%@page trimDirectiveWhitespaces="true"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="gen" uri="http://help.jspgen.com/taglib/jstl"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>备忘录</title>
<!-- 当前页面 -->
<link type="text/css" rel="stylesheet" href="${config.staticUrl}ui/form/form.css">
</head>
<body>
<div id="wrapper">
<div class="area">
<h2><i><gen:if test='${empty param.id}'>添加<gen:else />编辑/查看</gen:if></i></h2>
<div class="area-content">
<form id="form" name="form" method="post" action="<gen:if test='${empty param.id}'>memo!save.gen<gen:else />memo!update.gen</gen:if>">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table">
<tr>
<td width="30%" align="right"> 主题:</td>
<td>
<input name="subject" type="text" class="text small ignore" value="${memo.subject}" size="55" /></td>
</tr>
<tr class="border-top">
<td align="right" valign="top">内容:</td>
<td><table border="0" cellspacing="0" cellpadding="0">
<tr class="ignore">
<td><textarea id="content" name="content" class="text small textarea general">${memo.content}</textarea></td>
<td valign="top"><label for="content" generated="true" class="tip normal" tip=""></label></td>
</tr>
</table></td>
</tr>
<tr class="border-top">
<td colspan="2" class="push txt-c"><input type="hidden" name="formHash" value="${formHash}"/>
<input type="hidden" name="id" value="${memo.id}" />
<input name="submit" class="button blue small" type="submit" value="确 定" />
<input name="reset" class="button gray small" type="reset" value="取 消" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
9.2.7、运行、调式
项目发布上线时,不需要Java源码,也就是项目中的所有scr文件夹里的内容不用发布。
9.2.8、数据模型生成工具
一般设计好数据表后,可使用Fire自带的工具完成数据模型的所有基础文件生成工作,例:
// 测试 public static void main(String[] args) { String name = null; // 数据源 String dir = "D://bean"; // 生成文件保存目录 String packName = "demo"; // 包名 MySQLBean mb = new MySQLBean(); // mb.parseTable(name, packName, dir); // 所有表 // mb.parseTable(name, packName, new String[]{"id"}, dir); // 所有表 不处理id字段 // 单独某表 mb.parseTable(name, packName, "memo", "备忘录;", new String[]{"id"}, dir); }