javaee之SSH项目

通过学习SSH的框架,便做了一个小项目,是一个简单的CRM项目,包括的功能主要有用户的crud和部门的crud
在部门中又有对用户的多对一的关联关系,还包括了一个系统日志的功能,即为当哪个用户做了什么功能都能够在系统日志中
显示出来。


这次的项目也还是分为3层架构去实现,DAO层、SERVICE层、ACTION层
大致的分包情况:



首先是SSH的配置文件:
Struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<!-- 配置为开发模式 -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple"></constant>
    
	<package name="default" namespace="/" extends="struts-default">
	
		<!-- 拦截器 -->
		<interceptors>
			<interceptor name="myinterceptor" class="gz.itcast.crm.inter.Interecptor">
				<param name="excludeMethods">login</param>
			</interceptor>
			<interceptor-stack name="mystack">
				<interceptor-ref name="myinterceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
	
		<!-- 配置全局的拦截器 -->
		<default-interceptor-ref name="mystack"></default-interceptor-ref>
	
	
		<!-- 系统主页 -->
		<action name="main_*" class="mainAction" method="{1}">
			<result>/WEB-INF/views/main.jsp</result>
			<result name="{1}">/WEB-INF/views/{1}.jsp</result>
			<result name="input">/WEB-INF/login.jsp</result>
		</action>
	
		<!-- 用户模块 -->
		<action name="user_*" class="userAction" method="{1}">
			<result>/WEB-INF/views/user/list.jsp</result>
			<result name="input">/WEB-INF/views/user/input.jsp</result>
			<result name="save" type="chain">user_pageList</result>
		</action>
		
		<!-- 部门模块 -->
		<action name="dept_*" class="deptAction" method="{1}">
			<result>/WEB-INF/views/dept/list.jsp</result>
			<result name="input">/WEB-INF/views/dept/input.jsp</result>
			<result name="save" type="redirectAction">dept_pageList</result>
		</action>
		
		<!-- 系统模块 -->
		<action name="system_*" class="systemLogAction" method="{1}">
			<result>/WEB-INF/views/sys/list.jsp</result>
		</action>
		
	</package>
</struts>


hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	
<hibernate-configuration>
<session-factory>
	<!-- hibernate对数据库的连接参数,注册的驱动,url,用户名和密码 
		
		<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
		</property>
		<property name="hibernate.connection.url">
		jdbc:mysql://localhost:3306/shopsys
		</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123</property>
		
		hibernate对不同的数据库用不同的数据库的方言,hibernate能够为各个数据库进行一个orm映射
		主要是因为拥有了不同的数据库方言支持
	-->
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQL5InnoDBDialect
	</property>


	<!-- hibernate的其他配置 -->
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>




	<!-- hibernate中对实体映射文件的加载,需要在hibernate中使用映射就要把映射文件加上 -->
	<mapping resource="gz/itcast/crm/entity/User.hbm.xml" />
	<mapping resource="gz/itcast/crm/entity/Admins.hbm.xml" />
	<mapping resource="gz/itcast/crm/entity/Dept.hbm.xml" />
	<mapping resource="gz/itcast/crm/entity/SystemLog.hbm.xml" />




</session-factory>


</hibernate-configuration>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
        
        <!-- 导入数据库的properties文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        
	<!-- 配置数据库连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${c3p0jdbcurl}"></property>
		<property name="driverClass" value="${c3p0driver}"></property>
		<property name="user" value="${c3p0user}"></property>
		<property name="password" value="${c3p0password}"></property>
		<property name="initialPoolSize" value="${c3p0minpool}"></property>
		<property name="maxPoolSize" value="${c3p0maxpool}"></property>
	</bean>
	
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>


	<!-- 配置HibernateTemplate -->
	<bean id="htID" class="org.springframework.orm.hibernate4.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 事务管理的配置 -->
	<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 采用切面来定义事务 -->
	<tx:advice id="txID" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
			<tx:method name="get*" read-only="true"  propagation="SUPPORTS"/>
			<tx:method name="load*" read-only="true" propagation="SUPPORTS"/>
			<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
		</tx:attributes>
	</tx:advice>
	
	
	<!-- 事务的切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* gz.itcast.crm.service.impl.*.*(..))" id="txpt"/>
		<aop:advisor advice-ref="txID" pointcut-ref="txpt"/>
	</aop:config>
	
	
	
	
	<!-- 加入其它applicationContext.xml的配置文件 -->
	<import resource="classpath:applicationContext-*.xml"/>
	
	<bean id="systemlog" class="gz.itcast.crm.util.SystemLogUtil">
		<property name="systemLogService" ref="systemLogService"></property>
	</bean>
	
	<aop:config>
		<aop:aspect ref="systemlog">
			<aop:pointcut expression="execution(* gz.itcast.crm.service.impl.*.*(..))" id="syspt"/>
			<aop:before method="log" pointcut-ref="syspt"/>
		</aop:aspect>
	</aop:config>
</beans>


然后就是实体类(USER、ADMINS、DEPT、SYSTEMLOG):
use.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="gz.itcast.crm.entity">
    	<class name="User" table="t_user">
    		<id name="id">
    			<generator class="native"></generator>
    		</id>
    		
    		<property name="name"></property>
    		<property name="password"></property>
    		<property name="realName"></property>
    		<property name="phone"></property>
    		<property name="email"></property>
    		<property name="inputTime"></property>
    		
    	</class>
</hibernate-mapping>

dept.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="gz.itcast.crm.entity">
    	<class name="Dept" table="t_dept">
    		<id name="id">
    			<generator class="native"></generator>
    		</id>
    		
    		<property name="deptName" />
    		<property name="descr" />
    		
    		<!-- leader属性,表示本类与User的多对一的关系 -->
    		<many-to-one name="leader" class="User" column="uid" />
    		
    		<property name="inputTime" type="timestamp" />
    	</class>
</hibernate-mapping>



dao层,dao层采用了一个父类来统一规范,让所有的dao都来继承父类即可,BASEDAO:
baseDao:
	package gz.itcast.crm.dao.base;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;


public abstract class BaseDaoImpl<T> implements IBaseDao<T> {


	private HibernateTemplate ht;
	public void setHt(HibernateTemplate ht) {
		this.ht = ht;
	}


	Class targerClass;
	public BaseDaoImpl(){
		Class clazz = this.getClass();
		Type type = clazz.getGenericSuperclass();
		ParameterizedType pt = (ParameterizedType)type;
		targerClass = (Class) pt.getActualTypeArguments()[0];
	}
	
	
	
	public List<T> queryAll() {
		return ht.loadAll(targerClass);
	}


	public void save(T t) {
		ht.saveOrUpdate(t);
	}


	public List<T> queryByHQL(String hql, Object... values) {
		return (List<T>) ht.find(hql, values);
	}


	public T get(int id) {
		return (T) ht.get(targerClass, id);
	}


	public void delete(int id) {
		T t  = get(id);
		ht.delete(t);
	}


	public List<T> queryByPage(final String condition,final  int curPage,final int pageSize,
			final Object... values) {
		return ht.execute(new HibernateCallback<List<T>>() {


			public List<T> doInHibernate(Session session) throws HibernateException {
				Query query = session.createQuery("from "
						+ targerClass.getSimpleName() + " o where 1=1 "
						+ condition);
				if(values !=null){
					for(int i=0;i<values.length;i++){
						query.setParameter(i, values[i]);
					}
				}
				
				query.setFirstResult((curPage-1)*pageSize);
				query.setMaxResults(pageSize);
				
				return query.list();
			}
		
		
		});
	}


	public int queryByCount(final String condition,final  Object... values) {
		return ht.execute(new HibernateCallback<Long>() {


			public Long doInHibernate(Session session) throws HibernateException {
				Query query = session.createQuery("select count(o) from "+
							targerClass.getSimpleName() + " o where 1=1 " + condition
						);
				// 赋值
				if (values != null) {
					for (int i = 0; i < values.length; i++) {
						query.setParameter(i, values[i]);
					}
				}
				return (Long) query.uniqueResult();
			}
		
		}).intValue();
	}


}




采用了父类的好处就是全部dao层的方法,大多数都能够交给baseDao来完成,而相对的dao层可以简洁




service层:
UserService:
package gz.itcast.crm.service.impl;


import gz.itcast.crm.dao.IUserDao;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.UserQuery;


import java.util.ArrayList;
import java.util.List;


import org.springframework.util.StringUtils;


public class UserServiceImpl implements IUserService{
	//注入UserDao
	private IUserDao userDao;
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}


	public PageBean<User> queryByPage(UserQuery userquery) {


		PageBean pb = new PageBean();
		
		//1.设置当前页码
		pb.setCurPage(userquery.getCurPage());
		
		//添加查询条件
		String condition = "";
		List param = new ArrayList();
		if(userquery!=null){
			//StringUtils: 既能判断null,也能判断空字符串
			//关键词
			if(!StringUtils.isEmpty(userquery.getKeyword())){
				condition+=" and o.name like ? or o.realName like ? or o.phone like ? or o.email like ? ";
				param.add("%"+userquery.getKeyword()+"%");
				param.add("%"+userquery.getKeyword()+"%");
				param.add("%"+userquery.getKeyword()+"%");
				param.add("%"+userquery.getKeyword()+"%");
			}
			//录入时间
			if(!StringUtils.isEmpty(userquery.getBeginTime())){
				condition+=" and o.inputTime>=?";
				param.add(userquery.getBeginTime());
			}
			if(!StringUtils.isEmpty(userquery.getEndTime())){
				condition+=" and o.inputTime<=?";
				param.add(userquery.getEndTime());
			}
		}
		
		
		//2.设置当前页数
		pb.setData(userDao.queryByPage( condition , pb.getCurPage(), pb.getPageSize(), param.toArray() ) );
		//3.设置总记录数
		pb.setTotalCount(userDao.queryByCount(condition,param.toArray()));
		return pb;
	}




	public boolean checkName(String name,int id) {
		//如果是添加,则全部数据进行检查;如果是修改,则排除当前修改的用户
		List param = new ArrayList();
		String hql = "from User where name=?";
		param.add(name);
		//修改操作
		if(id!=0){
			hql += " and id<>?";
			param.add(id);
		}
		List<User> list = userDao.queryByHQL(hql, param.toArray());
		return list!=null&&list.size()>0; 
	}
	
	
	public void save(User user) {
		userDao.save(user);
	}


	public void delete(int id) {
		if(id != 0){
			userDao.delete(id);
		}
	}


	public User get(int id) {
		if(id !=0){
			return userDao.get(id);
		}
		return null;
	}


	public List<User> findAll() {
		// TODO Auto-generated method stub
		return userDao.queryAll();
	}




}





DeptService:

package gz.itcast.crm.service.impl;


import gz.itcast.crm.dao.IDeptDao;
import gz.itcast.crm.entity.Dept;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IDeptService;
import gz.itcast.crm.web.query.DeptQuery;


import java.util.ArrayList;
import java.util.List;


import org.springframework.util.StringUtils;


public class DeptServiceImpl implements IDeptService {


	private IDeptDao deptDao;
	public void setDeptDao(IDeptDao deptDao) {
		this.deptDao = deptDao;
	}


	public List<Dept> queryAll() {
		return deptDao.queryAll();
	}


	public void save(Dept dept) {
		deptDao.save(dept);
		
	}


	public Dept get(int id) {
		return deptDao.get(id);
	}


	public void delete(int id) {
		deptDao.delete(id);
	}


	public PageBean queryByPage(DeptQuery query) {
		//封装PageBean
		PageBean pb = new PageBean();
		
		//1.设置当前页码
		pb.setCurPage(query.getCurPage());
		
		String condition = "";
		List param = new ArrayList();
		if(query!=null){
			if(!StringUtils.isEmpty(query.getKeyword())){
				condition +=" and  o.deptName like ? or o.descr like ? or o.leader.realName like ?";
				param.add("%"+query.getKeyword()+"%");
				param.add("%"+query.getKeyword()+"%");
				param.add("%"+query.getKeyword()+"%");
			}
			//录入时间
			if(!StringUtils.isEmpty(query.getBeginTime())){
				condition+=" and o.inputTime>=?";
				param.add(query.getBeginTime());
			}
			if(!StringUtils.isEmpty(query.getEndTime())){
				condition+=" and o.inputTime<=?";
				param.add(query.getEndTime());
			}
			if(query.getLeader()!=0){
				condition +=" and  o.leader.id=?";
				param.add(query.getLeader());
			}
		}
		
		//2.设置当前页数
		pb.setData(deptDao.queryByPage( condition , pb.getCurPage(), pb.getPageSize(), param.toArray() ) );
		//3.设置总记录数
		pb.setTotalCount(deptDao.queryByCount(condition,param.toArray()));
		
		
		return pb;
	}


	public boolean checkName(String deptName, int id) {
			//如果是添加,则全部数据进行检查;如果是修改,则排除当前修改的用户
				List param = new ArrayList();
				String hql = "from Dept where deptName=?";
				param.add(deptName);
				//修改操作
				if(id!=0){
					hql += " and id<>?";
					param.add(id);
				}
				List<Dept> list = deptDao.queryByHQL(hql, param.toArray());
				return list!=null&&list.size()>0; 
	}


}




SystemLogService:
package gz.itcast.crm.service.impl;


import gz.itcast.crm.dao.ISystemLogDao;


import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.SystemLog;
import gz.itcast.crm.service.ISystemLogService;
import gz.itcast.crm.web.query.SystemLogQuery;


import java.util.ArrayList;
import java.util.List;


import org.springframework.util.StringUtils;




public class SystemLogServiceImpl implements ISystemLogService {
	
	private ISystemLogDao systemLogDao;
	public void setSystemLogDao(ISystemLogDao systemLogDao) {
		this.systemLogDao = systemLogDao;
	}


	public List<SystemLog> queryAll() {
		return systemLogDao.queryAll();
	}


	public void save(SystemLog SystemLog) {
		systemLogDao.save(SystemLog);
	}


	public SystemLog get(int id) {
		return systemLogDao.get(id);
	}


	public void delete(int id) {
		systemLogDao.delete(id);
	}


	public PageBean queryByPage(SystemLogQuery query) {
		//封装PageBean
		PageBean pb = new PageBean();
		
		//1.设置当前页码
		pb.setCurPage(query.getCurPage());
		
		//添加查询条件
		String condition = "";
		List param = new ArrayList();
		if(query!=null){
			if(!StringUtils.isEmpty(query.getKeyword())){
				condition +=" and  o.operator.realName like ? or o.func like ? or o.ip like ?";
				param.add("%"+query.getKeyword()+"%");
				param.add("%"+query.getKeyword()+"%");
				param.add("%"+query.getKeyword()+"%");
			}
			//录入时间
			if(!StringUtils.isEmpty(query.getBeginTime())){
				condition+=" and o.inputTime>=?";
				param.add(query.getBeginTime());
			}
			if(!StringUtils.isEmpty(query.getEndTime())){
				condition+=" and o.inputTime<=?";
				param.add(query.getEndTime());
			}
			if(query.getLeader()!=0){
				condition +=" and  o.operator.id=?";
				param.add(query.getLeader());
			}
		}
		
		//2.设置当前页数
		pb.setData(systemLogDao.queryByPage( condition , pb.getCurPage(), pb.getPageSize(), param.toArray() ) );
		//3.设置总记录数
		pb.setTotalCount(systemLogDao.queryByCount(condition,param.toArray()));
		return pb;
	}
}



action层:
UserAction:
package gz.itcast.crm.web;


import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.UserQuery;


import java.util.Date;
import java.util.List;
import java.util.Map;


import com.opensymphony.xwork2.ActionContext;


public class UserAction extends BaseAction{
	//注入userService
	private IUserService userService;
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	//接收User对象
	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	//接收UserQuery对象
	 UserQuery uquery = new UserQuery();
	public UserQuery getUquery() {
		return uquery;
	}
	public void setUquery(UserQuery uquery) {
		this.uquery = uquery;
	}
	//跳转页面
	public String input(){
		if(user != null &&user.getId() !=0){
			//修改操作
			user = userService.get(user.getId());
		}
		
		return "input";
	}
	
	//查询所有用户
	public String pageList(){
	if(uquery==null ||uquery.getCurPage()==null || uquery.getCurPage().equals("")){
		uquery.setCurPage(1);
		ActionContext.getContext().getSession().put("Uquery", uquery);
		}
		else{
			UserQuery uquery1 = (UserQuery) ActionContext.getContext().getSession().get("Uquery");
			uquery1.setCurPage(uquery.getCurPage());
			//System.out.println(uquery.getCurPage());
			uquery = uquery1;
		}
		PageBean pb = userService.queryByPage(uquery);
		putContext("pb", pb);
		return SUCCESS;
	}
	
	//校验save
	public void validateSave() {
		boolean result = userService.checkName(user.getName(),user.getId());
		if(result){
			addFieldError("user.name", "用户名已重复");
		}
	}
	
	//添加用户
	public String save(){
		user.setInputTime(new Date());
		userService.save(user);
		return "save";
	}
	
	//删除用户
	public String del(){
		userService.delete(user.getId());
		return pageList();
	}
	
	
}




DeptAction:
package gz.itcast.crm.web;


import gz.itcast.crm.entity.Dept;
import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.IDeptService;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.DeptQuery;


import java.util.Date;
import java.util.List;


import com.opensymphony.xwork2.ActionContext;




public class DeptAction extends BaseAction {
	//注入deptService
	private IDeptService deptService;
	public void setDeptService(IDeptService deptService) {
		this.deptService = deptService;
	}
	private IUserService userService;
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	//设置对象
	private Dept dept = new Dept();
	private DeptQuery query = new DeptQuery();
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	public DeptQuery getQuery() {
		return query;
	}
	public void setQuery(DeptQuery query) {
		this.query = query;
	}


	//跳转输入页面
	public String input(){
		if(dept != null &&dept.getId() !=0){
			dept = deptService.get(dept.getId());
		}
		List<User> list = userService.findAll();
		ActionContext.getContext().getSession().put("list", list);
		return "input";
	}


	//分页查询用户
	public String pageList(){
		//默认第一页
		if(query==null || query.getCurPage()==null || query.getCurPage().equals("")){
			query.setCurPage(1);
			ActionContext.getContext().getSession().put("dquery", query);
		}
		else{
			DeptQuery query1 = (DeptQuery) ActionContext.getContext().getSession().get("dquery");
			query1.setCurPage(query.getCurPage());
			query = query1;
		}
		//查询数据
		PageBean pb = deptService.queryByPage(query);
		
		//查询所有用户
		List<User> users = userService.findAll();
		putContext("users", users);
		
		putContext("pb", pb);
		return SUCCESS;
	}
	
	//校验保存
	public void validateSave() {
		boolean result = deptService.checkName(dept.getDeptName(),dept.getId());
		if(result){
			addFieldError("dept.deptName", "用户名已重复");
		}
	}
	
	//保存方法
	public String save(){
		//调用业务
		if(dept==null || dept.getId()==0){
			dept.setInputTime(new Date());
		}
		deptService.save(dept);
		//设置时间
		//回显列表页面
		return "save";
	}
	
	//删除用户
	public String delete(){
		deptService.delete(dept.getId());
		return pageList();
	}
	
	
}


SystemLogAction:

package gz.itcast.crm.web;


import gz.itcast.crm.entity.PageBean;
import gz.itcast.crm.entity.SystemLog;
import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.ISystemLogService;
import gz.itcast.crm.service.IUserService;
import gz.itcast.crm.web.query.SystemLogQuery;


import java.util.List;


import com.opensymphony.xwork2.ActionContext;


public class SystemLogAction extends BaseAction {
	//注入IsystemLogService
	private ISystemLogService systemLogService;
	public void setSystemLogService(ISystemLogService systemLogService) {
		this.systemLogService = systemLogService;
	}
	//注入IUserService
	private IUserService userService;
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	//设置query属性
	private SystemLogQuery query = new SystemLogQuery();
	public SystemLogQuery getQuery() {
		return query;
	}
	public void setQuery(SystemLogQuery query) {
		this.query = query;
	}
	//设置system属性
	private SystemLog system = new SystemLog();
	public SystemLog getSystem() {
		return system;
	}
	public void setSystem(SystemLog system) {
		this.system = system;
	}
		
	
	
	//分页查询用户
	public String pageList(){
		//默认第一页
		if(query==null || query.getCurPage()==null || query.getCurPage().equals("")){
			query.setCurPage(1);
			ActionContext.getContext().getSession().put("squery", query);
		}
		else{
			SystemLogQuery squery = (SystemLogQuery) ActionContext.getContext().getSession().get("squery");
			squery.setCurPage(query.getCurPage());
			query = squery;
		}
		//查询数据
		PageBean pb = systemLogService.queryByPage(query);
		List<User> users = userService.findAll();
		putContext("users", users);
		
		putContext("pb", pb);
		return SUCCESS;
	}
		
	//删除用户
	public String delete(){
		systemLogService.delete(system.getId());
		return pageList();
	}
}




在util层中,定义了一个切面类,也就是系统日志类,在applicationContext.xml文件中,采用aop切面的方法对其进行配置
SystemLogUtil:

	package gz.itcast.crm.util;


import gz.itcast.crm.entity.SystemLog;


import gz.itcast.crm.entity.User;
import gz.itcast.crm.service.ISystemLogService;


import java.util.Date;


import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;


import com.opensymphony.xwork2.ActionContext;


public class SystemLogUtil {


	private ISystemLogService systemLogService;
	public void setSystemLogService(ISystemLogService systemLogService) {
		this.systemLogService = systemLogService;
	}






	public void log(JoinPoint pt){
		
		
		Object target = pt.getTarget();
		if(target instanceof ISystemLogService){
			return;
		}
		
		String clazzname = target.getClass().getSimpleName();
		String method = pt.getSignature().getName();
		String func = clazzname +":"+ method + "()";
		
		SystemLog sl = new SystemLog();
		sl.setInputTime(new Date());
		
		String ip = ServletActionContext.getRequest().getRemoteHost();
		
		sl.setIp(ip);
		//sl.setIp("11111");
		//获取登录用户
		User u = new User();
		u.setId(1);
		
		sl.setOperator(u);
		sl.setFunc(func);
		
		//保存日志
		systemLogService.save(sl);
		
	}
}



还有一件很重要的事,便是使用SSH的时候需要在web.xml中进行配置,除了配置核心配置的过滤器和拦截器外,需要解决的懒加载问题,也可以在web.xml中进行配置

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>/WEB-INF/login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 解决sessionFactory的懒加载问题 -->
  <filter>
    <filter-name>osiv</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  	<init-param>
  		<param-name>sessionFactory</param-name>
  		<param-value>sessionFactory</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
    <filter-name>osiv</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 
 
  <!-- struts2的核心过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- spring的核心监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <servlet>
    <servlet-name>ImgServlet</servlet-name>
    <servlet-class>gz.itcast.crm.util.ImgServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>ImgServlet</servlet-name>
    <url-pattern>/ImgServlet.img</url-pattern>
  </servlet-mapping>
  
</web-app>




当整个项目都开发好了以后,在服务器中进行部署,在浏览器中打开的效果则为:



评论 3 您还未登录,请先 登录 后发表或查看评论
基于javaEE健康管理系统SSH设计软件源码+数据库+word毕业论文文档. javaEE健康管理系统主要功能包括:教师登录退出、教师饮食管理、教师健康日志、体检管理等等。本系统结构如下: (1)用户模块: 实现登录功能 实现用户登录的退出 实现用户注册 (2)教师饮食管理: 根据教师的相关信息,计算其每天所需热量,给出一周的推荐菜单。 (3)教师健康日志: 用户可以记录自己健康日记,记录每一天的睡眠,饮酒,吸烟,血压,体重等情况。 (4)教师体检管理: 教师可通过该平台查询每次的体检结果,显示部分项目随时间变化的曲线 (5)疾病预测评估: 针对糖尿病和高血压两种疾病,利用数学线性回归的方法,模拟计算出教师患病概率。 (6) 后台管理员模块 管理员用户登录功能:通过账号登录系统。 管理员管理教师功能:可以增删改查教师基本信息。 管理员管理体检结果:可以增删改查教师体检记录。 健康新闻管理:可以发布或删除、编辑新闻 面向特定群体的健康管理平台设计与实现 摘要 随着网络技术的不断发展,网站的开发与运用变得更加广泛。这次采用java语言SSH框架(Spring,Struts,Hibernate)设计并实现了面向特定群体的健康管理平台。该网站主要有教师饮食管理、教师健康日志、教师体检管理、疾病预测评估等功能模块。通过该网站,教师可以更好的关注自己的体检状况以及最近的身体状况,根据自身情况调整饮食,睡眠等生活习惯,并且可以及时预测高血压以及糖尿病的患病可能性大小,以便采取有效地预防措施。 本系统采用的是典型的J2EE 三层结构,分为表现层、中间层(业务逻辑层)和数据服务层。三层体系将业务规则、数据访问及合法性校验等工作放在中间层处理。客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与数据库交互。 关键词:健康管理,慢性病,Spring,Struts,Hibernate Design and Implement of Health Management Platform for the Specific Groups Abstract With the continuous development of network technology , the use of web site has became more widespread. This time I use JAVA language and SSH frame (Spring , Struts , Hibernate) to design and implement the health management platform for specific groups. Dietary management of teachers, teachers’ health log, medical management for teachers, disease forecast are designed in the web. Through the site, teachers can focus on their physical condition better, also can according to their circumstances to adjust their diet, sleep habits, etc. At the same time, we can take preventive measures to avoid hypertension and diabetes effectively. The system uses a three-tier structure called J2EE, divided into presentation layer, intermediate layer (business logic layer) and data services layer. Three-tier system put business rules, data access and legality verification work on the middle layer to process. Client does not establish a direct interaction with database, but make connections with the m
项目是作者当时花费了3个多月的时间完成的.在此拿出来给大家分享,请珍惜作者的劳动成果,用心研读! JEEBBS最新版本采用hibernate3+spring mvc+spring3+freemarker技术架构,重新设计了jeebbs,重新架构后的JEEBBS性能得到了很大的提升,功能相比jeebbs v1.0也丰富了许多。 jeebbsV4.0功能列表 1、论坛APP 2、登录更改shiro登录认证以及记住我 3、在线人数、时长统计 4、提供用户接口、其他系统用户接口调用设置以及接口管理(可与jeecms系列软件无缝对接实现单点登录) 5、用户自定义字段 6、禁用ip、id发帖、回帖 7、注册成功自动登录 8、设置在线活跃度等级 9、手机模板方案设置 10、最近登录过(三天,一周、一个月、三个月、半年)查询 11、类似微信团队号(与用户沟通账户以及推送系统消息) 12、QQ登录 jeebbsV4.0修复以及完善部分 1.权限的访问的地址链接 2.图片太大显示不全问题 3.会员组设置附件上线没有用以及其他相关设置无效 4.附件上传经常上传不了 5.发帖文字内容不能居中、居左、居右 编辑器字体、大小、插入图片、排序列表无效、左浮动、右浮动 6.用户自定义头像错误 7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 10.注册会员的时候提示邮箱的格式不对 11.登录设置邮箱密码文本框改成密码框

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

只有自己努力才是真

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值