简体   繁体   中英

How the data transfer from the action class to Jsp page in struts2

How the data transfer from the action class to Jsp page in struts2. Is any kind of stack is build inside the action class before its transfer its control to the jsp page , or there is something other mechanism is used for that.

Anshul,

You are some what correct in your implementation.For Struts2 we can see it in following manners

  1. ValueStack
  2. OGNL

Value stack is a virtual stack in Struts2 framework and a central place where all the data related to request processing will be placed by the Framework.In Struts2 Actions are also treated as a data carrier, so what exactly is happening is when the action done its work it is being placed on the top of value stack.

Lets we have something like this in Action

public class MyAction extends ActionSupport{

  private String firstname;
  private String lastname;

  public void setFirstName(firstname){
     this.firstname=firstname
  }

 public void setLastName(lastname){
     this.lastname=lastname
  }

public String getFirstName(){
   return firstname;
}

public String geLastName(){
   return lastname;
}

 public String execute() throws Exception{
             //action logic filling first name and last name from database etc.
             return SUCCESS;
 }
}

now when action will return after the logic framework will place action instance on the top of value stack and firstname and lastname will be on the top of it.

so now when we try to access these values in jsp something like
<s:textfield name="firstname" value= "%{firstname}"/>

here (%{firstname}) OGNL will come in to place and it will try to see if there is a property in the value stack with name firstname ,since action is on the top of value stack and it has property name firstname in it so OGNL will find it.

i hope this will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM