Interaction between jsp and struts action

Source: Internet
Author: User

1. the jsp sends a value to the action. The jsp sends the value to the action.

1. form submission method

2. href Method

"> Delete


Ii. Pass a value to the action in jsp. method accepted by the action


1. Define form attributes in the Action class. The two attributes must have the same name. Provides the setter and getter methods. You can receive the parameter. private String username passed in the form;
Private String password;


Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = username;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}

2. encapsulate the parameters passed in the form into a class, and then call the attributes. For example, encapsulate the parameters to be sent from the login. jsp page (for example, encapsulated in the Users class)

Private String username;
Private String password;

Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = username;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}

Then define the object of the class in the Action method, as shown in figure

Public class loginAction extends ActionSupport {
Private Users users;
Public Users getUsers (){
Return users;
}
Public void setUsers (Users users ){
This. users = users;
}
}

To pass the value through this method, you must also perform some processing on the jsp page. The from1 attribute name in login. jsp should be changed to this:

Login form login. jsp:



This method is a common method in struts development!
3. Implement the ModelDriven interface to receive form data
First, the Action class must implement the ModelDriven interface and encapsulate the data transmitted from the form. The Action class must instantiate the object and override the getModel () method.
Public class loginAction extends ActionSupport implementsModelDriven {
Private Users users = new Users ();
Public Users getModel (){
Return users;
}
} 3. The action is returned to jsp. jsp accepts method 1. In the Action, you can call the setAttirbute method of the session or request object, and then retrieve it from jsp. In the java source file of action, write ActionContext ac = ActionContext. getContext ();
Map session = ac. getSession ();
Session. put ("currentUser", user); On the jsp page, you can access the user User user = (User) session through the session. getAttribute ("currentUser"); 2. OGNL (
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.