首页 > 代码库 > SSH_框架整合2—查询显示

SSH_框架整合2—查询显示

4. 完成功能. 

(1)com.atguigu.ssh.actions包下新建EmployeeAction.java 

  技术分享
 1 package com.atguigu.ssh.actions; 2  3 import java.util.Map; 4  5 import org.apache.struts2.interceptor.RequestAware; 6  7 import com.atguigu.ssh.service.EmployeeService; 8 import com.opensymphony.xwork2.ActionSupport; 9 10 public class EmployeeAction extends ActionSupport implements RequestAware{11     /**12      * 13      */14     private static final long serialVersionUID = 1L;15     16     private EmployeeService employeeService;17     18     public void setEmployeeService(EmployeeService employeeService){19         this.employeeService=employeeService;20     }21     22     public String list(){23         request.put("employees", employeeService.getAll()); 24         return "list";25     }26 27     //放到页面里28     private Map<String,Object> request;29     @Override30     public void setRequest(Map<String, Object> arg0) {31         this.request=arg0;32     }33 34 }
View Code

  com.atguigu.ssh.dao包下新建EmployeeDao.java

  技术分享
 1 package com.atguigu.ssh.dao; 2  3 import java.util.List; 4  5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7  8 import com.atguigu.ssh.entities.Employee; 9 10 public class EmployeeDao {11     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     public List<Employee> getAll(){23         String hql="FROM Employee  e LEFT OUTER JOIN FETCH e.department";24         return getSession().createQuery(hql).list();25     }26 }
View Code

  com.atguigu.ssh.service包下新建EmployeeService.java 

  技术分享
 1 package com.atguigu.ssh.service; 2  3 import java.util.List; 4  5 import com.atguigu.ssh.dao.EmployeeDao; 6 import com.atguigu.ssh.entities.Employee; 7  8 public class EmployeeService { 9     10     private EmployeeDao employeeDao;11     public void setEmployeeDao(EmployeeDao employeeDao){12         this.employeeDao=employeeDao;13     }14     15     public List<Employee> getAll(){16         List<Employee> employees = employeeDao.getAll();17         return employees;18     }19 }
View Code

(2)完善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="employeeService" class="com.atguigu.ssh.service.EmployeeService">11         <property name="employeeDao" ref="employeeDao"></property>12     </bean>13     14     <bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction"15           scope="prototype">16         <property name="employeeService" ref="employeeService"></property>17     </bean>18 </beans>
View Code

(3)完善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         <action name="emp-*" class="employeeAction"13                 method="{1}">14                 <result name="list">/WEB-INF/views/emp-list.jsp</result>15         </action>16     </package>17 18 </struts>
View Code

(4)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     <a href="emp-list">显示所有员工信息List All Employees</a>11 </body>12 </html>
View Code

(5)WebContent-views-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 </head>10 <body>11     <h3>Employee List Page</h3>12     13     <s:if test="#request.employees == null ||#request.size()==0">14         没有员工信息15     </s:if>16     <s:else>17         <table border="1" cellpadding="10" cellspacing="0">18             <tr>19                 <td>ID</td>20                 <td>LASTNAME</td>21                 <td>EMAIL</td>22                 <td>BIRTH</td>23                 <td>CREATETIME</td>24                 <td>DEPT</td>25             </tr>26             <s:iterator value="#request.employees">27                 <tr>28                     <td>${id}</td>29                     <td>${lastName}</td>30                     <td>${email }</td>31                     <td>${birth}</td>32                     <td>${createTime}</td>33                     <td>${department.departmentName}</td>34                 </tr>35             </s:iterator>36         </table>37     </s:else>38 </body>39 </html>
View Code

(6)修改下web.xml中的信息

技术分享
1 <context-param>2         <param-name>contextConfigLocation</param-name>3         <param-value>classpath:applicationContext*.xml</param-value>4     </context-param>
View Code

SSH_框架整合2;

技术分享

SSH_框架整合2—查询显示