管理系统-------------SSH框架书写登录和显示用户

时间:2024-04-19 19:04:26

一、思路的穿插。

    web.xml中的配置找到--->application.xml---->找到对应的Action---->找到struts.xml----->在去找action----->service的实现类---->dao的impl实现类。

二、SSH框架整合需要的jar

  管理系统-------------SSH框架书写登录和显示用户

    管理系统-------------SSH框架书写登录和显示用户

三、实现登录

  (一)、创建实体

/*
* 员工表
* */
@Entity
@Table(name="sys_employee")
public class Employee {
@Id
@GeneratedValue
private String Sn;//员工编号
private Integer Position_id;//岗位编号
private Integer Department_id;//部门编号
private String Name;//员工姓名
private String Password;//密码
private String Stuatus;//状态
@ManyToOne(fetch=FetchType.LAZY)//和岗位表是多对一的关系
@JoinColumn(name="Posiid")
private Position position; @ManyToOne(fetch=FetchType.LAZY)
private Department department;   //省get/set方法
}
/*
* 岗位表
* */
@Entity
@Table(name="sys_position")
public class Position {
@Id
@GeneratedValue
@Column(name="ID")
private Integer Posiid;//岗位编号
@Column(name="NAME_CN")
private String Name_cn;//岗位名称(中文)
@Column(name="NAME_EN")
private String Name_en;//岗位名称(英文)
    //省get/set方法
}

  (二)1.创建dao

public interface LoginDao {
//登录
public Employee login(Employee emp) throws Exception;
}

    2.dao.Impl

public class LoginDaoImpl implements LoginDao {
private SessionFactory sessionFactory;//创建一个工厂对象
    //登录
public Employee login(Employee emp) throws Exception {
// System.out.println("SessionFactory"+sessionFactory);
//System.out.println("Session"+getSesion());
String hql = "from Employee where Sn=? and Password=?";
Employee emps=(Employee) getSesion().createQuery(hql).setString(0,emp.getSn()).setString(1, emp.getPassword()).uniqueResult();
System.out.println(emps);
return emps;
}
//创建一个工厂
private Session getSesion() {
return sessionFactory.getCurrentSession(); }
public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} }

  (三)1.创建service.dao

public interface LoginServiceDao {
//登录
public Employee login(Employee emp) throws Exception;
}

  2.创建service.Impl

public class LoginServiceDaoImpl implements LoginServiceDao {

    //登录的daoimpl的对象
private LoginDaoImpl li; @Transactional
public Employee login(Employee emp) throws Exception {
// TODO Auto-generated method stub
System.out.println("实现类");
return li.login(emp);
} public LoginDaoImpl getLi() {
return li;
} public void setLi(LoginDaoImpl li) {
this.li = li;
} }

  (四)创建Action

public class LoginAction extends ActionSupport {
private LoginServiceDao oaservice;
private Employee emp; public String login() throws Exception { Employee employee = oaservice.login(emp);
if (employee!=null) { ServletActionContext.getRequest().getSession().setAttribute("emp", employee);
//ServletActionContext.getRequest().getSession().setAttribute("position",employee.getPosition().getName_cn());
//ServletActionContext.getContext().getSession().put("position", employee);
return SUCCESS;
}else
return INPUT;
} public LoginServiceDao getOaservice() {
return oaservice;
} public void setOaservice(LoginServiceDao oaservice) {
this.oaservice = oaservice;
} public Employee getEmp() {
return emp;
} public void setEmp(Employee emp) {
this.emp = emp;
} }

(五)配置文件

  application.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="bdqn"></property>
<property name="password" value="bdqn"></property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!--找配置好的连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 添加Hibernate配置参数 -->
<property name="hibernateProperties">
<props>
<!--配置Hibernate方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--配置Hibernate在控制台显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!--配置Hibernate自动提交 -->
<!-- <prop key="hibernate.connection.autocommit">true</prop> -->
<!--配置Hibernate格式化 -->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class"> org.springframework.orm.hibernate3.SpringSessionContext</prop>
</props>
</property> <!--扫描所有包下的注解信息 包扫描器 -->
<property name="packagesToScan" value="cn.oa.entity"></property> </bean> <!-- 登录 -->
<!--配置Dao -->
<bean id="Logindao" class="cn.oa.dao.impl.LoginDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置Service 相当于之前我们使用new来构建的实例对象 -->
<bean id="OAService" class="cn.oa.service.dao.impl.LoginServiceDaoImpl">
<property name="li" ref="Logindao"></property>
</bean> <!--配置Action -->
<bean id="select" class="cn.oa.action.LoginAction" scope="prototype">
<property name="oaservice" ref="OAService"></property>
</bean> <!--查看部门 -->
<!--配置Dao -->
<bean id="deparDao" class="cn.oa.dao.impl.IdepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置Service 相当于之前我们使用new来构建的实例对象 -->
<bean id="serviceDao" class="cn.oa.service.dao.impl.DeparmentServiceDaoImpl">
<property name="deparmentImpl" ref="deparDao"></property>
</bean> <!--配置Action -->
<bean id="Claim" class="cn.oa.action.DeparmentAction" scope="prototype">
<property name="serviceDao" ref="serviceDao"></property>
</bean> <!--查看岗位 -->
<!--配置Dao -->
<bean id="positionDao" class="cn.oa.dao.impl.PositionDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置Service 相当于之前我们使用new来构建的实例对象 -->
<bean id="PositionserviceDao" class="cn.oa.service.dao.impl.PositionServiceDaoImpl">
<property name="position" ref="positionDao"></property>
</bean> <!--配置Action -->
<bean id="PAction" class="cn.oa.action.PositionAction" scope="prototype">
<property name="positionService" ref="PositionserviceDao"></property>
</bean> <!-- 注解的事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<!-- <property name="proxyTargetClass" >
<value>true</value>
</property> -->
</bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

    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>
<!-- Action 查看部门信息 -->
<package name="default" namespace="/" extends="struts-default"> <!-- 在spring的配置里面 -->
<action name="Action" class="select" method="login">
<result name="input">/login.jsp</result>
<result name="success">/index.jsp</result>
</action> <!-- 查找部门所有的信息 -->
<action name="ClaimAction" class="Claim" method="getList">
<result name="input">/index.jsp</result>
<result name="success">/deptList.jsp</result>
</action> <!-- 查找岗位所有的信息 放入的是Action类的路径-->
<action name="PositionAction" class="PAction">
<result name="input">/index.jsp</result>
<result name="success">/positionList.jsp</result> </action> </package> <!-- Action查找部门返回错误的页面-->
<package name="dept" namespace="/dept" extends="struts-default"> <!-- 在spring的配置里面 -->
<action name="deptAction" class="Claim" method="login">
<result name="error">/inde.jsp</result>
</action>
</package> <!-- <package name="josn_default" namespace="/" extends="json-default">
<action name="ClaimAction" class="Claim"> <result type="json" name="input">/index.jsp</result>
<result type="json" name="success">/deptList.jsp</result> <result type="json">
<param name="success">/deptList.jsp</param> </result> </action> </package> -->
</struts>

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name> <!-- 指定 applicationContext.xml的路径(或者放置与WEB-INF目录下)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 做一些初始化工作的监听器 -->
<listener>
<listener-class>cn.oa.listener.InitListener</listener-class>
</listener> <filter>
<filter-name>openSessionInview</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInview</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> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

(六)登录页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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> <style type="text/css">
* {
margin: 0;
padding: 0;
} body {
font: 12px 宋体;
background: #4BB8EF url(images/bg.gif) repeat-x;
} img {
border: 0;
} .login-top {
width: 100%;
height: 186px;
margin: 147px auto 0;
background: url(images/login_01.gif) no-repeat center 0;
} .login-area {
width: 100%;
height: 140px;
margin: 0 auto;
background: url(images/login_02.gif) no-repeat center 0;
} .login-area form {
width: 290px;
margin: 0 auto;
} .login-area label {
clear: left;
float: left;
margin-top: 13px;
width: 60px;
font: 600 14px 宋体;
} .login-area input {
width: 122px;
height: 16px;
margin-top: 11px;
border: 1px #767F94 solid;
font: 12px/ 16px 宋体;
} input.login-sub {
width: 104px;
height: 34px;
border: 0;
background: url(images/login_sub.gif) no-repeat 0px 1px; *
margin-top: 5px;
} .login-copyright {
width: 100%;
height: 30px;
margin: 18px auto 0;
background: url(images/copyright.gif) no-repeat center 0;
}
</style>
<script type="text/javascript">
function changeValidateCode(obj) {
//获取当前的时间作为参数,无具体意义
var timenow = new Date().getTime();
//每次请求需要一个不同的参数,否则可能会返回同样的验证码
//这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。
obj.src = "random.action?d=" + timenow;
}
function check() {
var msg = document.getElementById("msg").value;
if (msg.length != 0) {
alert(msg);
document.getElementById("msg").value = "";
}
}
</script>
</head>
<body onload="check()">
<div class="login-top"></div>
<div class="login-area">
<form action="Action" method="post">
<label>
工&nbsp;&nbsp;号:
</label>
<input type="text" name="emp.Sn" />
<label>
密&nbsp;&nbsp;码:
</label>
<input type="password" name="emp.Password" />
<label>
验证码:
</label>
<input type="text" name="random" size="6" />
<input type="image" src="random.action"
onclick="changeValidateCode(this)" title="点击图片刷新验证码" />
<input type="submit" class="login-sub" value="" />
<s:actionerror cssStyle="margin-top: 10px;"/>
<input type="hidden" id="msg" value="${requestScope.msg }" />
</form>
</div>
<div class="login-copyright"></div>
</body>
</html>

四、实现部门显示信息用(easyUI)

  (一)创建dao

public interface IdepartmentDao {
//查看所有的部门
public List<Department> getDepar();
//根据分页查询出指定的部门信息
public List<Department> getdeptBypage(int pageSize, int pageIndex)throws Exception; //查询所有记录
public int getAllCount() throws Exception; }

  Impl实现类

public class DeparmentAction extends ActionSupport {
private IdeparmentServiceDao serviceDao; private Department dept;
private List<Department> deptlist; private Integer rows;//和datagrid控件绑定的记录条数
private Integer page;//和datafrid控件绑定的页码数
private int size;
private String json; public String getList() throws Exception{
System.out.println("AAAAAAAA");
List<Department> list = serviceDao.getdeptBypage(rows, page);
System.out.println(list+"bbb");
setDeptlist(list);
size=serviceDao.getAllCount();
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); System.out.println(getJson().getBytes("utf-8"));
ServletActionContext.getResponse().getOutputStream().write(getJson().getBytes("utf-8"));
return NONE;
} @Override public String execute() throws Exception { /*System.out.println("action");
System.out.println("serviceDao"+serviceDao);*/
List<Department> depar = serviceDao.getDepar();
for (Department item : depar) {
System.out.println(item.getDaparid());
System.out.println(item.getDaparidname());
} if(depar!=null){
return SUCCESS;
}
return INPUT;
}
public IdeparmentServiceDao getServiceDao() {
return serviceDao;
}
public void setServiceDao(IdeparmentServiceDao serviceDao) {
this.serviceDao = serviceDao;
}
public Department getDept() {
return dept;
} public void setDept(Department dept) {
this.dept = dept;
} public List<Department> getDeptlist() {
return deptlist;
} public void setDeptlist(List<Department> deptlist) {
this.deptlist = deptlist;
} public Integer getRows() {
return rows;
} public void setRows(Integer rows) {
this.rows = rows;
} public Integer getPage() {
return page;
} public void setPage(Integer page) {
this.page = page;
} public int getSize() {
return size;
} public void setSize(int size) {
this.size = size;
} public String getJson() {
String json=null;
try {
json = "{\"total\":"+size+",\"rows\":"+JSONUtil.serialize(getDeptlist())+"}";
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("aaa"+json);
return json; } public void setJson(String json) {
this.json = json;
}

  (二)service.dao

public interface IdeparmentServiceDao {
//查看所有的部门
public List<Department> getDepar();
//根据分页查询出指定的部门信息
public List<Department> getdeptBypage(int pageSize, int pageIndex)throws Exception; //查询所有记录
public int getAllCount() throws Exception;
}

    Impl

public class DeparmentServiceDaoImpl implements IdeparmentServiceDao{
private IdepartmentDaoImpl deparmentImpl; @Transactional
public List<Department> getDepar() {
System.out.println("DeparmentServiceDaoImpl");
System.out.println("deparmentImpl"+deparmentImpl);
return deparmentImpl.getDepar();
}
public List<Department> getdeptBypage(int pageSize, int pageIndex)
throws Exception { return deparmentImpl.getdeptBypage(pageSize, pageIndex);
} public IdepartmentDaoImpl getDeparmentImpl() {
return deparmentImpl;
}
public void setDeparmentImpl(IdepartmentDaoImpl deparmentImpl) {
this.deparmentImpl = deparmentImpl;
}
public int getAllCount() throws Exception { return deparmentImpl.getAllCount();
} }
public class DeparmentAction extends ActionSupport {
private IdeparmentServiceDao serviceDao; private Department dept;
private List<Department> deptlist; private Integer rows;//和datagrid控件绑定的记录条数
private Integer page;//和datafrid控件绑定的页码数
private int size;
private String json; public String getList() throws Exception{
System.out.println("AAAAAAAA");
List<Department> list = serviceDao.getdeptBypage(rows, page);
System.out.println(list+"bbb");
setDeptlist(list);
size=serviceDao.getAllCount();
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); System.out.println(getJson().getBytes("utf-8"));
ServletActionContext.getResponse().getOutputStream().write(getJson().getBytes("utf-8"));
return NONE;
} @Override public String execute() throws Exception { /*System.out.println("action");
System.out.println("serviceDao"+serviceDao);*/
List<Department> depar = serviceDao.getDepar();
for (Department item : depar) {
System.out.println(item.getDaparid());
System.out.println(item.getDaparidname());
} if(depar!=null){
return SUCCESS;
}
return INPUT;
}
public IdeparmentServiceDao getServiceDao() {
return serviceDao;
}
public void setServiceDao(IdeparmentServiceDao serviceDao) {
this.serviceDao = serviceDao;
}
public Department getDept() {
return dept;
} public void setDept(Department dept) {
this.dept = dept;
} public List<Department> getDeptlist() {
return deptlist;
} public void setDeptlist(List<Department> deptlist) {
this.deptlist = deptlist;
} public Integer getRows() {
return rows;
} public void setRows(Integer rows) {
this.rows = rows;
} public Integer getPage() {
return page;
} public void setPage(Integer page) {
this.page = page;
} public int getSize() {
return size;
} public void setSize(int size) {
this.size = size;
} public String getJson() {
String json=null;
try {
json = "{\"total\":"+size+",\"rows\":"+JSONUtil.serialize(getDeptlist())+"}";
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("aaa"+json);
return json; } public void setJson(String json) {
this.json = json;
} }

    jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<script type="text/javascript" src="<%=path %>/js/jquery-easyui-1.2.6/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=path %>/js/jquery-easyui-1.2.6/jquery.easyui.min.js"></script>
<link rel="stylesheet" href="<%=path %>/js/jquery-easyui-1.2.6/themes/icon.css" type="text/css"></link>
<link rel="stylesheet" href="<%=path %>/js/jquery-easyui-1.2.6/themes/default/easyui.css" type="text/css"></link>
</head> <body>
<div align="right"></div>
<table id="dg" title="部门信息" style="width:700px;height:310px"><a href="<%=path%>/jsp/system/deptAdd.jsp">添加部门</a></table>
<script type="text/javascript">
$(function(){
$("#dg").datagrid({
url:'<%=path%>/ClaimAction',
iconCls : 'icon-search',//表头标题图片
collapsible : true,//是否可折叠的
rowNumbers:true,//显示行号
pagination:true,//显示底部分页栏
striped:true,//隔行变色
pageSize : 5,//设置每页默认显示的记录数
pageList:[5,10,15],
columns:[[
{field:'daparid',title:'编号',width:200 },
{field:'daparidname',title:'名称',width:200},
/* {field:'parent',title:'上级部门名称',
formatter: function(value,row,index){
if(value==null){
return "";
}else{
return "<a >"+row.parent.name+"</a>";
}
},
width:200}, */
/* {field:'caozuo',title:'操作',
formatter: function(value,row,index){ return "<a href='#'>编辑</a> <a href='/dept/deptAction!deleteDeptBySn?dept.id='"+row.id+">删除</a>";
}, width:200}
*/
]], loadMsg:"数据正在进行加载" ,
});
});
</script>
</body>
</html>