【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中的完全一樣,所以就重複了。