Practical for beginners: several methods for transferring jsp data to action in struts2

Source: Internet
Author: User

Practical for beginners: several methods for transferring jsp data to action in struts2

 

First give the struts. xml code:
 
      
   
    {1}_{2}.jsp
     
   
    User_toLogin.jsp
     
  
 

Method 1:

 

Import com. opensymphony. xwork2.ActionSupport;/*** Method 1: directly write attributes in the action, and then write the set and get Methods * 1. add an attribute in Action. The attribute name must correspond to the attribute name on jsp * 2. add the corresponding set method **/public class UserAction extends ActionSupport {private String name; private String pass; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPass () {return pass;} public void setPass (String pass) {this. pass = pass;} public String doLogin () {String forward = input; // System. out. println (name ++ pass); if (wepull. equalsIgnoreCase (name) & 123. login signorecase (pass) {return SUCCESS; // logon successful, return SUCCESS page} else {return forward; // Logon Failed, return logon again} public String toLogin () {return SUCCESS; // enter the logon page }}


 

Bytes ------------------------------------------------------------------------------------------------------------------------------

User_toLogin.jsp page:

 

 

 

######################################## ##################################

Method 2:

 

Import com. opensymphony. xwork2.ActionSupport;/*** Method 2: encapsulate attributes in dto, and use the dto name when passing values. attribute * 1. add an attribute in Action. The attribute name must correspond to the attribute name on jsp * 2. add the corresponding set method **/public class UserAction extends ActionSupport {private UserDTO user; public UserDTO getUser () {return user;} public void setUser (UserDTO user) {this. user = user;} public String doLogin () {String forward = input; // System. out. println (user. getName () ++ user. getPass (); if (wepull. equalsIgnoreCase (user. getName () & 123. equalsIgnoreCase (user. getPass () {return SUCCESS;} else {return forward;} public String toLogin () {return SUCCESS;} login UserDTO. java code public class UserDTO {private String id; private String name; private String pass; public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPass () {return pass;} public void setPass (String pass) {this. pass = pass ;}}


 

Bytes ------------------------------------------------------------------------------------------------------------------------------

User_toLogin.jsp page:

 

 

 

######################################## ##################################

Method 3:

 

Import com. opensymphony. xwork2.ActionSupport; import com. opensymphony. xwork2.ModelDriven;/*** method 3: Implement ModelDriven * to implement the ModelDriven interface **/public class UserAction extends ActionSupport implements ModelDriven
 
  
{Private UserDTO user; public UserDTO getUser () {return user;} public void setUser (UserDTO user) {this. user = user;} public String doLogin () {String forward = input; // System. out. println (user. getName () ++ user. getPass (); if (wepull. equalsIgnoreCase (user. getName () & 123. equalsIgnoreCase (user. getPass () {return SUCCESS;} else {return forward;} public String toLogin () {return SUCCESS;} public UserDTO getModel () {if (user = null) {user = new UserDTO ();} return user ;}}
 


 

Bytes ------------------------------------------------------------------------------------------------------------------------------

User_toLogin.jsp page:

 

 

 

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&

In addition, we will introduce a method for passing parameters through HttpServletRequest request.

 

Import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpSession; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionSupport; import com. opensymphony. xwork2.ModelDriven;/*** method of obtaining request Parameters * HttpServletRequest request parameter passing method **/public class UserAction extends ActionSupport {public String doLogin () {// obtain the request object HttpServletRequest request = ServletActionContext. getRequest (); // get the session object // HttpSession session = request. getSession (); String name = request. getParameter (name); String pass = request. getParameter (pass); String forward = input; if (wepull. equalsIgnoreCase (name) & 123. inclusignorecase (pass) {return SUCCESS;} else {return forward;} public String toLogin () {return SUCCESS ;}}


 

DTO and ModelDriven are the most common features in development.


 

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.