Struts2入门第九讲——优化CRM系统中客户列表显示的功能

在Struts2入门的第二讲中,我们就已经完成了CRM系统中客户列表显示的功能,但是那个时候,我们是将查询出来的List集合存放在了request域中,然后再带到页面中显示。现在,咱可以对该功能进行一个优化,将查询出来的List集合存入到值栈中,然后再带到页面中显示。
首先,我们需要修改CustomerAction类的代码,将查询出来的List集合存入到值栈中。

package com.meimeixia.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.meimeixia.domain.Customer;
import com.meimeixia.service.CustomerService;
import com.meimeixia.service.impl.CustomerSeviceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 客户管理的Action
 * @author liayun
 *
 */
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	
	//模型驱动使用的对象
	private Customer customer = new Customer();
	
	@Override
	public Customer getModel() {
		return customer;
	}

	//查询客户列表的方法
	public String find() {
		//调用业务层
		CustomerService customerService = new CustomerSeviceImpl();
		List<Customer> list = customerService.find();
		//需要带到页面上,即页面跳转
		//ServletActionContext.getRequest().setAttribute("list", list);
		
		//将查询到的List集合存入到值栈中
		ActionContext.getContext().getValueStack().set("list", list);
		
		return "findSuccess";
	}
	
	//跳转到添加客户的页面
	public String saveUI() {
		return "saveUI";
	}
	
	//保存客户的方法
	public String save() {
		//接收数据
		//封装数据
		
		//调用业务层
		CustomerService customerService = new CustomerSeviceImpl();
		customerService.save(customer);
		
		//页面跳转
		return "saveSuccess";
	}
}

然后,在jsp/customer目录下的list.jsp页面中通过Struts2标签和OGNL表达式获取值栈里面的List集合中的数据。
Struts2入门第九讲——优化CRM系统中客户列表显示的功能_第1张图片
最后,发布我们的项目到Tomcat服务器并启动,并访问该项目的首页,点击客户列表超链接,就能显示一系列客户的信息了。
Struts2入门第九讲——优化CRM系统中客户列表显示的功能_第2张图片

你可能感兴趣的:(Struts2框架,Struts2框架学习)