采用的框架 Struts2+Spring4+Hbiernate4.

目录结构

   

EmployeeAction:

  1. package com.xx.ssh.actions;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.InputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.Date;
  7. import java.util.Map;
  8. import org.apache.struts2.interceptor.RequestAware;
  9. import com.opensymphony.xwork2.ActionSupport;
  10. import com.opensymphony.xwork2.ModelDriven;
  11. import com.opensymphony.xwork2.Preparable;
  12. import com.xx.ssh.entities.Employee;
  13. import com.xx.ssh.service.DepartmentService;
  14. import com.xx.ssh.service.EmployeeService;
  15.  
  16. public class EmployeeAction extends ActionSupport implements RequestAware,
  17. ModelDriven<Employee>, Preparable {
  18.  
  19. private static final long serialVersionUID = 1L;
  20.  
  21. private EmployeeService employssService;
  22.  
  23. public void setEmployssService(EmployeeService employssService) {
  24. this.employssService = employssService;
  25. }
  26.  
  27. private DepartmentService departmentService;
  28.  
  29. public void setDepartmentService(DepartmentService departmentService) {
  30. this.departmentService = departmentService;
  31. }
  32.  
  33. public String list() {
  34. request.put("employees", employssService.getAll());
  35. System.out.println("request: " + request.size());
  36. return "list";
  37. }
  38.  
  39. private Integer id;
  40.  
  41. public void setId(Integer id) {
  42. this.id = id;
  43. }
  44.  
  45. private InputStream inputStream;
  46.  
  47. public InputStream getInputStream() {
  48. return inputStream;
  49. }
  50. //回调函数。判断是否删除
  51. public String delete() {
  52. try {
  53. employssService.delete(id);
  54. inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. try {
  58. inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
  59. } catch (UnsupportedEncodingException e1) {
  60. e1.printStackTrace();
  61. }
  62. }
  63. return "ajax-success";
  64. }
  65.  
  66. private String lastName;
  67.  
  68. public void setLastName(String lastName) {
  69. this.lastName = lastName;
  70. }
  71. //回调函数。判断用户名是否存在。
  72. public String validateLastName() {
  73. try {
  74. if (employssService.lastNameIsValid(lastName)) {
  75.  
  76. inputStream = new ByteArrayInputStream("1".getBytes("utf-8"));
  77. } else {
  78.  
  79. inputStream = new ByteArrayInputStream("0".getBytes("utf-8"));
  80. }
  81. } catch (Exception e) {
  82.  
  83. }
  84. return "ajax-success";
  85. }
  86.  
  87. private Employee model;
  88.  
  89. /*
  90. * 可以根椐ID来判断为save方法准备的model是new的还是数据库获取的。
  91. */
  92. public void prepareSave() {
  93. if (id == null) {
  94. model = new Employee();
  95. } else {
  96. model = employssService.get(id);
  97. }
  98. }
  99.  
  100. public String save() {
  101.  
  102. if (id == null) {
  103. model.setCreateTime(new Date());
  104.  
  105. }
  106. employssService.saveOrUpdate(model);
  107. return SUCCESS;
  108. }
  109.  
  110. public String input() {
  111. request.put("departments", departmentService.getAll());
  112. return INPUT;
  113. }
  114.  
  115. public void prepareInput() {
  116. if (id != null) {
  117. model = employssService.get(id);
  118. }
  119.  
  120. }
  121.  
  122. private Map<String, Object> request;
  123.  
  124. @Override
  125. public void setRequest(Map<String, Object> arg0) {
  126. this.request = arg0;
  127.  
  128. }
  129.  
  130. @Override
  131. public Employee getModel() {
  132.  
  133. return model;
  134. }
  135.  
  136. @Override
  137. public void prepare() throws Exception {
  138.  
  139. }
  140.  
  141. }

SSHDateConverter:自定义转换器

  1. package com.xx.ssh.converters;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.Map;
  8.  
  9. import org.apache.struts2.util.StrutsTypeConverter;
  10.  
  11. public class SSHDateConverter extends StrutsTypeConverter {
  12.  
  13. private DateFormat dateFormat;
  14. {
  15. dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  16. }
  17.  
  18. @Override
  19. public Object convertFromString(Map context, String[] values, Class toClass) {
  20. if(toClass == Date.class){
  21. try {
  22. return dateFormat.parse(values[0]);
  23. } catch (ParseException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. return null;
  29. }
  30.  
  31. @Override
  32. public String convertToString(Map context, Object o) {
  33. if(o instanceof Date){
  34. return dateFormat.format((Date)o);
  35. }
  36. return null;
  37. }
  38.  
  39. }

BaseDao:SessionFactory

  1. package com.xx.ssh.dao;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5.  
  6. public class BaseDao {
  7. private SessionFactory sessionFactory;
  8.  
  9. public void setSessionFactory(SessionFactory sessionFactory) {
  10. this.sessionFactory = sessionFactory;
  11. }
  12.  
  13. public Session getSession() {
  14. return this.sessionFactory.getCurrentSession();
  15. }
  16. }

DepartmentDao:Dao层

  1. package com.xx.ssh.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import com.xx.ssh.entities.Department;
  6.  
  7. public class DepartmentDao extends BaseDao{
  8. public List<Department> getAll(){
  9. String hql="FROM Department";
  10. return getSession().createQuery(hql).list();
  11. }
  12. }

EmployeeDao

  1. package com.xx.ssh.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import org.hibernate.Query;
  6. import org.hibernate.Session;
  7. import org.hibernate.SessionFactory;
  8.  
  9. import com.xx.ssh.entities.Employee;
  10.  
  11. public class EmployeeDao extends BaseDao {
  12.  
  13. public void delete(Integer id){
  14. String hql="delete from Employee e where e.id=? ";
  15. getSession().createQuery(hql).setInteger(0,id).executeUpdate();
  16. }
  17.  
  18. public List<Employee> getAll(){
  19.  
  20. String hql="from Employee e LEFT OUTER JOIN FETCH e.department";
  21. return getSession().createQuery(hql).list();
  22. }
  23. public void saveOrUpdate(Employee employee){
  24. getSession().saveOrUpdate(employee);
  25. }
  26. public Employee getEmployeeByLastName(String lastName){
  27.  
  28. String hql="from Employee e where e.lastName=? ";
  29. Query query = getSession().createQuery(hql).setString(0,lastName);
  30. return (Employee)query.uniqueResult();
  31. }
  32. public Employee get(Integer id){
  33. return (Employee) getSession().get(Employee.class,id);
  34.  
  35. }
  36. }

实体:Department

  1. package com.xx.ssh.entities;
  2.  
  3. public class Department {
  4. private Integer id;
  5. private String departmentName;
  6.  
  7. public Integer getId() {
  8. return id;
  9. }
  10.  
  11. public void setId(Integer id) {
  12. this.id = id;
  13. }
  14.  
  15. public String getDepartmentName() {
  16. return departmentName;
  17. }
  18.  
  19. public void setDepartmentName(String departmentName) {
  20. this.departmentName = departmentName;
  21. }
  22. }

实体:Employee

  1. package com.xx.ssh.entities;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Employee {
  6.  
  7. //
  8. private Integer id;
  9. //不能被修改
  10. private String lastName;
  11. private String email;
  12. //从前端传入的是string类型,所以需要注意转换。
  13. private Date birth;
  14. //不能被修改
  15. private Date createTime;
  16. //单向n-1的关联关系
  17. private Department department;
  18.  
  19. public Integer getId() {
  20. return id;
  21. }
  22. public void setId(Integer id) {
  23. this.id = id;
  24. }
  25. public String getLastName() {
  26. return lastName;
  27. }
  28. public void setLastName(String lastName) {
  29. this.lastName = lastName;
  30. }
  31. public String getEmail() {
  32. return email;
  33. }
  34. public void setEmail(String email) {
  35. this.email = email;
  36. }
  37. public Date getBirth() {
  38. return birth;
  39. }
  40. public void setBirth(Date birth) {
  41. this.birth = birth;
  42. }
  43. public Date getCreateTime() {
  44. return createTime;
  45. }
  46. public void setCreateTime(Date createTime) {
  47. this.createTime = createTime;
  48. }
  49. public Department getDepartment() {
  50. return department;
  51. }
  52. public void setDepartment(Department department) {
  53. this.department = department;
  54. }
  55.  
  56. @Override
  57. public String toString() {
  58. return "Employee [birth=" + birth + ", createTime=" + createTime
  59. + ", department.id=" + department + ", email=" + email + ", id="
  60. + id + ", lastName=" + lastName + "]";
  61. }
  62. }

表与类映射文件配置。

Department.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- Generated 2014-7-22 11:21:48 by Hibernate Tools 3.4.0.CR1 -->
  5. <hibernate-mapping>
  6. <class name="com.xx.ssh.entities.Department" table="SSH_DEPARTMENT">
  7. <id name="id" type="java.lang.Integer">
  8. <column name="ID" />
  9. <generator class="native" />
  10. </id>
  11.  
  12. <property name="departmentName" type="java.lang.String">
  13. <column name="DEPARTMENT_NAME" />
  14. </property>
  15.  
  16. </class>
  17. </hibernate-mapping>

Employee.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- Generated 2014-7-22 11:21:48 by Hibernate Tools 3.4.0.CR1 -->
  5. <hibernate-mapping>
  6. <class name="com.xx.ssh.entities.Employee" table="SSH_EMPLOYEE">
  7.  
  8. <id name="id" type="java.lang.Integer">
  9. <column name="ID" />
  10. <generator class="native" />
  11. </id>
  12.  
  13. <property name="lastName" type="java.lang.String">
  14. <column name="LAST_NAME" />
  15. </property>
  16.  
  17. <property name="email" type="java.lang.String">
  18. <column name="EMAIL" />
  19. </property>
  20.  
  21. <property name="birth" type="java.util.Date">
  22. <column name="BIRTH" />
  23. </property>
  24.  
  25. <property name="createTime" type="java.util.Date">
  26. <column name="CREATE_TIME" />
  27. </property>
  28.  
  29. <!-- 映射单向 n-1 的关联关系 -->
  30. <many-to-one name="department" class="com.xx.ssh.entities.Department" lazy="false">
  31. <column name="DEPARTMENT_ID" />
  32. </many-to-one>
  33.  
  34. </class>
  35. </hibernate-mapping>

Service层:Department

  1. package com.xx.ssh.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.xx.ssh.dao.DepartmentDao;
  6. import com.xx.ssh.entities.Department;
  7.  
  8. public class DepartmentService {
  9. private DepartmentDao departmentDao;
  10.  
  11. public void setDepartmentDao(DepartmentDao departmentDao){
  12. this.departmentDao=departmentDao;
  13. }
  14. public List<Department>getAll(){
  15. return departmentDao.getAll();
  16. }
  17.  
  18. }

Service层:Employee

  1. package com.xx.ssh.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.xx.ssh.dao.EmployeeDao;
  6. import com.xx.ssh.entities.Employee;
  7.  
  8. public class EmployeeService {
  9. private EmployeeDao employeeDao;
  10.  
  11. public void setEmployeeDao(EmployeeDao employeeDao)
  12. {
  13. this.employeeDao=employeeDao;
  14. }
  15. public boolean lastNameIsValid(String lastName){
  16. return employeeDao.getEmployeeByLastName(lastName)==null;
  17. }
  18. public void delete(Integer id){
  19. employeeDao.delete(id);
  20. }
  21. public void saveOrUpdate(Employee employee){
  22. employeeDao.saveOrUpdate(employee);
  23. }
  24. public List<Employee> getAll(){
  25. List<Employee> employees=employeeDao.getAll();
  26. /*employees.clear();*/
  27. System.out.println(employees.size());
  28. return employees;
  29. }
  30. public Employee get(Integer id) {
  31. // TODO Auto-generated method stub
  32. return employeeDao.get(id);
  33.  
  34. }
  35.  
  36. }

配置文件:

applicationContext-beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean id="employeeDao" class="com.xx.ssh.dao.EmployeeDao">
  8. <property name="sessionFactory" ref="sessionFactory"></property>
  9. </bean>
  10.  
  11. <bean id="departmentDao" class="com.xx.ssh.dao.DepartmentDao">
  12. <property name="sessionFactory" ref="sessionFactory"></property>
  13. </bean>
  14.  
  15. <bean id="employeeService" class="com.xx.ssh.service.EmployeeService">
  16. <property name="employeeDao" ref="employeeDao"></property>
  17. </bean>
  18.  
  19. <bean id="departmentService" class="com.xx.ssh.service.DepartmentService">
  20. <property name="departmentDao" ref="departmentDao"></property>
  21. </bean>
  22.  
  23. <bean id="employeeAction" class="com.xx.ssh.actions.EmployeeAction"
  24. scope="prototype">
  25. <property name="employssService" ref="employeeService"></property>
  26. <property name="departmentService" ref="departmentService"></property>
  27. </bean>
  28. </beans>

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  15.  
  16. <!-- 导入资源文件 -->
  17. <context:property-placeholder location="classpath:db.properties"/>
  18.  
  19. <!-- 配置 C3P0 数据源 -->
  20. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  21. <property name="user" value="${jdbc.user}"></property>
  22. <property name="password" value="${jdbc.password}"></property>
  23. <property name="driverClass" value="${jdbc.driverClass}"></property>
  24. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  25. </bean>
  26.  
  27. <!-- 配置 SessionFactory -->
  28. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  29. <property name="dataSource" ref="dataSource"></property>
  30. <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
  31. <property name="mappingLocations" value="classpath:com/xx/ssh/entities/*.hbm.xml"></property>
  32. </bean>
  33.  
  34. <!-- 配置 Spring 的声明式事务 -->
  35. <!-- 1. 配置 hibernate 的事务管理器 -->
  36. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  37. <property name="sessionFactory" ref="sessionFactory"></property>
  38. </bean>
  39.  
  40. <!-- 2. 配置事务属性 -->
  41. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  42. <tx:attributes>
  43. <tx:method name="get*" read-only="true"/>
  44. <tx:method name="lastNameIsValid" read-only="true"/>
  45. <tx:method name="*"/>
  46. </tx:attributes>
  47. </tx:advice>
  48.  
  49. <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
  50. <aop:config>
  51. <aop:pointcut expression="execution(* com.xx.ssh.service.*.*(..))" id="txPointcut"/>
  52. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  53. </aop:config>
  54. </beans>

db.properties

  1. jdbc.user=root
  2. jdbc.password=root
  3. jdbc.driverClass=com.mysql.jdbc.Driver
  4. jdbc.jdbcUrl=jdbc:mysql:///spring6
  5.  
  6. jdbc.initPoolSize=5
  7. jdbc.maxPoolSize=10

hibernate.cfg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 配置hibernate的基本属性-->
  8.  
  9. <!-- 方言 -->
  10. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  11.  
  12. <!--是否显示及格式化SQL-->
  13. <property name="hibernate.show_sql">true</property>
  14. <property name="hibernate.format_sql">true</property>
  15.  
  16. <!-- 生成数据表的策略 -->
  17. <property name="hibernate.hbm2ddl.auto">update</property>
  18.  
  19. <!--二级缓存相关 -->
  20. </session-factory>
  21. </hibernate-configuration>

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7.  
  8. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  9. <constant name="struts.devMode" value="true" />
  10.  
  11. <package name="default" namespace="/" extends="struts-default">
  12.  
  13. <!-- 定义新的拦截器栈, 配置 prepare 拦截器栈的 alwaysInvokePrepare 参数值为 false -->
  14. <interceptors>
  15. <interceptor-stack name="sshStack">
  16. <interceptor-ref name="paramsPrepareParamsStack">
  17. <param name="prepare.alwaysInvokePrepare">false</param>
  18. </interceptor-ref>
  19. </interceptor-stack>
  20. </interceptors>
  21.  
  22. <!-- 使用新的拦截器栈 -->
  23. <default-interceptor-ref name="sshStack"></default-interceptor-ref>
  24.  
  25. <action name="emp-*" class="employeeAction"
  26. method="{1}">
  27. <result name="list">/WEB-INF/views/emp-list.jsp</result>
  28. <result type="stream" name="ajax-success">
  29. <param name="contentType">text/html</param>
  30. <param name="inputName">inputStream</param>
  31. </result>
  32. <result name="input">/WEB-INF/views/emp-input.jsp</result>
  33. <result name="success" type="redirect">/emp-list</result>
  34. </action>
  35.  
  36. </package>
  37.  
  38. </struts>

xwork-conversion.properties :时间转换器配置文件。

java.util.Date=com.xx.ssh.converters.SSHDateConverter

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6.  
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext*.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener> <!-- 为spring添加监听器 -->
  14. <!-- 配置 Struts2 的 Filter -->
  15. <filter>
  16. <filter-name>struts2</filter-name>
  17. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>struts2</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. </web-app>

JSP:

emp-input.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="s" uri="/struts-tags" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.8.0.js"></script>
  10. <script type="text/javascript">
  11. $(function(){
  12. $(":input[name=lastName]").change(function(){
  13. var val= $(this).val();
  14. val=$.trim(val);
  15. var $this=$(this);
  16. if(val!=""){
  17. $this.nextAll("font").remove();
  18. var url="emp-validateLastName";
  19. var args={"lastName":val,"time":new Date()};
  20.  
  21. $.post(url,args,function(data){
  22.  
  23. //表示可用
  24. if(data == "1"){
  25. $this.after("<font color='green'>LastName可用</font>" );
  26. }
  27. //表示不可用
  28. else if(data == "0") {
  29. $this.after("<font color='red'>LastName不可用</font>" );
  30. }else{
  31. alert("服务器错误");
  32. }
  33. })
  34. }else{
  35. //alert("lastName 不能为空");
  36. var i=$(this).val("");
  37. alert(i);
  38. //this.focus();
  39. }
  40. })
  41. })
  42. </script>
  43. </head>
  44. <body>
  45.  
  46. <s:debug></s:debug>
  47.  
  48. <h4>Employee Input Page</h4>
  49.  
  50. <s:form action="emp-save" method="post">
  51. <s:if test="id != null">
  52. <s:textfield name="lastName" label="LastName" disabled="true"></s:textfield>
  53. <s:hidden name="id"></s:hidden>
  54. <%--
  55. <!-- 通过添加隐藏域的方式把未显式提交的字段值提交到服务器 -->
  56. <s:hidden name="lastName"></s:hidden>
  57. <s:hidden name="createTime"></s:hidden>
  58. --%>
  59. </s:if>
  60. <s:else>
  61. <s:textfield name="lastName" label="LastName"></s:textfield>
  62. </s:else>
  63.  
  64. <s:textfield name="email" label="Email"></s:textfield>
  65. <s:textfield name="birth" label="Birth"></s:textfield>
  66.  
  67. <s:select list="#request.departments"
  68. listKey="id" listValue="departmentName" name="department.id"
  69. label="Department"></s:select>
  70. <s:submit></s:submit>
  71. </s:form>
  72.  
  73. </body>
  74. </html>

emp-list.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%><!-- -->
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Insert title here</title>
  10. <script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery-1.8.0.js"></script>
  11. <script type="text/javascript" >
  12. $(function(){
  13. //1.点击delete时,弹出确定是要删除xx的信息吗。若确定,删除。反之。取消
  14. $(".delete").click(function(){
  15. var lastName=$(this).next(":input").val();
  16. var flag=confirm("确定是要删除"+lastName+"信息吗?");
  17. if(flag){
  18. var $tr=$(this).parent().parent();
  19.  
  20. //删除,使用ajax方式 。
  21. var url=this.href;
  22. var args={"time":new Date()};
  23. $.post(url,args,function(data){
  24. //若data的返回值为1.则提示删除成功,且把当前行删除 。
  25. if(data=="1"){
  26. alert("删除成功");
  27. $tr.remove();
  28. }else{
  29. alert("删除失败");
  30. }
  31. });
  32. }
  33. //取消默认行为。
  34. return false;
  35. });
  36.  
  37. });
  38.  
  39. </script>
  40. </head>
  41. <body>
  42. <h4>Employee List Page</h4>
  43. <s:if test="#request.employees == null || #request.employees.size()==0">
  44. 没有任何员工信息;
  45. </s:if>
  46. <s:else>
  47. <table border="1" cellpadding="10" cellspacing="0">
  48. <tr>
  49. <td>ID</td>
  50. <td>LASTNAME</td>
  51. <td>EMAIL</td>
  52. <td>BIRTH</td>
  53. <td>CREATETIME</td>
  54. <td>DEPT</td>
  55. <td>DELETE</td>
  56. <td>Edit</td>
  57. </tr>
  58. <s:iterator value="#request.employees">
  59. <tr>
  60. <td>${id } </td>
  61. <td>${lastName }</td>
  62. <td>${email }</td>
  63. <td>
  64. <s:date name="birth" format="yyyy-MM-dd"/>
  65. </td>
  66. <td>
  67. <s:date name="birth" format="yyyy-MM-dd hh:mm:ss"/>
  68. </td>
  69. <td>${department.departmentName }</td>
  70. <td><a href="emp-delete?id=${id }" class="delete">Delete</a>
  71. <input type="hidden" value="${lastName }"/>
  72. </td>
  73. <td><a href="emp-input?id=${id }">Edit</a></td>
  74. </tr>
  75. </s:iterator>
  76. </table>
  77. </s:else>
  78. </body>
  79. </html>

整合过程出现的异常请点我:

S2SH CRUD 整合的更多相关文章

  1. s2sh框架整合具体配置-xml方式

    s2sh整合之xml方式 说明:本文档所採用的框架版本号为:Struts 2.1.8, Sping2.5.5,  Hibernate 3.5.6 1.    须要的jar包: ------------ ...

  2. S2SH框架整合(注解)Struts2+Spring+Hibernate+MySql

    整合简介 Struts2+Spring4+hibernate4整合,Maven管理jar包,Eclipse工具.注解方式 架构截图   1.Spring整合Hibernate 1.1.创建Hibern ...

  3. 框架技术--S2SH框架整合(spring部分)No 3--声明式事务

    声明式事务:就是讲事务的处理,通过配置进行配置. 几种传播特性  1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务.如果没有事务则开启(比较常用)  2. PROPA ...

  4. Spring入门4.AOP配置深入

    Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...

  5. JAVA学习路线图---(JAVA1234)

    第一阶段-Java基础   这一阶段很重要,关系到你后面阶段的学习,所以务必把这一阶段掌握好: 如果你是0基本,我推荐一本比较好的,适合初学者看的书:明日科技的<Java从入门到精通>,最 ...

  6. JAVA学习路线图---(JAVA1234) 分类: B1_JAVA 2013-10-05 10:22 502人阅读 评论(1) 收藏

    转自:http://blog.csdn.net/pplcheer/article/details/12276999 第一阶段-Java基础        这一阶段很重要,关系到你后面阶段的学习,所以务 ...

  7. JDBC基础:JDBC快速入门,JDBC工具类,SQL注入攻击,JDBC管理事务

    JDBC基础 重难点梳理 一.JDBC快速入门 1.jdbc的概念 JDBC(Java DataBase Connectivity:java数据库连接)是一种用于执行SQL语句的Java API,可以 ...

  8. 整合s2sh,实现页面操作数据库

    先说点废话 s2sh,就是struts2,spring,hibernate:s2作为表现层和控制器,hibernate作为持久层,spring作为业务层(充分应用IOC和AOP).其实业务还是业务,只 ...

  9. Spring boot(三)整合mybaties+thymeleaf实现基础crud

    工程结构: 首先在pom文件中引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

随机推荐

  1. golang flag包

    go flag 包用来解析命令行参数,通过一个简单的例子来了解下 package main import (     "flag"     "fmt" ) fu ...

  2. Saddest's polar bear Pizza offered new YorkShire home

    Saddest:adj,可悲的,悲哀的,polar,两级的,极地额,YorkShire,约克郡 A UK wildlife park has confirmed that it is offering ...

  3. EXT.net DateField format设置

    DateField df = new DateField();                        df.Format = "yyyy-MM-dd HH:mm:ss";格 ...

  4. fastdfs-nginx扩展模块源码分析

    FastDFS-Nginx扩展模块源码分析 1. 背景 在大多数业务场景中,往往需要为FastDFS存储的文件提供http下载服务,而尽管FastDFS在其storage及tracker都内置了htt ...

  5. [转]PHP Session的一个警告

    警告全文如下: PHP Warning: Unknown: Your script possibly relies on a session side-effect which existed unt ...

  6. php.ini修改php上传文件大小限制的方法详解

    打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...

  7. 基于Lattice_CPLD/FPGA Diamond 开发流程

         本文主要介绍了Lattice CPLD/FPGA集成开发环境的使用方法,并通过点亮开发板(Mach XO2 Breakout Board)上位号为D2的LED这一实例来演示其开发流程. 1. ...

  8. 在ASP.NET Web API中使用OData

    http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...

  9. 使用图灵机器人API实现聊天机器人

    使用图灵机器人的API需要先注册,获取key才行,这我就不说了,自己到http://www.tuling123.com/注册一个账号即可. 下面就是一个简单的python调用API实现聊天机器人的简易 ...

  10. maven的阿里镜像

    偶然发现maven有了阿里镜像 vim ~/.m2/setting.xml <mirrors> <mirror> <id>alimaven</id> & ...