Interaction between jsp and struts and action

Source: Internet
Author: User

Interaction between jsp and struts and action
Abstract: Interaction Between jsp and struts and action = 1. Pass a value from jsp to action, method 1 sent by jsp, method jsp submitted by form and interaction between struts and action = 1. jsp transfers values to action, method 1 of jsp sending: method 1 of form submission <form action = "login" method = "post" name = "form1"> Username: <s: textfieldname = "username"/> <br/> password: <s: passwordname = "password"/> <br/> <s: submit value = "submit"/> </form> 2. href method <a href = "messageDelete. action? Id = <% = msg. getId () %> "> Delete </a> = 2. Pass a value to the action in jsp. method 1 accepted by the action. define form attributes in the Action class. The two attributes must have the same name. Provides the setter and getter methods. You can receive parameters passed in the form. 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;} 2. encapsulate the parameters passed in the form into a class and call the attributes. for example. private String username; pr Ivate 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 this class in the Action method, for example, public class loginAction extends ActionSupport {private Users users; public Users getUsers () {return users ;} Public void setUsers (Users users) {this. users = users;} in this way, you must also perform some processing on the jsp page, login. in jsp, The from1 attribute name should be changed to the following: <form action = "login" method = "post" name = "form1"> Username: <s: textfieldname = "users. username "/> <br/> password: <s: passwordname =" users. password "/> <br/> <s: submit value =" submit "/> </form> is a common method in struts development! 3. to receive form data through the ModelDriven interface, the Action class must first implement the ModelDriven interface and encapsulate the data transmitted from the form. The Action class must instantiate the object and override getModel () method public class loginAction extends ActionSupport implements ModelDriven <Users> {private Users users = new Users (); public Users getModel () {return users ;}} my suggestion is to use ModelDriven to apply the entire application layer. Then, ModelDriven <Model> defines a generic type, and all entities can be applied. For example, public class BaseAction extends ActionSupport implements ModelDriven <Model> {private Model model = new Model (); public Model getModel () {return model ;}} = 3. The action is returned to jsp. the jsp method is accepted. 1. In the Action, you can call the setAttirbute method of the session or request object, and then retrieve it from jsp. Write ActionContext ac = ActionContext in the java source file of action. 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 is a very effective data processing tool introduced by XWork. We have learned about the basic operations of OGNL and the internal structure of OGNL. Next, let's take a look at how XWork has strengthened OGNL and how the OGNL system works in Struts2.

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.