跟我一起学extjs5(30--加入模块和菜单定义[3后台系统数据的组织和生成])
对于大多web程序来说,后台是完成控制和处理的,前台就是一个展示工具,这个系统也是这样。在上一节中建立了四个模块,下面开始设计前后台的交互。将系统信息和模块、菜单信息传到前台,由前台来进行展示。
首先新建一个java bean类用来存放各种需要传到前台的数据,里面包括:系统信息、操作人员信息、服务人员信息、模块信息、菜单。现在只加入了这几个,以后还要加入各种 各样的权限设置。在com.jfok.server.common中新增包info,在这下面加入四个class。
package com.jfok.server.common.info;
import java.io.Serializable;
@SuppressWarnings("serial")
// 这是服务单位情况的设置,也是放在数据库里的,可以进行修改
public class ServiceInfo implements Serializable {
private String tf_serviceDepartment;// 服务单位
private String tf_serviceMen;// 服务人员
private String tf_serviceTelnumber;// 联系电话
private String tf_serviceFaxnumber;// 传真
private String tf_serviceEmail;// 电子邮件
private String tf_serviceHomepage;// 主页
private String tf_serviceQQ;// QQ号
private String tf_copyrightOwner;// 版权所有单位
private String tf_copyrightInfo;// 版权信息
public ServiceInfo() {
}
}
package com.jfok.server.common.info;
import java.io.Serializable;
@SuppressWarnings("serial")
// 这是系统总体情况的设置,也是放在数据库里的,可以进行修改
public class SystemInfo implements Serializable {
private String tf_systemName; // 系统名称
private String tf_systemVersion; // 版本号
private String tf_systemAddition;// 附加设置
public SystemInfo() {
}
}
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
// 这是用户单位和登录用户的信息
public class UserInfo implements Serializable {
private String tf_userdwmc;// 使用单位名称
private Date tf_userStartdate;// 开始使用时间
private Integer tf_userId;// 用户id
private String tf_loginName;// 用户登录名
private String tf_userName;// 用户姓名
private String tf_departmentId = null;// 用户部门id
private String tf_departmentName = null;// 用户部门名称
public UserInfo() {
}
}
package com.jfok.server.common.info;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.jfok.server.hibernate.system._MenuGroup;
import com.jfok.server.hibernate.system._Module;
/**
* 用于向客户端返回系统的模块信息和登录人员的信息的类
*
* @author jfok
*
*/
@SuppressWarnings("serial")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ApplicationInfo implements Serializable {
// 这是系统总体情况的设置,也是放在数据库里的,可以进行修改
private SystemInfo systemInfo;
// 这是用户单位和登录用户的信息
private UserInfo userInfo;
// 这是服务单位情况的设置,也是放在数据库里的,可以进行修改
private ServiceInfo serviceInfo;
// 系统中模块的字义和菜单的定义
private Set<_Module> tf_Modules; // 系统模块定义信息
private List<_MenuGroup> tf_MenuGroups; // 系统菜单
// 系统中各种权限的定义
// 其他一些附加信息需要传送到前台的
private Integer tf_additionFileMaxMB; // 上传文件的最大大小
private String tf_previewExts; // 可预览的文件的后缀名 ,用逗号分开
public ApplicationInfo() {
}
}
以上类的getter和setter全部自己加一下。
继续在com.jfok.server.service中新增一个类用来生成数据的服务类 ApplicationService.java:
package com.jfok.server.service;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.jfok.server.DAO.SystemBaseDAO;
import com.jfok.server.common.info.ApplicationInfo;
import com.jfok.server.common.info.ServiceInfo;
import com.jfok.server.common.info.SystemInfo;
import com.jfok.server.common.info.UserInfo;
import com.jfok.server.hibernate.system._MenuGroup;
import com.jfok.server.hibernate.system._Module;
@Service
public class ApplicationService {
@Resource
private SystemBaseDAO systemBaseDAO;
// 事务注释
@SuppressWarnings("unchecked")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public ApplicationInfo getApplicationInfo(HttpServletRequest request) {
ApplicationInfo result = new ApplicationInfo();
// 以上内容暂时为自定义的,以后会改为从数据库和登录信息中读取。
SystemInfo systemInfo = new SystemInfo();
systemInfo.setTf_systemName("自定义的中小型管理系统");
systemInfo.setTf_systemVersion("2014.09.28");
result.setSystemInfo(systemInfo);
UserInfo userInfo = new UserInfo();
userInfo.setTf_userdwmc("无锡市宏宇电子有限公司");
userInfo.setTf_userStartdate(new Date());
userInfo.setTf_userName("管理员");
userInfo.setTf_loginName("admin");
userInfo.setTf_userId(0);
userInfo.setTf_departmentId("00");
userInfo.setTf_departmentName("工程部");
result.setUserInfo(userInfo);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setTf_serviceDepartment("熙旺公司");
serviceInfo.setTf_serviceMen("蒋锋");
serviceInfo.setTf_serviceTelnumber("1320528xxxx");
serviceInfo.setTf_serviceFaxnumber("0510-88888888");
serviceInfo.setTf_serviceQQ("7858xxxx");
serviceInfo.setTf_serviceEmail("jfok1972@qq.com");
serviceInfo.setTf_serviceHomepage("www.www.net");
serviceInfo.setTf_copyrightInfo("熙旺公司版权所有");
serviceInfo.setTf_copyrightOwner("熙旺软件");
result.setServiceInfo(serviceInfo);
// 把所有的模块定义信息加进去
result
.setTf_Modules(new HashSet<_Module>((List<_Module>) systemBaseDAO.findAll(_Module.class)));
// 加入菜单分组
result.setTf_MenuGroups((List<_MenuGroup>) systemBaseDAO.findAll(_MenuGroup.class));
for (_MenuGroup mg : result.getTf_MenuGroups()) {
// 加入这一条是为了让菜单组下面的菜单也执行sql 语句加进来,不然的话,返回以后mvc要加入菜单,
// 就会在执行sql的时候因为session已经关闭而报错
mg.getTf_menuModules().size();
}
return result;
}
}
在上面用到了DAO类,我自己做了一个系统的基本DAO类接品和类,放在包com.jfok.server.DAO之下。
package com.jfok.server.DAO;
import java.util.List;
@SuppressWarnings("rawtypes")
public interface ISystemBaseDAO {
public void save(Object record);
public void attachDirty(Object record, Object old);
public void delete(Object record);
public Object findById(Class<?> className, Object id);
public Object findById(String beanClassName, Object id);
public List findByProperty(Class<?> className, String propertyName,
Object value);
public Object findByPropertyFirst(Class<?> className, String propertyName,
Object value);
public List findByString(Class<?> className, String value);
public List findByProperty(String beanClassName, String propertyName,
Object value);
public List findByPropertyWithOtherCondition(Class<?> className, String propertyName,
Object value , String otherCondString);
public List findByLikeProperty(String beanClassName, String propertyName,
Object value);
public List findByLikePropertyWithOtherCondition(String beanClassName, String propertyName,
Object value, String otherCondString);
public List findByPropertyWithOtherCondition(String beanClassName, String propertyName,
Object value , String otherCondString);
public List findByPropertyAllSort(String beanClassName, String sort,
String dir, String propertyName, Object value, String defaultSort,
String defaultDir);
public List findAll(Class<?> className);
public List findAll(String className);
public List findAllSort(String beanClassName, String sort, String dir);
List findByPropertyAllSort(Class<?> className, String sort, String dir,
String propertyName, Object value, String defaultSort, String defaultDir);
}
package com.jfok.server.DAO;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
@SuppressWarnings("rawtypes")
public class SystemBaseDAO extends HibernateDaoSupport implements ISystemBaseDAO {
@Resource
private SessionFactory mySessionFactory;
public static SystemBaseDAO systemBaseDAO;
@PostConstruct
public void InjectedSessionFactory() {
//System.out.println("system base dao impl injected sessionFactory");
super.setSessionFactory(mySessionFactory);
systemBaseDAO = this;
}
public SystemBaseDAO() {
super();
//System.out.println("system base dao impl created");
String[] dateFormats = new String[] { "yyyy-MM-dd" };
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
//System.out.println("json tobean dataformats created");
}
private static final Log log = LogFactory.getLog(SystemBaseDAO.class);
@Override
public void save(Object record) {
getHibernateTemplate().save(record);
log.debug("new record saved:" + record.getClass().getSimpleName() + ":"
+ record.toString());
}
@Override
public void attachDirty(Object record, Object old) {
getHibernateTemplate().saveOrUpdate(record);
log.debug("save record:" + record.getClass().getSimpleName() + ":"
+ record.toString());
}
@Override
public void delete(Object record) {
getHibernateTemplate().delete(record);
log.debug("delete record:" + record.getClass().getSimpleName() + ":"
+ record.toString());
}
@Override
public Object findById(Class<?> className, Object id) {
return findById(className.getName(), id);
}
@Override
public Object findById(String beanClassName, Object id) {
Object record;
try {
record = getHibernateTemplate().get(beanClassName, Integer.parseInt(id.toString()));
} catch (Exception e) {
record = getHibernateTemplate().get(beanClassName, (Serializable) id);
}
// log.debug("get record " + beanClassName + " key:" + id + ":" + record);
return record;
}
@Override
public List<?> findAll(Class<?> className) {
return findAll(className.getName());
}
@Override
public List<?> findAll(String className) {
log.debug("find all:" + className);
String queryString = "from " + className;
return getHibernateTemplate().find(queryString);
}
public void setMySessionFactory(SessionFactory mySessionFactory) {
this.mySessionFactory = mySessionFactory;
}
@Override
public List<?> findAllSort(String beanClassName, String sort, String dir) {
log.debug("find all:" + beanClassName + "---sort:" + sort + "--" + dir);
String queryString;
if (sort == null || sort.length() == 0)
queryString = "from " + beanClassName + " as model ";
else
queryString = "from " + beanClassName + " as model " + " order by " + sort + " "
+ dir;
return getHibernateTemplate().find(queryString);
}
@Override
public List<?> findByPropertyAllSort(Class<?> className, String sort, String dir,
String propertyName, Object value, String defaultSort, String defaultDir) {
return findByPropertyAllSort(className.getName(), sort, dir, propertyName, value,
defaultSort, defaultDir);
}
@Override
public List<?> findByPropertyAllSort(String beanClassName, String sort, String dir,
String propertyName, Object value, String defaultSort, String defaultDir) {
log.debug("find all:" + beanClassName + "---sort:" + sort + "--" + dir);
if (propertyName.indexOf(".") > 0)
return findByPropertyCriteria(beanClassName, sort, dir, propertyName, value,
defaultSort, defaultDir);
String queryString;
String otherFilter = "";
if (sort == null || sort.length() == 0) {
if (defaultSort != null) {
sort = defaultSort;
dir = defaultDir;
}
}
if (sort == null || sort.length() == 0)
queryString = "from " + beanClassName + " as model where model." + propertyName
+ "= ? " + otherFilter;
else
queryString = "from " + beanClassName + " as model where model." + propertyName
+ "= ? " + otherFilter + " order by " + sort + " " + dir;
//System.out.println(queryString);
return getHibernateTemplate().find(queryString, value);
}
public List<?> findByPropertyCriteria(String beanClassName, String sort, String dir,
String propertyName, Object value, String defaultSort, String defaultDir) {
Session session = getSessionFactory().openSession();
Criteria criteria = session.createCriteria(beanClassName);
String[] props = propertyName.split("\\.");
Criteria subCriteria = criteria.createCriteria(props[0]);
subCriteria.add(Restrictions.eq(props[1], value));
if (sort != null) {
if (dir == null || !dir.toLowerCase().equals("desc"))
criteria.addOrder(Order.asc(sort));
else
criteria.addOrder(Order.desc(sort));
} else if (defaultSort != null) {
if (defaultDir == null || !defaultDir.toLowerCase().equals("desc"))
criteria.addOrder(Order.asc(defaultSort));
else
criteria.addOrder(Order.desc(defaultSort));
}
List<?> result = criteria.list();
session.close();
return result;
}
@Override
public Object findByPropertyFirst(Class<?> className, String propertyName, Object value) {
List<?> result = findByProperty(className, propertyName, value);
if (result.size() == 0)
return null;
else
return result.get(0);
}
// @Override
public Object findByPropertyFirstWithOtherCondition(Class<?> className,
String propertyName, Object value, String otherCondString) {
List<?> result = findByPropertyWithOtherCondition(className, propertyName, value,
otherCondString);
if (result.size() == 0)
return null;
else
return result.get(0);
}
@Override
public List<?> findByProperty(Class<?> className, String propertyName, Object value) {
return findByPropertyWithOtherCondition(className.getSimpleName(), propertyName,
value, null);
}
@Override
public List<?> findByProperty(String beanClassName, String propertyName, Object value) {
return findByPropertyWithOtherCondition(beanClassName, propertyName, value, null);
}
@Override
public List<?> findByPropertyWithOtherCondition(Class<?> className,
String propertyName, Object value, String otherCondString) {
return findByPropertyWithOtherCondition(className.getSimpleName(), propertyName,
value, otherCondString);
}
@SuppressWarnings("unchecked")
@Override
public List<?> findByPropertyWithOtherCondition(String beanClassName,
String propertyName, Object value, String otherCondString) {
String queryString = "from " + beanClassName + " as model where model."
+ propertyName + "= ?";
if (otherCondString != null && otherCondString.length() > 1) {
queryString = queryString + " and (" + otherCondString + ")";
}
List<Object> result = getHibernateTemplate().find(queryString, value);
log.debug(String.format("finding %s with property:%s value: %s : record number:%d",
beanClassName, propertyName, value, result.size()));
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<?> findByString(Class<?> className, String value) {
String queryString = "from " + className.getSimpleName() + " as model where " + value;
List<Object> result = getHibernateTemplate().find(queryString);
log.debug(String.format("finding %s with string:%s : record number:%d",
className.getSimpleName(), value, result.size()));
return result;
}
@Override
public List findByLikeProperty(String beanClassName, String propertyName, Object value) {
return findByLikePropertyWithOtherCondition(beanClassName, propertyName, value, "");
}
@Override
public List findByLikePropertyWithOtherCondition(String beanClassName,
String propertyName, Object value, String otherCondString) {
String queryString = "from " + beanClassName + " as model where model."
+ propertyName + " like ? ";
if (otherCondString != null && otherCondString.length() > 1) {
queryString = queryString + " and (" + otherCondString + ")";
}
List<?> result = getHibernateTemplate().find(queryString, value);
log.debug(String.format(
"finding %s with like property:%s value: %s : record number:%d", beanClassName,
propertyName, value, result.size()));
return result;
}
}
最后加入spring MVC的控制类,在com.jfok.server.controller中新增一个类ApplicationController.java:
package com.jfok.server.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jfok.server.common.info.ApplicationInfo;
import com.jfok.server.service.ApplicationService;
@Controller
public class ApplicationController {
// spring注释,自动注入ApplicationService 的实例
@Resource
private ApplicationService applicationService;
@RequestMapping("/applicationinfo.do")
public synchronized @ResponseBody
ApplicationInfo getApplicationInfo(HttpServletRequest request) {
return applicationService.getApplicationInfo(request);
}
}
加好这几个文件后的图示为: