【 SSH 实例】使用ssh开发的简单项目
简单的员工管理项目,使用spring、struts1、hibernate开发
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加载db.properties文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations" value="classpath*:*.properties"/>
</bean> <!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- //加载实体类的映射文件位置及名称 -->
<property name="mappingLocations" value="classpath:k/bean/*.hbm.xml"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> <!--事务-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/> <!--开启注解配置-->
<context:annotation-config/> <!--配置 action-->
<bean name="/login" class="k.action.LoginAction" scope="prototype"/>
<bean name="/employee" class="k.action.EmployeeAction" scope="prototype"/> <!--配置 Service-->
<bean name="testService" class="k.service.TestService"/>
<bean id="employeeService" name="employeeService" class="k.service.impl.EmployeeServiceImpl"/>
<bean name="departmentService" class="k.service.impl.DepartmentServiceImpl"/> </beans>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=root3306
initialSize=20
maxActive=500
maxIdle=2
minIdle=1
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置Hibernate的基本属性 -->
<!-- 1.数据源配置到IOC容器中 -->
<!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 -->
<!-- 3.配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!--<property name="cache.use_second_level_cache">true</property>-->
<property name="ache.provider_class">org.hibernate.cache.internal.DefaultCacheKeysFactory</property>
<property name="generate_statistics">true</property>
</session-factory>
</hibernate-configuration>
log4j.properties
log4j.rootLogger=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans>
<form-bean name="employeeForm" type="k.form.EmployeeForm">
<form-property name="id" type="java.lang.Integer"></form-property>
<form-property name="name" type="java.lang.String"></form-property>
<form-property name="password" type="java.lang.String"></form-property>
</form-bean>
</form-beans> <global-forwards>
<forward name="ok" path="/WEB-INF/jsp/ok.jsp"></forward>
<forward name="err" path="/WEB-INF/jsp/err.jsp"></forward>
</global-forwards> <action-mappings>
<action name="employeeForm" path="/login" parameter="action" type="k.action.LoginAction"
scope="request" attribute="employeeForm" input="index.jsp" validate="false">
<forward name="main" path="/WEB-INF/jsp/main.jsp"></forward>
<forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward>
</action>
<action name="employeeForm" path="/employee" parameter="action" type="k.action.EmployeeAction"
scope="request" attribute="employeeForm" input="index.jsp" validate="false">
<forward name="add" path="/WEB-INF/jsp/employee/add.jsp"></forward>
<forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward>
<forward name="list" path="/WEB-INF/jsp/employee/list.jsp"></forward>
<forward name="update" path="/WEB-INF/jsp/employee/update.jsp"></forward>
</action>
</action-mappings> <controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"></set-property>
</controller> </struts-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!-- spring 初始化 -->
<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-class>k.listener.StartSystemListener</listener-class>
<!--<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>-->
</listener> <!-- struts 初始化 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
<filter>
<filter-name>springUtf8Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springUtf8Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--<filter>-->
<!--<filter-name>EncodingFilter</filter-name>-->
<!--<filter-class>k.listener.EncodingFilter</filter-class>-->
<!--</filter>-->
<!--<filter-mapping>-->
<!--<filter-name>EncodingFilter</filter-name>-->
<!--<url-pattern>/*</url-pattern>-->
<!--</filter-mapping>--> <!-- 懒加载 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 以下3项参数与log4j的配置相关 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param> <context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener> </web-app>
LoginAction
package k.action; import k.domain.Employee;
import k.form.EmployeeForm;
import k.service.EmployeeService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginAction extends DispatchAction { @Resource
private EmployeeService employeeService; public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("loginJsp");
} public ActionForward doLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
EmployeeForm employeeOld = (EmployeeForm) form;
Employee employee = new Employee(Integer.parseInt(employeeOld.getId()), employeeOld.getPassword());
employee = employeeService.checkEmployee(employee);
if (employee != null) {
request.getSession().setAttribute("employee", employee);
return mapping.findForward("main");
}
return mapping.findForward("err");
} public ActionForward loginOut(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return super.execute(mapping, form, request, response);
} public ActionForward main(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("loginJsp");
}
}
EmployeeAction
package k.action; import k.domain.Department;
import k.domain.Employee;
import k.form.EmployeeForm;
import k.service.DepartmentService;
import k.service.EmployeeService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date; public class EmployeeAction extends DispatchAction { @Resource
private EmployeeService employeeService;
@Resource
private DepartmentService departmentService; public ActionForward addEmployeeUI(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("add");
} public ActionForward addEmployee(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
EmployeeForm employeeForm = (EmployeeForm) form;
Employee employee = new Employee();
employee.setName(employeeForm.getName());
employee.setEmail(employeeForm.getEmail());
employee.setSalary(employeeForm.getSalary());
employee.setPassword(employeeForm.getPassword());
employee.setGrade(employeeForm.getGrade());
employee.setHireDate(new Date());
Department department = (Department) departmentService.findById(Department.class, Integer.parseInt(employeeForm.getDepartment_id()));
employee.setDepartment(department);
try {
employeeService.add(employee);
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("err");
}
return mapping.findForward("ok");
} public ActionForward getList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// request.setAttribute("employeeList", employeeService.getEmployeeList());
// return mapping.findForward("list");
String s_pageNow = request.getParameter("pageNow");
int pageNow = 1;
int pageSize = 3;
if (s_pageNow != null) {
pageNow = Integer.parseInt(s_pageNow);
}
int pageCount = employeeService.getPageCount(pageSize);
request.setAttribute("pageCount", pageCount);
request.setAttribute("employeeList", employeeService.getEmployeeList(pageSize, pageNow));
return mapping.findForward("list");
} public ActionForward delEmp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
employeeService.delById(Employee.class, Integer.parseInt(request.getParameter("id")));
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("err");
}
return mapping.findForward("ok");
} public ActionForward updateUI(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String id = request.getParameter("id");
Employee employee = (Employee) employeeService.findById(Employee.class, Integer.parseInt(id));
request.setAttribute("emp", employee);
return mapping.findForward("update");
} public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
EmployeeForm employeeForm = (EmployeeForm) form;
Employee employee = new Employee();
employee.setId(Integer.parseInt(employeeForm.getId()));
employee.setName(employeeForm.getName());
employee.setEmail(employeeForm.getEmail());
employee.setSalary(employeeForm.getSalary());
employee.setPassword(employeeForm.getPassword());
employee.setGrade(employeeForm.getGrade());
employee.setHireDate(new Date());
try {
employeeService.executeUpdate(employee);
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("err");
}
return mapping.findForward("ok");
}
}
BasicAbstractService
package k.basic; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List; @Transactional
public abstract class BasicAbstractService implements BasicService { @Resource
private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} @Override
public void add(Object object) {
this.sessionFactory.getCurrentSession().save(object);
} @Override
public Object findById(Class classz, Serializable id) {
return this.sessionFactory.getCurrentSession().get(classz, id);
} @Override
public List executeQuery(String hql, Object[] parameters) {
Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
query.setParameter(i, parameters[i]);
}
}
return query.list();
} @Override
public List executeQueryByPage(String hql, Object[] parameters, Integer pageNow, Integer pageSize) {
Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
query.setParameter(i, parameters[i]);
}
}
return query.setFirstResult((pageNow - 1) * pageSize).setMaxResults(pageSize).list();
} @Override
public List executeUpdate(String hql, Object[] parameters) {
return null;
} @Override
public Object uniqueQuery(String hql, Object[] parameters) {
Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
return query.uniqueResult();
} @Override
public int queryPageCount(String hql, Object[] parameters, Integer pageSize) {
return 0;
} @Override
public void delById(Class clazz, Serializable id) {
Session session = this.sessionFactory.getCurrentSession();
session.delete(findById(clazz, id));
} @Override
public List executeUpdate(Object object) {
Session session = this.sessionFactory.getCurrentSession();
session.update(object);
return null;
}
}
BasicService
package k.basic; import java.io.Serializable;
import java.util.List; public interface BasicService { Object findById(Class classz, Serializable id); List executeQuery(String hql, Object[] parameters); List executeQueryByPage(String hql, Object[] parameters, Integer pageNow, Integer pageSize); void add(Object object); List executeUpdate(String hql, Object[] parameters); List executeUpdate(Object object); Object uniqueQuery(String hql, Object[] parameters); int queryPageCount(String hql, Object[] parameters, Integer pageSize); void delById(Class clazz, Serializable id);
}
Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="k.domain.Department" table="t_department">
<!--<cache usage="read-write"></cache>-->
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="java.lang.String" length="64"></property>
<!-- 在客户映射文件中,表示所有联系人
使用set标签表示所有联系人
set标签里面有name属性:
属性值写在客户实体类里面表示联系人的set集合名称 inverse属性默认值:false不放弃关系维护
true 表示放弃关系维护
-->
<set name="employees" inverse="true" cascade="save-update,delete">
<!-- 一对多建表,有外键
hibernate机制:双向维护外键,在一和多那一方都配置外键
column属性值:外键名称
-->
<key column="department_id"></key>
<one-to-many class="k.domain.Employee"></one-to-many>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="k.domain.Employee" table="t_employee">
<id name="id" column="id" type="java.lang.Integer">
<!-- 设置数据库表id增长策略 native:生成表id值就是主键自动增长 uuid -->
<generator class="native"></generator>
</id>
<!-- 配置其他属性和表字段对应 name属性:实体类属性名称 column属性:生成表字段名称 -->
<property name="name" column="name" type="java.lang.String" length="64"></property>
<property name="email" column="email" type="java.lang.String" length="64"></property>
<property name="hireDate" column="hireDate" type="java.util.Date"></property>
<property name="salary" column="salary" type="java.lang.Float"></property>
<property name="password" column="password" type="java.lang.String" length="64"></property>
<property name="grade" column="grade" type="java.lang.Integer" length="3"></property>
<many-to-one name="department" class="k.domain.Department" column="department_id"></many-to-one>
</class>
</hibernate-mapping>
Department
package k.domain; import java.util.Set; public class Department {
private Integer id;
private String name;
private Set<Employee> employees; public Department() {
} public Department(String name) {
this.name = name;
} public Integer getId() { return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Set<Employee> getEmployees() {
return employees;
} public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
Employee
package k.domain; import java.util.Date; public class Employee {
private Integer id;
private String name;
private String email;
private Date hireDate;
private Float salary;
private String password;
private Integer grade;
private Department department;
private String department_id; public Employee(Integer id, String password) {
this.id = id;
this.password = password;
} public Employee() {
} public Employee(String name, String password, String email, Date hireDate, Float salary, Integer grade) {
this.name = name;
this.email = email;
this.hireDate = hireDate;
this.salary = salary;
this.password = password;
this.grade = grade;
} public String getDepartment_id() {
return department_id;
} public void setDepartment_id(String department_id) {
this.department_id = department_id;
} public Integer getId() { return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Date getHireDate() {
return hireDate;
} public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
} public Float getSalary() {
return salary;
} public void setSalary(Float salary) {
this.salary = salary;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getGrade() {
return grade;
} public void setGrade(Integer grade) {
this.grade = grade;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} }
EmployeeForm
package k.form; import org.apache.struts.action.ActionForm; import java.util.Date; public class EmployeeForm extends ActionForm {
private String id;
private String name;
private String email;
private Date hireDate;
private Float salary;
private String password;
private Integer grade;
private String department_id; public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Date getHireDate() {
return hireDate;
} public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
} public Float getSalary() {
return salary;
} public void setSalary(Float salary) {
this.salary = salary;
} public Integer getGrade() {
return grade;
} public void setGrade(Integer grade) {
this.grade = grade;
} public String getDepartment_id() {
return department_id;
} public void setDepartment_id(String department_id) {
this.department_id = department_id;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
EncodingFilter
package k.listener; import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException; public class EncodingFilter extends HttpServlet implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
// System.out.println("========== set utf-8 ok ==========");
filterChain.doFilter(servletRequest, servletResponse);
} @Override
public void init(FilterConfig filterConfig) throws ServletException { }
}
StartSystemListener
package k.listener; import k.util.WebHelper; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; public class StartSystemListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//1.将项目上下文路径(request.getContextPath())放置到application域中.
ServletContext application = sce.getServletContext();
String app_path = application.getContextPath();
application.setAttribute("APP_PATH", app_path);
System.out.println("========== APP_PATH = " + app_path);
WebHelper.setApp_Path(app_path);
} @Override
public void contextDestroyed(ServletContextEvent sce) { }
}
DepartmentService
package k.service; import k.basic.BasicService; public interface DepartmentService extends BasicService { }
EmployeeService
package k.service; import k.basic.BasicService;
import k.domain.Employee; import java.io.Serializable;
import java.util.List; public interface EmployeeService extends BasicService {
Employee checkEmployee(Employee e); List getEmployeeList(int pageSize, int pageNo); List getEmployeeList(); int getPageCount(int pageSize); }
DepartmentServiceImpl
package k.service.impl; import k.basic.BasicAbstractService;
import k.service.DepartmentService; public class DepartmentServiceImpl extends BasicAbstractService implements DepartmentService { }
EmployeeServiceImpl
package k.service.impl; import k.basic.BasicAbstractService;
import k.domain.Employee;
import k.service.EmployeeService; import java.io.Serializable;
import java.util.List; public class EmployeeServiceImpl extends BasicAbstractService implements EmployeeService { @Override
public Employee checkEmployee(Employee e) {
String hql = "from Employee c where c.id=? and c.password=?";
Object[] parameters = new Object[]{e.getId(), e.getPassword()};
List list = executeQuery(hql, parameters);
return list != null && list.size() > 0 ? (Employee) list.get(0) : null;
} @Override
public List getEmployeeList(int pageSize, int pageNo) {
String hql = "from Employee order by id";
return this.executeQueryByPage(hql, null, pageNo, pageSize);
} @Override
public int getPageCount(int pageSize) {
String sql = "select count(*) from Employee";
Object obj = this.uniqueQuery(sql, null);
int rowCount = Integer.parseInt(obj.toString());
return (rowCount - 1) / pageSize + 1;
} @Override
public List getEmployeeList() {
String hql = "from Employee ";
return executeQuery(hql, null);
} }
WebHelper
package k.util; import java.util.UUID; public class WebHelper { public static String getApp_Path() {
return APP_PATH;
} public static void setApp_Path(String appPath) {
APP_PATH = appPath;
} private static String APP_PATH = ""; public static String getNewFileName(String fileName) {
int beginIndex = fileName.lastIndexOf(".");
return UUID.randomUUID().toString() + fileName.substring(beginIndex, fileName.length());
}
}
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加雇员</title>
</head>
<body>
<h1>添加雇员</h1>
<form action="${APP_PATH}/employee.do?action=addEmployee" method="post">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="name" value="张三4"></td>
</tr>
<tr>
<td>email:</td>
<td><input type="text" name="email" value="zhangsan@qq.com"></td>
</tr>
<tr>
<td>salary:</td>
<td><input type="text" name="salary" value="5000"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password" value="1"></td>
</tr>
<tr>
<td>grade:</td>
<td><input type="text" name="grade" value="1"></td>
</tr>
<tr>
<td>department_id:</td>
<td><select name="department_id">
<option value="1" selected>部门1</option>
</select></td>
</tr>
<tr>
<td><input type="submit" name="sub1" value="添加"></td>
<td><input type="reset" name="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>管理员列表</title>
</head>
<body>
<h1>管理员列表</h1>
<form action="${APP_PATH}/login.do?action=doLogin" method="post">
<table>
<tr>
<td>id</td>
<td>name</td>
<td>email</td>
<td>grade</td>
<td>salary</td>
<td>修改用户</td>
<td>删除用户</td>
</tr>
<c:forEach var="emp" items="${employeeList}">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.email}</td>
<td>${emp.grade}</td>
<td>${emp.salary}</td>
<td><a href="${APP_PATH}/employee.do?action=updateUI&id=${emp.id}">修改用户</a></td>
<td><a href="${APP_PATH}/employee.do?action=delEmp&id=${emp.id}">删除用户</a></td>
</tr>
</c:forEach>
</table>
<c:forEach var="i" begin="1" end="${pageCount}">
<a href="${APP_PATH}/employee.do?action=getList&pageNow=${i}">${i}</a>
</c:forEach>
</form>
</body>
</html>
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>修改用户</title>
</head>
<body>
<form id="form" action="${APP_PATH}/employee.do?action=update" method="post">
<table>
<tr hidden>
<td>id</td>
<td><input type="text" name="id" value="${emp.id}"></td>
</tr>
<tr>
<td>name</td>
<td><input type="text" name="name" value="${emp.name}"></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email" value="${emp.email}"></td>
</tr>
<tr>
<td>grade</td>
<td><input type="text" name="grade" value="${emp.grade}"></td>
</tr>
<tr>
<td>salary</td>
<td><input type="text" name="salary" value="${emp.salary}"></td>
</tr>
<tr>
<td><input type="submit" name="salary" value="修改"></td>
</tr>
</table>
</form>
</body>
</html>
err.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>操作失败</title>
</head>
<body>
<h1>操作失败</h1>
<a href="#">操作失败,返回主界面</a>
<a href="${APP_PATH}/employee.do?action=getList">返回列表</a>
<a href="${APP_PATH}/login.do?action=login">返回登录</a>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>管理员登录</title>
</head>
<body>
<h1>管理员登录</h1>
<form action="${APP_PATH}/login.do?action=doLogin" method="post">
<table>
<tr>
<td>id:</td>
<td><input type="text" name="id" value="1"></td>
</tr>
<tr>
<td>name:</td>
<td><input type="text" name="name" value="1哈哈"></td>
</tr>
<tr>
<td>pwd:</td>
<td><input type="password" name="password" value="1"></td>
</tr>
<tr>
<td><input type="submit" name="sub1" value="登录"></td>
<td><input type="reset" name="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h1>登录成功</h1>
<a href="${APP_PATH}/login.do?action=login">返回登录</a>
<h2>欢迎 【${employee.name}】 您所在的部门是 【${employee.getDepartment().getName()} 】请选择您要进行的操作</h2>
<a href="${APP_PATH}/employee.do?action=addEmployeeUI">添加雇员</a> <br/>
<a href="${APP_PATH}/employee.do?action=getList">显示雇员</a> <br/>
<a href="#">查询雇员</a> <br/>
<a href="#">退出系统</a> <br/>
<a href="${APP_PATH}/employee.do?action=getList">返回列表</a>
</body>
</html>
ok.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>成功</h1>
<a href="${APP_PATH}/employee.do?action=addEmployeeUI">返回继续添加</a>
<a href="${APP_PATH}/employee.do?action=getList">返回列表</a>
<a href="${APP_PATH}/login.do?action=login">返回登录</a>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:forward page="WEB-INF/jsp/login.jsp"></jsp:forward>
</body>
</html>
jar包
EmployeeServiceImpl
【 SSH 实例】使用ssh开发的简单项目的更多相关文章
- Python3学习之路~8.2 socket简单实例 实现ssh 发送大量数据
实例1: 利用socket模拟客户端和服务器端各自收发一次数据: #Author:Zheng Na # 客户端 import socket # 声明socket类型,同时生成socket连接对象 cl ...
- SSH框架分模块开发
------------------siwuxie095 SSH 框架分模块开发 1.在 Spring 核心配置文件中配置多个内容,容易造成 配置混乱,不利于维护 「分模块开发主要针对 Spring ...
- 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务
[源码下载] 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后 ...
- Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状)
Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状) 本篇博客来给大家介绍怎样使用Lua这门语言来开发一个简单的小游戏-记数字踩白块. 游戏的流程是这种:在界面上生成5个数1~5字并显 ...
- 多人开发的git项目如何保持提交日志为一条直线?
多人开发的git项目如何保持提交日志为一条直线? 一.Git的项目的git常用操作 a)Git clone 项目地址 从远程仓库克隆项目到本地 b)Git pull 从当前分支拉取更新代码 c)Git ...
- MVVM With ReactiveCocoa让移动开发更简单
作者:@雷纯锋2011 MVVM是一种软件架构模式,它是 Martin Fowler 的 Presentation Model 的一种变体,最先由微软的架构师 John Gossman 在 2005 ...
- Python开发一个简单的BBS论坛
项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...
- .NET Web开发技术简单整理 转
.NET Web开发技术简单整理 原文:http://www.cnblogs.com/SanMaoSpace/p/3157293.html 在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何 ...
- 3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
随机推荐
- wa自动机 的 莫队 刷题记录
洛谷P2709小B的询问 莫队裸题,模板题 莫队就是把询问区间排个序,先按左端点的Pos排序(POS是分块那个数组),pos一样的按右端点排序 代码: #include <bits/stdc++ ...
- 1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP
1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP 题意 用摄像机观察动物,有两个摄像机,一个可以放在奇数天,一个可以放在偶数天.摄像机在 ...
- C语言-浮点数的秘密
一.浮点数的秘密 1.内存中的浮点数 浮点数在内存中的存储方式为:符号位.指数.尾数 十进制浮点数的内存表示: 实例分析: #include <stdio.h> //打印十进制的内存表示 ...
- java序列化与反序列化的使用
个人博客 地址:http://www.wenhaofan.com/article/20180925214701 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字 ...
- crontab定时任务打包备份文件并删除过期文件
Crontab 示例,最后这里要写成shell脚本定时运行 30 17 * * * cp -rf /usr/local/tomcat9-jforum/tomcat/logs/catalina.out ...
- C++构造函数和重载函数运算符如何区分
构造函数和重载函数运算符如何区分: class Distance { private: int feet; int inches; public: Distance(){ feet = ; inche ...
- Docker最全教程——从理论到实战(十四)
本篇教程主要讲解基于容器服务搭建TeamCity服务,并且完成内部项目的CI流程配置.教程中也分享了一个简单的CI.CD流程,仅作探讨.不过由于篇幅有限,完整的DevOps,我们后续独立探讨. 为了降 ...
- Java 并发核心机制
目录 一.J.U.C 简介 二.synchronized 三.volatile 四.CAS 五.ThreadLocal 参考资料
- 浅析ReDoS
ReDoS(Regular expression Denial of Service) 正则表达式拒绝服务攻击.开发人员使用了正则表达式来对用户输入的数据进行有效性校验, 当编写校验的正则表达式存在缺 ...
- C#.net连接Sybase的方法
一 .ODBC方式连接 1 安装Sybase客户端,安装ODBC驱动,配置DSN<略> 2 连接代码 string strconn = "DSN=TEST;SRVR=TEST;D ...