说说项目背景吧,之所以会用到这个开源项目呢,是因为我们公司要做一个自己的门户网站。要求速度和质量。所以了在众多因素下我们选择了这个开源的项目。
https://git.oschina.net/sanluan/PublicCMS-lib
http://www.publiccms.com/
可以说这个项目的项目目录结构还是比较清晰的大概就是我们说的mvc模式了
1.首先将项目启动起来,进入这个页面。
2. 后台的一些东西。和创建模板的东西自己百度看看哇这里就不介绍了。基本就是拿到前端给的html页面后在后台配置一个基本就出来了。重点是需要进行二次开发的东西这里就重点讲一下了。并且配上代码。
3.我们讲一下如何提交表单数据到后台。和后台页面产生表格查看吧
这里我们需要提交询价信息。
4.代码实现
1.控制器
package com.publiccms.controller.admin.message;
import static com.sanluan.common.tools.JsonUtils.getString;
import static com.sanluan.common.tools.RequestUtils.getIpAddress;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.publiccms.common.base.AbstractController;
import com.publiccms.entities.log.LogOperate;
import com.publiccms.entities.message.EnquiryMessage;
import com.publiccms.entities.sys.SysSite;
import com.publiccms.logic.service.log.LogLoginService;
import com.publiccms.logic.service.message.EnquiryMessageService;
import com.publiccms.logic.service.sys.SysDomainService;
/**
*
* @ClassName: EnquiryAdminMessageController
* @Description:(项目询价)
* @author Sirius_KP
* @date 2017-3-8 下午5:41:33
*/
@Controller
@RequestMapping("enquiryMessage")
public class EnquiryAdminMessageController extends AbstractController {
@Autowired
private EnquiryMessageService service;
private String[] ignoreProperties = new String[] { "id" };
@RequestMapping("save")
public String save(EnquiryMessage entity, HttpServletRequest request, HttpSession session) {
try {
if (notEmpty(entity.getId())) {
entity = service.update(entity.getId(), entity, ignoreProperties);
} else {
entity.setCreate_time(new Date());
service.save(entity);
}
} catch (Exception e) {
e.printStackTrace();
return TEMPLATE_ERROR;
}
return TEMPLATE_DONE;
}
@SuppressWarnings("unchecked")
@RequestMapping("delete")
public String delete(Integer id, HttpServletRequest request, HttpSession session, ModelMap model) {
SysSite site = getSite(request);
if (verifyCustom("noright", !siteComponent.isMaster(site.getId()), model)) {
return TEMPLATE_ERROR;
}
EnquiryMessage entity = service.getEntity(id);
if (null != entity) {
service.delete(id);
//记录日志
Long userId = getAdminFromSession(session).getId();
Date now = getDate();
String ip = getIpAddress(request);
logOperateService.save(new LogOperate(site.getId(), userId, LogLoginService.CHANNEL_WEB_MANAGER, "delete.site", ip,
now, getString(entity)));
}
return TEMPLATE_DONE;
}
}
package com.publiccms.controller.web.message;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.publiccms.common.base.AbstractController;
import com.publiccms.entities.message.EnquiryMessage;
import com.publiccms.logic.service.message.EnquiryMessageService;
/**
*
* @ClassName: EnquiryMessageController
* @Description:(项目询价)
* @author Sirius_KP
* @date 2017-3-8 下午5:43:04
*/
@Controller
@RequestMapping("enquiryMessage")
public class EnquiryMessageController extends AbstractController {
@Autowired
private EnquiryMessageService service;
private String[] ignoreProperties = new String[] { "id" };
@RequestMapping("save")
@ResponseBody
public String save(EnquiryMessage entity, HttpServletRequest request, HttpSession session) {
try {
if (notEmpty(entity.getId())) {
entity = service.update(entity.getId(), entity, ignoreProperties);
} else {
entity.setCreate_time(new Date());
service.save(entity);
}
} catch (Exception e) {
return ERROR;
}
return SUCCESS;
}
}
2.实体
package com.publiccms.entities.message;
// Generated 2016-1-19 11:28:06 by Hibernate Tools 4.3.1
import static javax.persistence.GenerationType.IDENTITY;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sanluan.common.generator.annotation.GeneratorColumn;
/**
* 项目询价
* @ClassName: EnquiryMessage
* @Description:(EnquiryMessage的实体类)
* @author Sirius_KP
* @date 2017-3-3 上午10:25:43
*/
@Entity
@Table(name = "enquiryMessage")
public class EnquiryMessage 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 phone;
@GeneratorColumn(title = "电子邮件", condition = true, like = true)
private String email;
@GeneratorColumn(title = "需求描述", condition = true, like = true)
private String descriptions;
/**(状态)*/
@GeneratorColumn(title = "状态", condition = true, like = true)
private Integer status=0;
@GeneratorColumn(title = "询价时间", condition = true)
private Date create_time;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", length = 50)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "phone", length = 50)
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name = "email", length = 50)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "descriptions", length = 500)
public String getDescriptions() {
return descriptions;
}
public void setDescriptions(String descriptions) {
this.descriptions = descriptions;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_time", length = 19)
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
3.dao层代码
package com.publiccms.logic.dao.message;
import org.springframework.stereotype.Repository;
import com.publiccms.entities.message.EnquiryMessage;
import com.sanluan.common.base.BaseDao;
import com.sanluan.common.handler.PageHandler;
import com.sanluan.common.handler.QueryHandler;
@Repository
public class EnquiryMessageDao extends BaseDao {
@Override
protected EnquiryMessage init(EnquiryMessage entity) {
if (empty(entity.getCreate_time())) {
entity.setCreate_time(getDate());
}
return entity;
}
public PageHandler getPage(String name, Integer status, Integer pageIndex, Integer pageSize) {
QueryHandler queryHandler = getQueryHandler("from EnquiryMessage 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);
}
}
4.service层代码
package com.publiccms.logic.service.message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.publiccms.entities.message.EnquiryMessage;
import com.publiccms.logic.dao.message.EnquiryMessageDao;
import com.sanluan.common.base.BaseService;
import com.sanluan.common.handler.PageHandler;
/**
*
* @ClassName: EnquiryMessageService
* @Description:(询价服务)
* @author Sirius_KP
* @date 2017-3-8 下午5:45:47
*/
@Repository
public class EnquiryMessageService extends BaseService<EnquiryMessage> {
@Transactional(readOnly = true)
public PageHandler getPage( String name, Integer status, Integer pageIndex, Integer pageSize) {
return dao.getPage( name,status, pageIndex, pageSize);
}
@Autowired
private EnquiryMessageDao dao;
}
5.视图层代码
package com.publiccms.views.directive.message;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.logic.service.message.EnquiryMessageService;
import com.sanluan.common.handler.PageHandler;
import com.sanluan.common.handler.RenderHandler;
/**
*
* @ClassName: EnquiryMessageListDirective
* @Description:(询价)
* @author Sirius_KP
* @date 2017-3-8 下午5:46:59
*/
@Component
public class EnquiryMessageListDirective extends AbstractTemplateDirective {
@Override
public void execute(RenderHandler handler) throws IOException, Exception {
PageHandler page = service.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;
}
@Autowired
private EnquiryMessageService service;
}
package com.publiccms.views.directive.message;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.entities.message.EnquiryMessage;
import com.publiccms.logic.service.message.EnquiryMessageService;
import com.sanluan.common.handler.RenderHandler;
@Component
public class EnquiryMessageSeeDirective extends AbstractTemplateDirective {
@Override
public void execute(RenderHandler handler) throws IOException, Exception {
Integer id = handler.getInteger("id");
if (notEmpty(id)) {
EnquiryMessage entity = service.getEntity(id);
if (null != entity) {
handler.put("object", entity).render();
}
} else {
Integer[] ids = handler.getIntegerArray("ids");
if (notEmpty(ids)) {
List entityList = service.getEntitys(ids);
Map map = new LinkedHashMap();
for (EnquiryMessage entity : entityList) {
map.put(String.valueOf(entity.getId()), entity);
}
handler.put("map", map).render();
}
}
}
@Override
public boolean needAppToken() {
return true;
}
@Autowired
private EnquiryMessageService service;
}
6.模板
list__Enquiry.html
<@_enquiryMessageList name=name status=status pageIndex=pageNum count=numPerPage >
<div class="pageHeader">
<form id="pagerForm" onsubmit="return navTabSearch(this);" method="post">
<#include "../include_page/paramters.html"/>
<#include "../include_page/messageQuery.html"/>
form>
div>
<div class="pageContent">
<div class="panelBar">
<ul class="toolBar">
<li><a href="enquiryMessage/delete?id={sid}" title="确定要删除该条记录吗?" target="ajaxTodo"><i class="icon-trash icon-large">i> 删除a>li>
ul>
div>
<table style="table-layout: fixed" class="list" width="100%" layoutH="92">
<thead>
<tr>
<th width="30">IDth>
<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>${a.id}td>
<td>${a.name!}td>
<td>${a.phone!}td>
<td>${a.email!}td>
<td style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">${a.descriptions!}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.create_time!}td>
<td align="center">
<a href="/admin/message/add_Enquiry.html?id=${a.id}" target="navTab" rel="/admin/message/add_Enquiry">查看a>
td>
tr>
#list>
tbody>
table>
<#include "../include_page/page.html"/>
div>
@_enquiryMessageList>
add_Enquiry.html
<@_enquiryMessageSee id=id><#assign a=object/><#assign b=object.status!/>@_enquiryMessageSee>
<form method="post" action="/admin/enquiryMessage/save" 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">保存button>li>
<li><button type="button" class="button close">关闭button>li>
ul>
div>
<div class="pageFormContent" layoutH="56">
<dl class="nowrap">
<dt>用户名:dt>
<dd>
<input readonly="readonly" class="required" name="" type="text" maxlength="255" size="30" value="${(a.name)!}"/>
dd>
dl>
<dl class="nowrap">
<dt>联系电话:dt>
<dd>
<input readonly="readonly" class="required" name="" type="text" maxlength="255" size="30" value="${(a.phone)!}"/>
dd>
dl>
<dl class="nowrap">
<dt>电子邮件:dt>
<dd>
<input readonly="readonly" class="required" name="" 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>
<textarea readonly="readonly" rows="10" cols="">${(a.descriptions)!}textarea>
dd>
dl>
<dl class="nowrap">
<dt>询价时间:dt>
<dd>
<input readonly="readonly" class="required" name="" type="text" maxlength="255" size="30" value="${(a.create_time)!}"/>
dd>
dl>
div>
form>
messageQuery.html
class="searchBar">
class="searchContent">
-
type="text" size="20" name="name" value="${name!}" />
-
class="subBar">
-
好了代码就是上面的就可以实现二次开发的需求了
这时在浏览器上输入http://localhost:8080/admin/message/list__Enquiry看看
出现这个就是代码是正确的
接下来只需要将这个路径配置进后台,在后台添加一个菜单就可以完成这个功能了