【SSH】struts2的Action中的属性,不必再次put到ActionContext域中

注意,首先要确保是说的属性,请看这个案例:

package indi.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import indi.entity.Customer;
import indi.service.CustomerService;

@SuppressWarnings("serial")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
	private Customer customer=new Customer();
	//通过spring注入
	private CustomerService cs;
	//接收请求的cust_id值
	//private String cust_id;
	
	public String find() throws Exception {
		//1.根据从页面获得来到id值取数据库中查找对应的customer对象
		customer=cs.findCustomerById(customer.getCust_id().toString());
		//不必再次放入ActionContext中,customer因为含有getset方法,所以customer是ActionContext的属性值
		//ActionContext.getContext().put("customer", customer);
		//2.直接将结果转发至info.jsp
		return "display";
	}

	/*public String getCust_id() {
		return cust_id;
	}

	public void setCust_id(String cust_id) {
		this.cust_id = cust_id;
	}*/

	public void setCs(CustomerService cs) {
		this.cs = cs;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	@Override
	public Customer getModel() {
		
		return customer;
	}
	
}

前端通过这个url访问:http://localhost:8080/ssh_crm_maven/customerAction_find?cust_id=1。

一、采用模型驱动,并给对象设置getset方法,后台的执行过程如下:

1、因为action接收参数采用的是模型驱动,此例中我们设置了getset方法(模型驱动一般对应接收参数的对象不设置setget方法,此处只是做一个实验),所以customer对象首先会被压入valuestack的栈顶,然后接收cust_id参数。

2、之后后台再执行action,然后action并没有被压入valuestack的栈顶,但是依然被压入栈中,又因为customer对象同时又是CustomerAction的属性,所以相当于customer也被压入了栈中,如下图所示,所以前端通过${customer.cust_name}直接可以获取到从数据库中返回回来的值。如下图所示:


但是,依然不建议给模型驱动的对象设置get和set方法,模型驱动的时候还是将其放入ActionContext好,一般都是这样做的。

其java代码演示如下:

package indi.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import indi.entity.Customer;
import indi.service.CustomerService;

@SuppressWarnings("serial")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
	private Customer customer=new Customer();
	//通过spring注入
	private CustomerService cs;
	
	public String find() throws Exception {
		//1.根据从页面获得来到id值取数据库中查找对应的customer对象
		customer=cs.findCustomerById(customer.getCust_id().toString());
		ActionContext.getContext().put("customer", customer);
		//2.直接将结果转发至info.jsp
		return "display";
	}


	public void setCs(CustomerService cs) {
		this.cs = cs;
	}


	@Override
	public Customer getModel() {
		
		return customer;
	}

}

ValueStack中的栈的情况如下:


Context的情况如下:在ActionContext域中多了一个customer键值对,也是可以通过${customer.cust_name}直接获取的。


二、如果不采用模型驱动而采用属性驱动CustomerAction直接继承的ActionSupport,然后action同样有一个属性customer,那么在前端取值,同样也不需要把customer对象put入ActionContext域中,因为这个时候,CustomerAction被压入栈顶,它有个属性值customer,前台通过${customer.cust_name}直接可以获取到从数据库中返回回来的值,如果一定要put入ActionContext域中,那么也就是在valuestack的congtext(即:ActionContext)中多了一个customer键值对,而且这个键值对的对象和值都与栈中的完全一样,所以就没必要put入ActionContext域,可在前台直接获得。相关代码和值栈的情况如下:

package indi.web.action;

import com.opensymphony.xwork2.ActionSupport;
import indi.entity.Customer;
import indi.service.CustomerService;

@SuppressWarnings("serial")
public class CustomerAction extends ActionSupport{
	private Customer customer=new Customer();
	//通过spring注入
	private CustomerService cs;
	//接收请求的cust_id值
	private String cust_id;
	
	public String find() throws Exception {
		//1.根据从页面获得来到id值取数据库中查找对应的customer对象
		customer=cs.findCustomerById(customer.getCust_id().toString());
		//不必再次放入ActionContext中,customer因为含有getset方法,所以customer是ActionContext的属性值,所以在执行Action的时候,CustomerAction会被顶入valuestack的栈顶
		//ActionContext.getContext().put("customer", customer);
		//2.直接将结果转发至info.jsp
		return "display";
	}

	public String getCust_id() {
		return cust_id;
	}

	public void setCust_id(String cust_id) {
		this.cust_id = cust_id;
	}

	public void setCs(CustomerService cs) {
		this.cs = cs;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

}

上面的是栈中的情况,Action被顶入栈中。如果我们把customer放入ActionContext也只是会在context中多一个customer键值对而已,而这个键值对的值和CustomerAction中的完全一样,所以就重复了。


猜你喜欢

转载自blog.csdn.net/Topdandan/article/details/80285926