Struts完成客户列表显示 所用的基础知识 在之前的随笔中已经讲过。这篇是介绍如何使用Struts 完成客户列表显示  。

下面是  完成的代码执行逻辑图:

抽取项目部分代码 相信大家认真看一遍就明白的:

jsp 页面代码片段:

<TABLE cellSpacing=0 cellPadding=3 width=165 align=center border=0>
<TBODY>
<TR>
<TD class=mainMenu onClick="MenuDisplay('table_1');"><SPAN
class=span id=table_1Span>+</SPAN> 客户管理</TD>
</TR>
<TR>
<TD>
<TABLE id=table_1 style="DISPLAY: none" cellSpacing=0
cellPadding=2 width=155 align=center border=0>
<TBODY>
<TR>
<TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/jsp/customer/add.jsp"
target=main>- 新增客户</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/CustomerAction_list"
target=main>- 客户列表</A></TD>
</TR> </TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD background=images/new_027.jpg height=1></TD>
</TR>
<TR>
<TD class=mainMenu onClick="MenuDisplay('table_2');"><SPAN
class=span id=table_2Span>+</SPAN> 联系人管理</TD>
</TR>
<TR>
<TD>
<TABLE id=table_2 style="DISPLAY: none" cellSpacing=0
cellPadding=2 width=155 align=center border=0>
<TBODY>
<TR>
<TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/jsp/linkman/add.jsp"
target=main>- 新增联系人</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="linkmanServlet?method=list"
target=main>-联系人列表</A></TD>
</TR> </TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD background=images/new_027.jpg height=1></TD>
</TR>
<TR>
<TD class=mainMenu onClick="MenuDisplay('table_5');"><SPAN
class=span id=table_5Span>+</SPAN> 客户拜访管理</TD>
</TR>
<TR>
<TD>
<TABLE id=table_5 style="DISPLAY: none" cellSpacing=0
cellPadding=2 width=155 align=center border=0>
<TBODY>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-新增客户拜访</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-客户拜访列表</A></TD>
</TR> </TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD background=images/new_027.jpg height=1></TD>
</TR>
<TR>
<TD class=mainMenu onClick="MenuDisplay('table_3');"><SPAN
class=span id=table_3Span>+</SPAN> 综合查询</TD>
</TR>
<TR>
<TD>
<TABLE id=table_3 style="DISPLAY: none" cellSpacing=0
cellPadding=2 width=155 align=center border=0>
<TBODY>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>- 客户信息查询</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>- 联系人信息查询</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>- 客户拜访记录查询</A></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD background=images/new_027.jpg height=1></TD>
</TR>
<TR>
<TD class=mainMenu onClick="MenuDisplay('table_4');"><SPAN
class=span id=table_4Span>+</SPAN> 统计分析</TD>
</TR>
<TR>
<TD>
<TABLE id=table_4 style="DISPLAY: none" cellSpacing=0
cellPadding=2 width=155 align=center border=0>
<TBODY>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-客户行业统计</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-客户来源统计</A></TD>
</TR> </TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD background=images/new_027.jpg height=1></TD>
</TR>
<TR>
<TD class=mainMenu onClick="MenuDisplay('table_6');"><SPAN
class=span id=table_6Span>+</SPAN>系统管理</TD>
</TR>
<TR>
<TD>
<TABLE id=table_6 style="DISPLAY: none" cellSpacing=0
cellPadding=2 width=155 align=center border=0>
<TBODY>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-角色管理</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-用户管理</A></TD>
</TR>
<TR>
<TD class=menuSmall><A class=style2 href="#"
target=main>-数据字典</A></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>

Action类 代码:

 public class CustomerAction extends ActionSupport {
private CustomerService cs = new CustomerServiceImpl(); public String list() throws Exception {
//1 接受参数
String cust_name = ServletActionContext.getRequest().getParameter("cust_name");
//2 创建离线查询对象
DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);
//3 判断参数拼装条件
//StringUtils.isNotBlank 静态方法 判断
//当cust_name 不为null 不为 ""(空字符串) ,不为空格时 返回true
if(StringUtils.isNotBlank(cust_name)){
dc.add(Restrictions.like("cust_name", "%"+cust_name+"%"));
}
//4 调用Service将离线对象传递
List<Customer> list = cs.getAll(dc);
//5 将返回的list放入request域.转发到list.jsp显示
//引用ServletActionContext类的静态方法getRequest
//getRequest返回HttpServletRequest对象
ServletActionContext.getRequest().setAttribute("list", list); return "list";
}
}

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>
<!-- 指定struts2是否以开发模式运行
1.热加载主配置.(不需要重启即可生效)
2.提供更多错误信息输出,方便开发时的调试
-->
<constant name="struts.devMode" value="true"></constant>
<package name="crm" namespace="/" extends="struts-default" >
<action name="CustomerAction_*" class="cn.itheima.web.action.CustomerAction" method="{1}" >
<result name="list" >/jsp/customer/list.jsp</result>
</action>
</package>
</struts>

ServiceImpl 类

 import java.util.List;

 import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria; import cn.itheima.dao.CustomerDao;
import cn.itheima.dao.impl.CustomerDaoImpl;
import cn.itheima.domain.Customer;
import cn.itheima.service.CustomerService;
import cn.itheima.utils.HibernateUtils; public class CustomerServiceImpl implements CustomerService { private CustomerDao customerDao = new CustomerDaoImpl(); public List<Customer> getAll() {
Session session = HibernateUtils.getCurrentSession();
//打开事务
Transaction tx = session.beginTransaction(); List<Customer> list = customerDao.getAll(); //关闭事务
tx.commit();
return list;
} public List<Customer> getAll(DetachedCriteria dc) {
Session session = HibernateUtils.getCurrentSession();
//打开事务
Transaction tx = session.beginTransaction(); List<Customer> list = customerDao.getAll(dc); //关闭事务
tx.commit();
return list;
} }

dao层实现类:

 import java.util.List;

 import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria; import cn.itheima.dao.CustomerDao;
import cn.itheima.domain.Customer;
import cn.itheima.utils.HibernateUtils; public class CustomerDaoImpl implements CustomerDao {
public List<Customer> getAll() {
//1 获得session
Session session = HibernateUtils.getCurrentSession();
//2 创建Criteria对象
Criteria c = session.createCriteria(Customer.class);
return c.list();
}
}

Struts2(六) 用Struts完成客户列表显示的更多相关文章

  1. 转载 Struts2的配置 struts.xml Action详解

    在学习struts的时候,我们一定要掌握struts2的工作原理.只有当我们明确了在struts2框架的内部架构的实现过程,在配置整个struts 的框架时,可以很好的进行逻辑上的配置.接下来我就先简 ...

  2. struts2学习笔记--struts.xml配置文件详解

    这一节主要讲解struts2里面的struts.xml的常用标签及作用: 解决乱码问题 <constant name="struts.i18n.encoding" value ...

  3. Struts2学习笔记--Struts例子及开发流程

    参考资料:http://blog.csdn.net/hntyzgn2010/article/details/5547753 http://chenlh.iteye.com/blog/464341 入门 ...

  4. struts2.0中struts.xml配置文件详解

    先来展示一个配置文件 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration ...

  5. 1-1 struts2 基本配置 struts.xml配置文件详解

    详见http://www.cnblogs.com/dooor/p/5323716.html 一. struts2工作原理(网友总结,千遍一律) 1 客户端初始化一个指向Servlet容器(例如Tomc ...

  6. struts2之配置文件struts.xml详解

    struts配置文件 struts.xml配置参数详解 struts.xml中很大一部分配置默认配置就好了 但是有些还是需要做了解  以便于理解 和修改 <?xml version=" ...

  7. Struts2学习:struts.xml引入自定义的xml文件

    随着项目代码的增多,用一个struts.xml来管理所有功能模块的Action未免显得臃肿且结构不清晰,因此可以根据实际的功能划分,将各模块的Action放在自定义的xml文件中,再引入struts. ...

  8. Struts2(六)result

    一.result简述 result:输出结果:第个Action返回一个字符串,Struts2根据这个值来决定响应结果 name属性:result的逻辑名.和Actin里的返回值匹配,默认"s ...

  9. Struts2(六.用标签显示用户列表及Value Stack和Stack Context)

    一.用Struts2标签显示用户列表 原理: 在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问 原则: 在jsp网页上,尽量不要出现 ...

随机推荐

  1. ABP官方文档翻译 7.3 Quartz集成

    Quartz集成 介绍 安装 创建Jobs 计划安排Jobs 更多 介绍 Quartz是一个全功能的.开源的job计划安排系统,可以用在小的apps也可以用于大型的企业系统.Abp.Quartz包简化 ...

  2. SpringMVC源码情操陶冶-DispatcherServlet父类简析

    阅读源码有助于陶冶情操,本文对springmvc作个简单的向导 springmvc-web.xml配置 <servlet> <servlet-name>dispatch< ...

  3. 洛谷 [P2761] 软件补丁问题

    并不是网络流 状压+SPFA 通过题目中的描述及数据范围可知,我们状压当前的漏洞,以每个二进制位表示是否有这个漏洞,并以状压的结果为顶点,以补丁的时间为边跑SPFA即可 #include <io ...

  4. 小甲鱼OD学习第8讲

    这次我们的任务是破解这个有日期限制的软件 我们可以看到,这个一个有日期限制的软件,如图所示 首先,我们把程序载入OD,我们从字符串搜索入手,选择  所有参考文本字串,如图 我们输入相应的字符串尝试搜索 ...

  5. ssh: Could not resolve hostname git.*****-inc.com : Temporary failure in name resolution fatal: The remote end hung up unexpectedly

    问题出现的情景:使用git pull拉取开发的代码到测试服务器,报错: ssh: Could not resolve hostname git.****-inc.com : Temporary fai ...

  6. mysql必知必会

    春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ...

  7. Java堆栈内存总结

    在Java中,主要存在四块内存空间,除了保存static类型属性的全局数据区,以及保存虽有方法定义的全局代码区之外,程序员更多的在乎内存中的另外两种区域--对象的生存空间堆(heap)和方法调用及变量 ...

  8. 免费 Https 证书(Let's Encrypt)申请与配置

    之前要申请免费的 https 证书操作步骤相当麻烦,今天看到有人在讨论,就搜索了一下.发现现在申请步骤简单多了. 1. 下载 certbot git clone https://github.com/ ...

  9. 【技术干货】git常用命令

    2.1 git init语法: git init在当前目录初始化git仓库,适用于尚未使用git管理的项目2.2 git clone语法: git clone <url>例如: git c ...

  10. yml 文件操作方法

    文件读取方法示例: import yaml fr = open('yml_file_address', 'r',encoding='utf-8') data = yaml.load(fr) print ...