zoukankan      html  css  js  c++  java
  • SSH_框架整合7--整个项目CODE

    一 架构

      1Action类

      

      2 配置文件

        

      3 View页面

      

    二  Code

      1 src

        (1)com.atguigu.ssh.actions

          >EmployeeAction.java

          
      1 package com.atguigu.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 
      9 import org.apache.struts2.interceptor.RequestAware;
     10 
     11 import com.atguigu.ssh.entities.Employee;
     12 import com.atguigu.ssh.service.DepartmentService;
     13 import com.atguigu.ssh.service.EmployeeService;
     14 import com.opensymphony.xwork2.ActionSupport;
     15 import com.opensymphony.xwork2.ModelDriven;
     16 import com.opensymphony.xwork2.Preparable;
     17 
     18 public class EmployeeAction extends ActionSupport implements RequestAware,
     19 ModelDriven<Employee>,Preparable{
     20     
     21     private static final long serialVersionUID = 1L;
     22     
     23     private EmployeeService employeeService;
     24     
     25     public void setEmployeeService(EmployeeService employeeService){
     26         this.employeeService=employeeService;
     27     }
     28     
     29     //5 校验注册的姓名是否可用
     30     private String lastName;
     31     
     32     public void setLastName(String lastName) {
     33         this.lastName = lastName;
     34     }
     35     
     36     public String validateLastName() throws UnsupportedEncodingException{
     37         if(employeeService.lastNameIsValid(lastName)){
     38             inputStream = new ByteArrayInputStream("1".getBytes("UTF-8")); 
     39         }else{
     40             inputStream = new ByteArrayInputStream("0".getBytes("UTF-8")); 
     41         }
     42         return "ajax-success";
     43     }
     44     
     45     //**** 4 使用ModelDriven拦截器方法存储添加的信息
     46     public String save(){
     47         //第一次添加 就加上系统时间,否则直接保存不修改时间
     48         if(id == null){
     49             model.setCreateTime(new Date());
     50         }
     51         employeeService.saveorUpdate(model);
     52         return SUCCESS;
     53     }
     54     
     55     /**
     56      * 可以根据 id 来判断为 save 方法准备的 model 是 new 的还是从数据库获取的!
     57      */
     58     //****6-2 Edit
     59     public void prepareSave(){
     60         //第一次添加
     61         if(id == null){
     62             model=new Employee();
     63         }
     64         //修改,从数据库中获取
     65         else{
     66             model = employeeService.get(id);
     67         }
     68         
     69     }
     70     
     71     //3-1  查询Department
     72     private DepartmentService departmentService;
     73     public void setDepartmentService(DepartmentService departmentService) {
     74         this.departmentService = departmentService;
     75     }
     76     //3-2 实现添加员工信息的方法
     77     public String input(){
     78         request.put("departments", departmentService.getAll());
     79         return INPUT;
     80     }
     81     
     82     //**** 6-1   拦截器的方法
     83     public void prepareInput(){
     84         //说明在修改,这时从数据库中获取状态
     85         if(id != null){
     86             model = employeeService.get(id);
     87         }
     88     }
     89     //2 删除
     90     private Integer id;
     91     public void setId(Integer id) {
     92         this.id = id;
     93     }
     94     /*public String delete(){
     95         employeeService.delete(id);
     96         return SUCCESS;
     97     }*/
     98     //2-1 使用Ajax方式删除
     99     private InputStream inputStream;
    100     
    101     public InputStream getInputStream() {
    102         return inputStream;
    103     }
    104     
    105     public String delete(){
    106         employeeService.delete(id);
    107         try {
    108             //删除成功
    109             inputStream=new ByteArrayInputStream("1".getBytes("UTF-8"));
    110         } catch (UnsupportedEncodingException e) {
    111             //删除失败
    112             try {
    113                 inputStream=new ByteArrayInputStream("0".getBytes("UTF-8"));
    114             } catch (UnsupportedEncodingException e1) {
    115                 // TODO Auto-generated catch block
    116                 e1.printStackTrace();
    117             }
    118             e.printStackTrace();
    119         }
    120         return "ajax-success";
    121     }
    122     //1 查询
    123     public String list(){
    124         request.put("employees", employeeService.getAll()); 
    125         return "list";
    126     }
    127 
    128     //放到页面里
    129     private Map<String,Object> request;
    130     
    131     @Override
    132     public void setRequest(Map<String, Object> arg0) {
    133         this.request=arg0;
    134     }
    135     
    136     //****
    137     @Override
    138     public void prepare() throws Exception {
    139     }
    140     //****
    141     private Employee model;
    142     //****
    143     @Override
    144     public Employee getModel() {
    145         return model;
    146     }
    147 
    148 }
    View Code

        (2)com.atguigu.ssh.converters

          >SSHDateConverter.java

          
     1 package com.atguigu.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     public SSHDateConverter()
    16     {
    17         dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    18     }
    19     
    20     @Override
    21     public Object convertFromString(Map context, String[] values, Class toClass) {
    22         if(toClass == Date.class){
    23             if(values !=null && values.length>0){
    24                 String value=values[0];
    25                 try {
    26                     return dateFormat.parse(value);
    27                 } catch (ParseException e) {
    28                     e.printStackTrace();
    29                 }
    30             }
    31         }
    32         return values;
    33     }
    34 
    35     @Override
    36     public String convertToString(Map context, Object o) {
    37         if(o instanceof Date){
    38             Date date=(Date)o;
    39             return dateFormat.format(date);
    40         }
    41         return null;
    42     }
    43 
    44 }
    View Code

        (3)com.atguigu.ssh.dao

          >BaseDao.java

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

          >DepartmentDao.java

          
     1 package com.atguigu.ssh.dao;
     2 
     3 import java.util.List;
     4 
     5 import com.atguigu.ssh.entities.Department;
     6 
     7 public class DepartmentDao extends BaseDao{
     8 
     9     //获取查询到的Department集合
    10     public List<Department> getAll(){
    11         String hql="FROM Department";
    12         return getSession().createQuery(hql).list();
    13     }
    14 }
    View Code

          >EmployeeDao.java     

          
     1 package com.atguigu.ssh.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Query;
     6 
     7 import com.atguigu.ssh.entities.Employee;
     8 
     9 public class EmployeeDao extends BaseDao{
    10     
    11     /*放到父类BaseDao中
    12      private SessionFactory sessionFactory;
    13     
    14     public void setSessionFactory(SessionFactory sessionFactory){
    15         this.sessionFactory=sessionFactory;
    16     }
    17     
    18     public Session getSession(){
    19         return  this.sessionFactory.getCurrentSession();
    20     }*/
    21     
    22     //5    Edit
    23     public Employee get(Integer id){
    24         return (Employee) getSession().get(Employee.class, id);
    25     }
    26     
    27     //4 校验注册的姓名是否可用
    28     public Employee getEmployeeByLastName(String lastName){
    29         String hql = "FROM Employee e WHERE e.lastName = ?";
    30         Query query = getSession().createQuery(hql).setString(0, lastName);
    31         Employee employee = (Employee) query.uniqueResult();
    32         //System.out.println("***"+employee.getDepartment().getClass().getName());
    33         return employee;
    34     }
    35     //3 添加、存储
    36     public void saveOrUpadate(Employee employee){
    37         getSession().saveOrUpdate(employee);
    38     }
    39     //2 删除
    40     public void delete(Integer id){
    41         String hql="DELETE FROM Employee e WHERE e.id=?";
    42         getSession().createQuery(hql).setInteger(0,id).executeUpdate();
    43     }
    44     //1获取
    45     public List<Employee> getAll(){
    46         String hql="FROM Employee  e LEFT OUTER JOIN FETCH e.department";
    47         return getSession().createQuery(hql).list();
    48     }
    49 }
    View Code

        (4)com.atguigu.ssh.entities

          >Department.java

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

          >Employee.java

          
     1 package com.atguigu.ssh.entities;
     2 
     3 import java.util.Date;
     4 
     5 public class Employee {
     6     private Integer id;
     7     
     8     private String lastName="";
     9     private String email;
    10     
    11     private Date birth;
    12     private Date createTime;
    13     
    14     private Department department;
    15 
    16     public Integer getId() {
    17         return id;
    18     }
    19 
    20     public void setId(Integer id) {
    21         this.id = id;
    22     }
    23 
    24     public String getLastName() {
    25         return lastName;
    26     }
    27 
    28     public void setLastName(String lastName) {
    29         this.lastName = lastName;
    30     }
    31 
    32     public String getEmail() {
    33         return email;
    34     }
    35 
    36     public void setEmail(String email) {
    37         this.email = email;
    38     }
    39 
    40     public Date getBirth() {
    41         return birth;
    42     }
    43 
    44     public void setBirth(Date birth) {
    45         this.birth = birth;
    46     }
    47 
    48     public Date getCreateTime() {
    49         return createTime;
    50     }
    51 
    52     public void setCreateTime(Date createTime) {
    53         this.createTime = createTime;
    54     }
    55 
    56     public Department getDepartment() {
    57         return department;
    58     }
    59 
    60     public void setDepartment(Department department) {
    61         this.department = department;
    62     }
    63 
    64     /*@Override
    65     public String toString() {
    66         return "Employee [id=" + id + ", lastName=" + lastName + ", email="
    67                 + email + ", birth=" + birth + ", createTime=" + createTime
    68                 + ", department=" + department+ "]";
    69     }*/
    70     
    71     
    72 }
    View Code

          >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 2016-9-18 16:53:56 by Hibernate Tools 3.4.0.CR1 -->
     5 <hibernate-mapping>
     6     <class name="com.atguigu.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         <property name="departmentName" type="java.lang.String">
    12             <column name="DEPARTMENT_NAME" />
    13         </property>
    14     </class>
    15 </hibernate-mapping>
    View Code

          >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 2016-9-18 16:53:56 by Hibernate Tools 3.4.0.CR1 -->
     5 <hibernate-mapping>
     6     <class name="com.atguigu.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         <property name="lastName" type="java.lang.String">
    13             <column name="LAST_NAME" />
    14         </property>
    15         <property name="email" type="java.lang.String">
    16             <column name="EMAIL" />
    17         </property>
    18         <property name="birth" type="java.util.Date">
    19             <column name="BIRTH" />
    20         </property>
    21         <property name="createTime" type="java.util.Date">
    22             <column name="CREATE_TIME" />
    23         </property>
    24         <many-to-one name="department" class="com.atguigu.ssh.entities.Department" lazy="false">
    25             <column name="DEPARTMENT_ID" />
    26         </many-to-one>
    27     </class>
    28 </hibernate-mapping>
    View Code

        (5)com.atguigu.ssh.service

          >DepartmentService.java

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

          >EmployeeService.java

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

      2 conf

        (1)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 http://www.springframework.org/schema/beans/spring-beans.xsd">
     5     
     6     <bean id="employeeDao" class="com.atguigu.ssh.dao.EmployeeDao">
     7         <property name="sessionFactory" ref="sessionFactory"></property>
     8     </bean>
     9     
    10     <bean id="departmentDao" class="com.atguigu.ssh.dao.DepartmentDao">
    11         <property name="sessionFactory" ref="sessionFactory"></property>
    12     </bean>
    13     
    14     <bean id="employeeService" class="com.atguigu.ssh.service.EmployeeService">
    15         <property name="employeeDao" ref="employeeDao"></property>
    16     </bean>
    17     
    18     <bean id="departmentService" class="com.atguigu.ssh.service.DepartmentService">
    19         <property name="departmentDao" ref="departmentDao"></property>
    20     </bean>
    21     
    22     <bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction"
    23           scope="prototype">
    24         <property name="employeeService" ref="employeeService"></property>
    25         <property name="departmentService" ref="departmentService"></property>
    26     </bean>
    27 </beans>
    View Code

        (2)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:tx="http://www.springframework.org/schema/tx"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xmlns:p="http://www.springframework.org/schema/p"
     8     xmlns:util="http://www.springframework.org/schema/util"
     9     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    11         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
    12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    14     
    15     <!-- 导入资源文件 -->
    16     <context:property-placeholder location="classpath:db.properties"/>
    17     
    18     <!-- 导入C3P0数据源 -->
    19     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    20         <property name="user" value="${jdbc.user}"></property>
    21         <property name="password" value="${jdbc.password}"></property>
    22         <property name="driverClass" value="${jdbc.driverClass}"></property>
    23         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    24         
    25         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    26         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    27     </bean>
    28     
    29     <!--配置Hibernate 的sessionFactory的实例  -->
    30     <bean id="sessionFactory" 
    31           class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    32         <property name="dataSource" ref="dataSource"></property>
    33         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    34         <property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property>
    35     </bean>    
    36     
    37     <!--配置Spring声明式事务  -->
    38     <!--1 配置hibernate事务管理器  -->
    39     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    40         <property name="sessionFactory" ref="sessionFactory"></property>
    41     </bean>
    42     
    43     <!--2 配置事务属性  -->    
    44     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    45         <tx:attributes>
    46             <tx:method name="get*" read-only="true"/>
    47             <tx:method name="lastNameIsValid" read-only="true"/>
    48             <tx:method name="*"/>
    49         </tx:attributes>
    50     </tx:advice>
    51      
    52      <!--3 配置事务切入点  -->
    53     <aop:config>
    54         <aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))" id="txPointCut"/>
    55         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    56     </aop:config>
    57     
    58     
    59     
    60     
    61     
    62     
    63     
    64 
    65 </beans>
    View Code

        (3)db.properties

          
    1 jdbc.user=root
    2 jdbc.password=920614
    3 jdbc.driverClass=com.mysql.jdbc.Driver
    4 jdbc.jdbcUrl=jdbc:mysql:///spring_ssh
    5 
    6 jdbc.initPoolSize=5
    7 jdbc.maxPoolSize=10
    8 #...
    View Code

        (4)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         
     8         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
     9         <property name="hibernate.show_sql">true</property>
    10         <property name="hibernate.format_sql">true</property>
    11         <property name="hibernate.hbm2ddl.auto">update</property>
    12     </session-factory>
    13 </hibernate-configuration>
    View Code

        (5)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         <default-interceptor-ref name="sshStack"></default-interceptor-ref>
    23         
    24         <action name="emp-*" class="employeeAction"
    25                 method="{1}">
    26                 <result name="list">/WEB-INF/views/emp-list.jsp</result>
    27                 
    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                    
    34                    <result name="success" type="redirect">/emp-list</result>     
    35         </action>
    36     </package>
    37 
    38 </struts>
    View Code

        (6)xwork-conversion.properties

          
    1 java.util.Date=com.atguigu.ssh.converters.SSHDateConverter
    View Code

         

      3 WebContent-----WEB-INF

        (1)views

          >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="scripts/jquery-1.7.2.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             
    17             if(val != ""){
    18                 //把当前节点后面的所有 font 兄弟节点删除
    19                 $this.nextAll("font").remove();
    20                 
    21                 var url="emp-validateLastName";
    22                 var args={"lastName":val,"time":new Date()};
    23                 $.post(url,args,function(data){
    24                     //姓名可用
    25                     if(data == "1"){
    26                         $this.after("<font color='green'>LastName可用!</font>");
    27                     }
    28                     //不可用
    29                     else if(data == "0"){
    30                         $this.after("<font color='red'>LastName不可用!</font>");                        
    31                     }
    32                     //服务器错误
    33                     else{
    34                         alert("服务器错误!");
    35                     }
    36                 });
    37             }else{
    38                 alert("lastName不能为空!");
    39                 $(this).val("");
    40                 //$(this).focus();
    41             }
    42         });
    43     })
    44 </script>
    45 </head>
    46 <body>
    47 
    48     <s:debug></s:debug>
    49     
    50     <h4>Employee Input Page</h4>
    51     
    52     <s:form action="emp-save" method="post">
    53         <s:if test="id != null">
    54             <s:textfield name="lastName" label="LastName" disabled="true"></s:textfield>
    55             <s:hidden name="id"></s:hidden>
    56             
    57             <%--通过隐藏域的方法
    58              <s:hidden name="lastName"></s:hidden>
    59             <s:hidden name="createTime"></s:hidden> --%>
    60         </s:if>
    61         
    62         <s:else>
    63             <s:textfield name="lastName" label="LastName"></s:textfield>
    64         </s:else>
    65         <s:textfield name="email" label="Email"></s:textfield>
    66         <s:textfield name="birth" label="Birth"></s:textfield>
    67         
    68         <s:select list="#request.departments"
    69                   listKey="id" listValue="departmentName" name="department.id"
    70                   label="Department"></s:select>
    71         <s:submit></s:submit>                  
    72     </s:form>
    73     
    74 </body>
    75 </html>
    View Code

          >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 <!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="scripts/jquery-1.7.2.js"></script>
    10 <script type="text/javascript">
    11     //删除, 使用 ajax 的方式
    12     $(function(){
    13         $(".delete").click(function(){
    14             var lastName=$(this).next(":input").val();
    15             var flag=confirm("是否要删除"+lastName+"的信息吗?");
    16             //确认删除, 使用 ajax 的方式
    17             if(flag){
    18                 //获取要删除的行
    19                 var $tr=$(this).parent().parent();
    20                 var url=this.href;
    21                 var args={"time":new Date()};
    22                 $.post(url,args,function(data){
    23                     //若 data 的返回值为 1, 则提示 删除成功, 且把当前行删除
    24                     if(data== "1"){
    25                         alert("删除成功!");
    26                         $tr.remove();
    27                     }else{
    28                         //若 data 的返回值不是 1, 提示删除失败. 
    29                         alert("删除失败!");
    30                     }
    31                 });
    32             }
    33             return false;
    34         });
    35     })
    36 </script>
    37 </head>
    38 <body>
    39     <h3>Employee List Page</h3>
    40     
    41     <s:if test="#request.employees == null ||#request.size()==0">
    42         没有员工信息
    43     </s:if>
    44     <s:else>
    45         <table border="1" cellpadding="10" cellspacing="0">
    46             <tr>
    47                 <td>ID</td>
    48                 <td>LASTNAME</td>
    49                 <td>EMAIL</td>
    50                 <td>BIRTH</td>
    51                 <td>CREATETIME</td>
    52                 <td>DEPARTMENT</td>
    53                 <td>DELETE</td>
    54                 <td>EDIT</td>
    55             </tr>
    56             <s:iterator value="#request.employees">
    57                 <tr>
    58                     <td>${id}</td>
    59                     <td>${lastName}</td>
    60                     <td>${email }</td>
    61                     <%-- 
    62                         <td>${birth}</td>
    63                         <td>${createTime}</td>
    64                      --%>
    65                      <!-- 格式化时间 -->
    66                     <td>
    67                         <s:date name="birth" format="yyyy-MM-dd"/>
    68                     </td>
    69                     <td>
    70                         <s:date name="createTime" format="yyyy-MM-dd hh:mm:ss"/>
    71                     </td>
    72                     
    73                     <td>${department.departmentName}</td>
    74                     <td>
    75                         <a href="emp-delete?id=${id }" class="delete">Delete</a>
    76                         <input type="hidden" value="${lastName}"/>
    77                     </td>
    78                     <td>
    79                         <a href="emp-input?id=${id }">Edit</a>
    80                     </td>
    81                 </tr>
    82             </s:iterator>
    83         </table>
    84     </s:else>
    85 </body>
    86 </html>
    View Code

        (2)web.xml

            
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     3     
     4     <!-- 配置spring配置文件.xml的名称和位置路径 -->
     5     <context-param>
     6         <param-name>contextConfigLocation</param-name>
     7         <param-value>classpath:applicationContext*.xml</param-value>
     8     </context-param>
     9     
    10     <!-- Bootstraps the root web application context before servlet initialization -->
    11     <listener>
    12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    13     </listener>
    14     
    15     <!-- 配置Struts的Filter -->
    16     <filter>
    17         <filter-name>struts2</filter-name>
    18         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    19     </filter>
    20     <filter-mapping>
    21         <filter-name>struts2</filter-name>
    22         <url-pattern>/*</url-pattern>
    23     </filter-mapping>
    24 
    25 </web-app>
    View Code

        (3)WebContent----index.jsp

            
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 
    11     <a href="emp-list">显示所有员工信息:List All Employees</a>
    12     <br><br><br>
    13     <a href="emp-input">添加员工向信息:Add Employees' Information</a> 
    14     
    15 </body>
    16 </html>
    View Code

      

  • 相关阅读:
    Eclipse下载
    RTT之AT命令组件
    RTT之shell
    RTT之内存管理及异常中断
    单片机的图形UI
    RTT学习
    RTT学习之线程管理
    C++学习笔记:
    运放
    关于KEIL下的图形化显示和输出问题
  • 原文地址:https://www.cnblogs.com/noaman/p/5889721.html
Copyright © 2011-2022 走看看