SSH_框架整合2—查询显示
4. 完成功能.
(1)com.atguigu.ssh.actions包下新建EmployeeAction.java
package com.atguigu.ssh.actions; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import com.atguigu.ssh.service.EmployeeService;
import com.opensymphony.xwork2.ActionSupport; public class EmployeeAction extends ActionSupport implements RequestAware{
/**
*
*/
private static final long serialVersionUID = 1L; private EmployeeService employeeService; public void setEmployeeService(EmployeeService employeeService){
this.employeeService=employeeService;
} public String list(){
request.put("employees", employeeService.getAll());
return "list";
} //放到页面里
private Map<String,Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
} }
com.atguigu.ssh.dao包下新建EmployeeDao.java
package com.atguigu.ssh.dao; import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.atguigu.ssh.entities.Employee; public class EmployeeDao { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
} public Session getSession(){
return this.sessionFactory.getCurrentSession();
} public List<Employee> getAll(){
String hql="FROM Employee e LEFT OUTER JOIN FETCH e.department";
return getSession().createQuery(hql).list();
}
}
com.atguigu.ssh.service包下新建EmployeeService.java
package com.atguigu.ssh.service; import java.util.List; import com.atguigu.ssh.dao.EmployeeDao;
import com.atguigu.ssh.entities.Employee; public class EmployeeService { private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao){
this.employeeDao=employeeDao;
} public List<Employee> getAll(){
List<Employee> employees = employeeDao.getAll();
return employees;
}
}
(2)完善applicationContext-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="employeeDao" class="com.atguigu.ssh.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="employeeService" class="com.atguigu.ssh.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean> <bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction"
scope="prototype">
<property name="employeeService" ref="employeeService"></property>
</bean>
</beans>
(3)完善struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<action name="emp-*" class="employeeAction"
method="{1}">
<result name="list">/WEB-INF/views/emp-list.jsp</result>
</action>
</package> </struts>
(4)WebContent下新建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="emp-list">显示所有员工信息List All Employees</a>
</body>
</html>
(5)WebContent-views-emp-list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Employee List Page</h3> <s:if test="#request.employees == null ||#request.size()==0">
没有员工信息
</s:if>
<s:else>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>ID</td>
<td>LASTNAME</td>
<td>EMAIL</td>
<td>BIRTH</td>
<td>CREATETIME</td>
<td>DEPT</td>
</tr>
<s:iterator value="#request.employees">
<tr>
<td>${id}</td>
<td>${lastName}</td>
<td>${email }</td>
<td>${birth}</td>
<td>${createTime}</td>
<td>${department.departmentName}</td>
</tr>
</s:iterator>
</table>
</s:else>
</body>
</html>
(6)修改下web.xml中的信息,注册Struts2
<?xml version="1.0" encoding="UTF-8"?>
<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"> <!-- 配置spring配置文件.xml的名称和位置路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置Struts的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
SSH_框架整合2;
SSH_框架整合2—查询显示的更多相关文章
- SSH_框架整合4--添加员工信息
SSH_框架整合4--添加员工信息 一. 1 index.jsp:添加:<a href="emp-input">添加员工向信息:Add Employees' Infor ...
- SSH_框架整合6--修改Edit员工信息
SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...
- SSH_框架整合5--验证用户名是否可用
SSH_框架整合5--验证用户名是否可用 1 emp-input.jsp中编写ajax验证用户名是否可用: <script type="text/javascript" SR ...
- SSH_框架整合1
1 WEB环境下配置Spring 因为是在WEB环境中应用Spring,所以要先配置web.xml: (1)WebContent-WEB-INF-lib包中,加入Spring包下的required ...
- SSH_框架整合7--整个项目CODE
一 架构 1Action类 2 配置文件 3 View页面 二 Code 1 src (1)com.atguigu.ssh.actions >EmployeeAction.java packa ...
- SSH_框架整合3-删除
一.普通删除 1 完善src中 类: (1)EmployeeDao.java中: //2 删除 public void delete(Integer id){ String hql="DEL ...
- Mybatis框架-联表查询显示问题解决
需求:查询结果要求显示用户名,用户密码,用户的角色 因为在用户表中只有用户角色码值,没有对应的名称,角色名称是在码表smbms_role表中,这时我们就需要联表查询了. 这里需要在User实体类中添加 ...
- 商城02——dubbo框架整合_商品列表查询实现_分页
1. 课程计划 1.服务中间件dubbo 2.SSM框架整合. 3.测试使用dubbo 4.后台系统商品列表查询功能实现. 5.监控中心的搭建 2. 功能分析 2.1. 后台系统所用的技术 框 ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
随机推荐
- 安装完openfire之后打不开的解决方案
今天在http://www.igniterealtime.org/downloads/index.jsp下载了一个最新openfire for mac版 在系统的偏好设置里面是这样的.那个open A ...
- Mac中Fn键技巧
此文适用:Mac自带苹果键盘或外接有Fn键的普通键盘 上一页=Fn+⬅️ 下一页=Fn+➡️ 向上卷动=Fn+⬆️ 向下卷动=Fn+⬇️
- Windows 10触摸板手势
高級使用者試用 Windows 10 筆記本電腦的觸控板上的這些手勢: •選擇一項: 在觸控板上點擊. •滾動: 將兩根手指放在觸控板上,然後以水準或垂直方向滑動. •放大或縮小: 將兩根手指放在觸控 ...
- Post和get乱码
post 在web.xml中添加 <filter> <filter-name>CharacterEncodingFilter</filter-name> <f ...
- Oracle字符集的查看查询和Oracle字符集的设置修改
本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的oracle utf8字符集和oracle exp 字符集问题. 一.什么是Oracle字符集 Oracle字符集是 ...
- Eclipse中web-inf和meta-inf文件夹的信息
http://www.cnblogs.com/chinafine/archive/2010/06/13/1757514.html WEB-INF /WEB-INF/web.xml ...
- Amazon-countDuplicate
Print the count of duplicate char in a given string in same order. Ex: Input- 'abbaccdbac', Output- ...
- C++编程开发学习的50条建议(转)
每个从事C++开发的朋友相信都能给后来者一些建议,但是真正为此进行大致总结的很少.本文就给出了网上流传的对C++编程开发学习的50条建议,总结的还是相当不错的,编程学习者(不仅限于C++学习者)如果真 ...
- acess the C\c++ from the Java
https://en.wikipedia.org/wiki/Java_Native_Interface http://docs.oracle.com/javase/7/docs/technotes/g ...
- Linux-内存管理机制、内存监控、buffer/cache异同
在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,主要特点是,无论物理内存有多大,Linux 都将其充份利用,将 ...