Struts2 the action to pass a value to the page

Source: Internet
Author: User

Write an action class:
public class Loginaction{public String execute () {return SUCCESS;} public void SetValue (Somebean value) {This.value=value;} Public Somebean GetValue () {return this.value;} private Somebean value;}

  

Write The Bean class again:
public class Somebean{public String GetName () {}public void SetName (string name) {}}

  

Method One:
Use an OGNL expression. You can use the tag that comes with struts, and they all support OGNL, such as S:property. As an example:
<!--test.jsp--><%@ page contenttype= "Text/html;charset=utf-8" pageencoding= "Utf-8"%><%@ taglib Prefix= "S" uri= "/struts-tags"%><s:property value= "Value.name"/>
When you visit loginaction, the S:property tag will show GetValue (). GetName ();
If you want to learn more about struts2 tags and ognl expressions suggest finding more detailed information.

  

Method Two:
Of course, using the nature of the JSP itself. Get values by request and session.
Let's change the action class:
public class Loginaction{public String execute () {Somebean value=new somebean (); Value.setname ("Sfsfjsfje"); Actioncontext Context=actioncontext.getcontext ();//Put Attributecontext.put in Request ("value", value);// Put Context.getsession () in the session. Put ("value", value); return SUCCESS;}}

  

Next we change the page:
<!--test.jsp--><%@ page contenttype= "Text/html;charset=utf-8" pageencoding= "Utf-8"%><%@ taglib Prefix= "S" uri= "/struts-tags"%><%= ((Somebean) Request.getattribute ("value")). GetName ()%><%= (( Somebean) Session.get ("value")). GetName ()%>

  

Struts2 the action to pass a value to the page

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.