Struts2 to pass parameters between the action and the JSP __JS

Source: Internet
Author: User


1, the browser to the action pass parameters:

set the property in action and set the Get/set method for the property. Assign a value by the name of the property when passing the parameter. such as Xxx?name=aa

value with Request.getprameters ("property name");

public class Useraction {  
    private String name;  
    private user user;  
    Public String Useradd () {  
        System.out.print (User.getname ());  
        System.out.print (name);  
        Return "Success";  
    }  
    Public String GetName () {return  
        name;  
    }  
    public void SetName (String name) {  
        this.name = name;  
    }  
    Public User GetUser () {return  
        user;  
    }  
    public void SetUser (user user) {  
        this.user = user  
    }  
}  

Note: Struts2 does not use the single case mode, so each request is a new object.


2, Action to the browser interface pass parameters:

There are three kinds of ways:

1) Species: directly to the action attribute (there is a Get/set method) assignment, JSP in <s:property value= "OGNL expression"/> Value, note that the return result type is forward

For example: <s:property value= "name"/> <s:property value= "User.Name"/>

2: Pass the value by Actioncontext, add in the method called in the action:

Actioncontext.getcontext (). Put ("Key", "value");

<s:property value= "#key"/> value in JSP


3) Kinds: Through request, session pass value. The action method sets the value by obtaining the HttpServletRequest, HttpSession, and map objects.

For example:

Servletactioncontext.getrequest (). setattribute ("arg0", "value"); Servletactioncontext.getrequest (). GetSession (). setattribute ("arg0", "value"); Actioncontext.getcontext (). GetSession (). Put ("Key", "value");

JSP page through:


"Transferred from" http://cailangwei.blog.163.com/blog/static/131458579201287112552485/


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.