需求分析
我们需要把页面上的用户提交的表单放到数据库中,然后在内容管理页面呈现出来
如:
开始模仿
还是以mvc分层结构来写
Controller层:
package com.publiccms.controller.admin.message;
import com.publiccms.common.annotation.Csrf;
import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.common.constants.CommonConstants;
import com.publiccms.common.handler.RenderHandler;
import com.publiccms.common.tools.*;
import com.publiccms.controller.admin.cms.CmsContentAdminController;
import com.publiccms.entities.cms.CmsCategory;
import com.publiccms.entities.cms.CmsContent;
import com.publiccms.entities.cms.CmsContentAttribute;
import com.publiccms.entities.log.LogLogin;
import com.publiccms.entities.log.LogOperate;
import com.publiccms.entities.message.VolunteersMessage;
import com.publiccms.entities.sys.SysSite;
import com.publiccms.entities.sys.SysUser;
import com.publiccms.logic.component.site.SiteComponent;
import com.publiccms.logic.service.log.LogLoginService;
import com.publiccms.logic.service.log.LogOperateService;
import com.publiccms.logic.service.message.VolunteersMessageService;
import com.publiccms.views.directive.cms.CmsContentListDirective;
import com.publiccms.views.pojo.model.CmsContentParameters;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import static com.publiccms.common.tools.CommonUtils.getDate;
import static com.publiccms.common.tools.CommonUtils.notEmpty;
import static com.publiccms.common.tools.RequestUtils.getIpAddress;
/**
* @Description:(志愿者信息)
* @author qyq
* @date 2021-04-19 18:37
*/
@Controller
@RequestMapping("volunteersMessage")
public class VolunteersMessageController extends CmsContentListDirective {
@Autowired
private VolunteersMessageService volunteersMessageService;
@Autowired
protected SiteComponent siteComponent;
@Autowired
protected LogOperateService logOperateService;
private String[] ignoreProperties = new String[]{"id"};
// @RequestMapping("save")
// public String save(@RequestAttribute SysSite site, @SessionAttribute SysUser admin, VolunteersMessage entity, HttpServletRequest request, HttpSession session) {
// try{
// if (notEmpty(entity.getId())) {
// entity = volunteersMessageService.update(entity.getId(), entity, ignoreProperties);
//
// if (null != entity) {
// logOperateService.save(new LogOperate(site.getId(), admin.getId(), LogLoginService.CHANNEL_WEB_MANAGER,
// "update.volunteersMessage", RequestUtils.getIpAddress(request), CommonUtils.getDate(), JsonUtils.getString(entity)));
// }
// }else {
entity.setSubmitTime(new Date());
volunteersMessageService.update(entity.getId(),entity);
// volunteersMessageService.save(entity);
// logOperateService.save(new LogOperate(site.getId(), admin.getId(), LogLoginService.CHANNEL_WEB_MANAGER, "save.volunteersMessage",
// RequestUtils.getIpAddress(request), CommonUtils.getDate(), JsonUtils.getString(entity)));
// }
// }catch (Exception e) {
// e.printStackTrace();
// return CommonConstants.TEMPLATE_ERROR;
// }
// return CommonConstants.TEMPLATE_DONE;
// }
// @SuppressWarnings("unchecked")
// @RequestMapping("delete")
// @Csrf
// public String delete(@RequestAttribute SysSite site, @SessionAttribute SysUser admin, Long[] ids, HttpServletRequest request,
// ModelMap modelMap) {
// if (ControllerUtils.verifyCustom("noright", !siteComponent.isMaster(site.getId()),modelMap)) {
// return CommonConstants.TEMPLATE_ERROR;
// }
// if (CommonUtils.notEmpty(ids)) {
// Set<Integer> categoryIdSet = new HashSet<>();
// for (CmsContent entity : volunteersMessageService.delete(site.getId(), admin, ids)) {
// categoryIdSet.add(entity.getCategoryId());
// if (entity.isHasStatic()) {
// String filePath = siteComponent.getWebFilePath(site, entity.getUrl());
// if (CmsFileUtils.exists(filePath)) {
// String backupFilePath = siteComponent.getWebBackupFilePath(site, filePath);
// CmsFileUtils.moveFile(filePath, backupFilePath);
// }
// }
// }
// if (!categoryIdSet.isEmpty()) {
// Integer[] categoryIds = categoryIdSet.toArray(new Integer[categoryIdSet.size()]);
// for (CmsCategory entity : categoryService.getEntitys(categoryIds)) {
// templateComponent.createCategoryFile(site, entity, null, null);
// }
// }
// logOperateService.save(new LogOperate(site.getId(), admin.getId(), LogLoginService.CHANNEL_WEB_MANAGER,
// "delete.content", RequestUtils.getIpAddress(request), CommonUtils.getDate(),
// StringUtils.join(ids, CommonConstants.COMMA)));
// }
// return CommonConstants.TEMPLATE_DONE;
// }
@SuppressWarnings("unchecked")
@RequestMapping("delete")
@Csrf
public String delete(@RequestAttribute SysSite site, @SessionAttribute SysUser admin, Integer[] ids, HttpServletRequest request,
HttpSession session, ModelMap modelMap) {
if (ControllerUtils.verifyCustom("noright", !siteComponent.isMaster(site.getId()),modelMap)) {
return CommonConstants.TEMPLATE_ERROR;
}
if (CommonUtils.notEmpty(ids)) {
List<VolunteersMessage> entitys = volunteersMessageService.getEntitys(ids);
if (null != entitys) {
volunteersMessageService.delete(ids);
//记录日志
// Long userId = getAdminFromSession(session).getId();
Date now = getDate();
String ip = getIpAddress(request);
logOperateService.save(new LogOperate(site.getId(), admin.getId(), LogLoginService.CHANNEL_WEB_MANAGER, "delete.site",
ip, now, JsonUtils.getString(entitys)));
}
}
return CommonConstants.TEMPLATE_DONE;
}
@RequestMapping(value = "insert", method = RequestMethod.POST)
@CrossOrigin
public String insert(@RequestAttribute SysSite site, String name, String uuid, String education, String email,
HttpServletRequest request, HttpSession session, HttpServletResponse response, ModelMap model) {
try {
name = StringUtils.trim(name);
uuid = StringUtils.trim(uuid);
education = StringUtils.trim(education);
email = StringUtils.trim(email);
Date date = new Date();
VolunteersMessage entity = new VolunteersMessage(name,uuid,education,email,0,date);
volunteersMessageService.save(entity);
//记录日志
String ip = getIpAddress(request);
logOperateService.save(new LogOperate(site.getId(), entity.getId().longValue(), LogLoginService.CHANNEL_WEB_MANAGER, "volunteersMessage save",
ip, date, JsonUtils.getString(entity)));
return CommonConstants.SUCCESS;
}catch (Exception e) {
e.printStackTrace();
}
return CommonConstants.ERROR;
}
}
package com.publiccms.controller.web.message;
import com.publiccms.common.constants.CommonConstants;
import com.publiccms.common.tools.CommonUtils;
import com.publiccms.common.tools.ControllerUtils;
import com.publiccms.common.tools.JsonUtils;
import com.publiccms.common.tools.RequestUtils;
import com.publiccms.entities.cms.*;
import com.publiccms.entities.log.LogOperate;
import com.publiccms.entities.message.VolunteersMessage;
import com.publiccms.entities.sys.SysDept;
import com.publiccms.entities.sys.SysDeptCategoryId;
import com.publiccms.entities.sys.SysSite;
import com.publiccms.entities.sys.SysUser;
import com.publiccms.logic.component.template.ModelComponent;
import com.publiccms.logic.component.template.TemplateComponent;
import com.publiccms.logic.service.cms.CmsCategoryModelService;
import com.publiccms.logic.service.cms.CmsCategoryService;
import com.publiccms.logic.service.cms.CmsContentAttributeService;
import com.publiccms.logic.service.cms.CmsContentService;
import com.publiccms.logic.service.log.LogLoginService;
import com.publiccms.logic.service.log.LogOperateService;
import com.publiccms.logic.service.message.VolunteersMessageService;
import com.publiccms.logic.service.sys.SysDeptCategoryService;
import com.publiccms.logic.service.sys.SysDeptService;
import com.publiccms.views.pojo.entities.CmsModel;
import com.publiccms.views.pojo.entities.ExtendData;
import com.publiccms.views.pojo.model.CmsContentParameters;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static com.publiccms.common.constants.CommonConstants.ERROR;
import static com.publiccms.common.constants.CommonConstants.SUCCESS;
import static com.publiccms.common.tools.CommonUtils.notEmpty;
import static com.publiccms.controller.admin.cms.CmsContentAdminController.initContent;
/**
注;引入的无用对象,是因为此版本为旧案,无需在意
* @Description:(志愿者信息)
* @author qyq
* @date 2021-04-19 20:17
*/
@Component
@Controller
@RequestMapping("volunteersMessage")
public class VolunteersMessageController extends AbstractController {
@Autowired
private VolunteersMessageService volunteersMessageService;
@Autowired
protected LogOperateService logOperateService;
@Autowired
private CmsContentService service;
@Autowired
private CmsContentAttributeService cmsContentAttributeService;
@Autowired
private ModelComponent modelComponent;
@Autowired
private SysDeptService sysDeptService;
@Autowired
private CmsCategoryModelService categoryModelService;
@Autowired
private TemplateComponent templateComponent;
@Autowired
private CmsCategoryService categoryService;
@Autowired
private SysDeptCategoryService sysDeptCategoryService;
private String[] ignoreProperties = new String[]{"id"};
@RequestMapping("save")
@ResponseBody
public String save(VolunteersMessage entity, HttpServletRequest request, HttpSession session) {
try {
if (notEmpty(entity.getId())) {
entity = volunteersMessageService.update(entity.getId(), entity, ignoreProperties);
} else {
entity.setSubmitTime(new Date());
volunteersMessageService.save(entity);
}
} catch (Exception e) {
return ERROR;
}
return SUCCESS;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
return null;
}
}
POJO类
package com.publiccms.entities.message;
import com.publiccms.common.database.CmsUpgrader;
import com.publiccms.common.generator.annotation.GeneratorColumn;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.exception.DataException;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
/**
* @author qyq
* @date 2021-04-19 18:53
*/
@Entity
@Table(name = "volunteersMessage")
@DynamicUpdate
public class VolunteersMessage implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@GeneratorColumn(title = "ID")
private Integer id;
@GeneratorColumn(title = "姓名", condition = true, like = true)
private String name;
@GeneratorColumn(title = "证件号码", condition = true, like = true)
private String uuid;
@GeneratorColumn(title = "学历", condition = true, like = true)
private String education;
@GeneratorColumn(title = "邮件", condition = true, like = true)
private String email;
/**(状态)*/
@GeneratorColumn(title = "状态", condition = true, like = true)
private Integer status = 0;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@GeneratorColumn(title = "提交时间", condition = true)
private Date submitTime;
@Id
@GeneratedValue(generator = "cmsGenerator")
@GenericGenerator(name = "cmsGenerator", strategy = CmsUpgrader.IDENTIFIER_GENERATOR)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", length = 50)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "uuid", length = 50)
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
@Column(name = "education", length = 50)
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
@Column(name = "email", length = 50)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "submit_time", length = 19)
public Date getSubmitTime() {
return submitTime;
}
public void setSubmitTime(Date submitTime) {
this.submitTime = submitTime;
}
public VolunteersMessage() {
}
public VolunteersMessage( String name, String uuid, String education, String email, Integer status, Date submitTime) {
this.name = name;
this.uuid = uuid;
this.education = education;
this.email = email;
this.status = status;
this.submitTime = submitTime;
}
}
service层
package com.publiccms.logic.service.message;
import com.publiccms.common.base.BaseService;
import com.publiccms.common.handler.PageHandler;
import com.publiccms.entities.message.VolunteersMessage;
import com.publiccms.logic.dao.message.VolunteersMessageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
* @Description:(志愿者信息)
* @author qyq
* @date 2021-04-19 20:27
*/
@Repository
public class VolunteersMessageService extends BaseService<VolunteersMessage> {
@Autowired
private VolunteersMessageDao volunteersMessageDao;
@Transactional(readOnly = true)
public PageHandler getPage(String name, Integer status, Integer pageIndex, Integer pageSize) {
return volunteersMessageDao.getPage(name,status,pageIndex,pageSize);
}
}
dao层
package com.publiccms.logic.dao.message;
import com.publiccms.common.base.BaseDao;
import com.publiccms.common.handler.PageHandler;
import com.publiccms.common.handler.QueryHandler;
import com.publiccms.common.tools.CommonUtils;
import com.publiccms.entities.message.VolunteersMessage;
import org.springframework.stereotype.Repository;
import java.text.SimpleDateFormat;
import static com.publiccms.common.tools.CommonUtils.getDate;
import static com.publiccms.common.tools.CommonUtils.notEmpty;
/**
* @Description:(志愿者信息)
* @author qyq
* @date 2021-04-19 19:59
*/
@Repository
public class VolunteersMessageDao extends BaseDao<VolunteersMessage> {
@Override
protected VolunteersMessage init(VolunteersMessage entity) {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (CommonUtils.empty(String.valueOf(entity.getSubmitTime()))) {
entity.setSubmitTime(getDate());
}
return entity;
}
public PageHandler getPage(String name, Integer status, Integer pageIndex, Integer pageSize) {
QueryHandler queryHandler = getQueryHandler("from VolunteersMessage bean");
if (notEmpty(name)) {
queryHandler.condition("(bean.name like :name)").setParameter("name", like(name));
}
if (notEmpty(status)) {
queryHandler.condition("(bean.status = :status)").setParameter("status", status);
}
queryHandler.order("bean.id desc");
return getPage(queryHandler, pageIndex, pageSize);
}
}
view层
package com.publiccms.views.directive.message;
import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.common.handler.PageHandler;
import com.publiccms.common.handler.RenderHandler;
import com.publiccms.logic.service.message.VolunteersMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author qyq
* @date 2021-04-20 11:06
*/
@Component
public class VolunteersMessageListDirective extends AbstractTemplateDirective {
@Autowired
private VolunteersMessageService volunteersMessageService;
@Override
public void execute(RenderHandler handler) throws IOException, Exception {
PageHandler page = volunteersMessageService.getPage(handler.getString("name"), handler.getInteger("status"),
handler.getInteger("pageIndex", 1), handler.getInteger("count", 30));
handler.put("page", page).render();
}
@Override
public boolean needAppToken() {
return true;
}
}
package com.publiccms.views.directive.message;
import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.common.handler.RenderHandler;
import com.publiccms.common.tools.CommonUtils;
import com.publiccms.entities.message.VolunteersMessage;
import com.publiccms.logic.service.message.VolunteersMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author qyq
* @date 2021-04-20 11:12
*/
@Component
public class VolunteersMessageSeeDirective extends AbstractTemplateDirective {
@Autowired
private VolunteersMessageService volunteersMessageService;
@Override
public void execute(RenderHandler handler) throws IOException, Exception {
Integer id = handler.getInteger("id");
if (CommonUtils.notEmpty(id)) {
VolunteersMessage entity = volunteersMessageService.getEntity(id);
if (null != entity) {
handler.put("object", entity).render();
}
} else {
Integer[] ids = handler.getIntegerArray("ids");
if (CommonUtils.notEmpty(ids)) {
List<VolunteersMessage> entityList = volunteersMessageService.getEntitys(ids);
Map<String, VolunteersMessage> map = new LinkedHashMap<String, VolunteersMessage>();
for (VolunteersMessage entity : entityList) {
map.put(String.valueOf(entity.getId()), entity);
}
handler.put("map", map).render();
}
}
}
@Override
public boolean needAppToken() {
return true;
}
}
如此逻辑代码已经完成!
内容管理展示模板
list_Enquiry:
<@_volunteersMessageSee id=id>
<#assign a=object/>
<#assign b=object.status!/>
</@_volunteersMessageSee>
<form method="post" autocomplete="off" action="volunteersMessage/save.do?callbackType=closeCurrent&navTabId=message/list&rel=page" class="pageForm required-validate" onsubmit="return validateCallback(this, navTabAjaxDone);">
<input name="id" type="hidden" value="${(a.id)!}" />
<div class="formBar">
<ul>
<li><button type="submit" class="buttonActive"><@t.page 'button.save'/></button></li>
<li><button type="button" class="button close"><@t.page 'button.close'/></button></li>
</ul>
</div>
<div class="pageFormContent" layoutH="56">
<dl class="nowrap">
<dt>姓名:</dt>
<dd>
<input readonly="readonly" class="required" name="name" type="text" maxlength="255" size="30" value="${(a.name)!}"/>
</dd>
</dl>
<dl class="nowrap">
<dt>证件号码:</dt>
<dd>
<input readonly="readonly" class="required" name="uuid" type="text" maxlength="255" size="30" value="${(a.uuid)!}"/>
</dd>
</dl>
<dl class="nowrap">
<dt>学历:</dt>
<dd>
<input readonly="readonly" class="required" name="education" type="text" maxlength="255" size="30" value="${(a.education)!}"/>
</dd>
</dl>
<dl class="nowrap">
<dt>电子邮件:</dt>
<dd>
<input readonly="readonly" class="required" name="email" type="text" maxlength="255" size="30" value="${(a.email)!}"/>
</dd>
</dl>
<dl class="nowrap">
<dt>状态:</dt>
<dd>
<select name="status">
<#if b==1>
<option selected value="1">已读</option>
<option value="0">未读</option>
<#elseif b==0>
<option value="1">已读</option>
<option selected value="0">未读</option>
<#else>
</#if>
</select>
</dd>
</dl>
<dl class="nowrap">
<dt>提交时间:</dt>
<dd>
<input readonly="readonly" class="required" name="submitTime" type="text" maxlength="255" size="30" value="${(a.submitTime)!}"/>
</dd>
</dl>
</div>
</form>
add_Enquiry.html:
<@_volunteersMessageList advanced=true name=name status=status pageIndex=pageNum count=numPerPage >
<div class="pageHeader">
<form id="pagerForm" autocomplete="off" action="list_Enquiry.html" onsubmit="return navTabSearch(this);" method="post">
<#include "../include_page/parameters.html"/>
<#include "./messageQuery.html"/>
</form>
</div>
<div class="pageContent">
<div class="panelBar">
<ul class="toolBar">
<li><a href="volunteersMessage/delete.do?_csrf=<@_csrfToken admin=true/>" title="<@t.page 'confirm.batch_delete'/>" target="selectedTodo" rel="ids"><i class="icon-trash icon-large"></i> <@t.page 'button.batch_delete'/></a></li>
</ul>
</div>
<table style="table-layout: fixed" class="list" width="100%" layoutH="92">
<thead>
<tr>
<th width="20"><input type="checkbox" group="ids" class="checkboxCtrl"></th>
<th width="30">ID</th>
<th>姓名</th>
<th>证件号码</th>
<th>学历</th>
<th>电子邮件</th>
<th>是否已读</th>
<th>提交时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<#list page.list as a>
<tr target="sid" rel="${a.id}">
<td><input name="ids" value="${a.id}" type="checkbox"></td>
<td>${a.id}</td>
<td>${a.name!}</td>
<td>${a.uuid!}</td>
<td>${a.education!}</td>
<td>${a.email!}</td>
<#if a.status==1>
<td><span style="color: green;">已读</span></td>
<#elseif a.status==0>
<td><span style="color: red;">未读</span></td>
<#else>
</#if>
<td>${a.submitTime!}</td>
<td align="center">
<a href="/publiccms/admin/message/add_Enquiry.html?id=${a.id}" target="navTab" rel="/publiccms/admin/message/add_Enquiry">查看</a>
</td>
</tr>
</#list>
</tbody>
</table>
<#include "../include_page/page.html"/>
</div>
</@_volunteersMessageList>
messageQuery.html:
<div class="searchBar">
<ul class="searchContent">
<li>
<!--<label>名称:</label>-->
<label><@t.page 'name'/>:</label>
<input type="text" size="20" name="name" value="${name!}" />
</li>
<li>
<label>查看状态:</label>
<select class="combox" name="status">
<option value=""${(!status?has_content)?then(' selected="true"','')?no_esc}>不限</option>
<option value="1"${(status?has_content&&"1"==status)?then(' selected="true"','')?no_esc}>已读</option>
<option value="0"${(status?has_content&&"0"==status)?then(' selected="true"','')?no_esc}>未读</option>
</select>
</li>
</ul>
<div class="subBar">
<ul>
<li>
<button type="submit" class="buttonActive"><@t.page 'button.search'/></button>
</li>
</ul>
</div>
</div>
好了代码就是上面的就可以实现二次开发的需求了
这时在浏览器上输入链接: http://localhost:8080/publiccms/admin/message/list_Enquiry.看看
出现这个就是代码是正确的
接下来只需要将这个路径配置进后台,在后台添加一个菜单就可以完成这个功能了
子模块管理:
子模块管理:
至于前端就不是我的熟悉范围了,我只弄了个表单: