Java EE Learning--struts2_03:ognl expressions, OGNL and Struts2 combinations and exercises: List of customers

Source: Internet
Author: User

First, OGNL expression

1. Introduction

OGNL: Object view navigation language. ${user.addr.name} This notation is called object View navigation.
OGNL can be more than just view navigation. Features that are richer than El expressions are supported.

2. Prepare for work with OGNL 2.1 Guide pack

Struts2 is already included in the package. So there's no need to import additional jar packages

2.2 Code Preparation

  

@Test//preparatory work     Public voidFUN1 ()throwsexception{//Prepare Ognlcontext//Prepare rootUser Rootuser =NewUser ("Tom", 18); //Prepare the contextmap<string,user> context =NewHashmap<string,user>(); Context.put ("User1",NewUser ("Jack", 18)); Context.put ("User2",NewUser ("Rose", 22)); Ognlcontext OC=NewOgnlcontext (); //use Rootuser as the root sectionOc.setroot (Rootuser); //Use the context map as the context sectionoc.setvalues (context); //Writing OGNLOgnl.getvalue ("", OC, Oc.getroot ()); }
preparatory work3. Basic Grammar Demo
        // Remove the Name property of the user object in root        String name = (string) ognl.getvalue ("name", OC, Oc.getroot ());         = (Integer) ognl.getvalue ("Age", OC, Oc.getroot ());        SYSTEM.OUT.PRINTLN (name);        System.out.println (age);
Remove the attribute value from root
        // Remove the Name property        of the context key to the User1 object String name = (string) ognl.getvalue ("#user1. Name", OC, Oc.getroot ());         = (String) ognl.getvalue ("#user2. Name", OC, Oc.getroot ());         = (Integer) ognl.getvalue ("#user2. Age", OC, Oc.getroot ());        SYSTEM.OUT.PRINTLN (name);        System.out.println (name2);        System.out.println (age);
Remove attribute values from the context
        // assigns a value        to the Name property of the user object in root Ognl.getvalue ("Name= ' Jerry '", OC, Oc.getroot ());         = (String) ognl.getvalue ("name", OC, Oc.getroot ());                 = (String) ognl.getvalue ("#user1. Name= ' Shangjiangyong ', #user1. Name", OC, Oc.getroot ());        SYSTEM.OUT.PRINTLN (name);        System.out.println (name2);
Assigning a value to a property
        // the SetName method for invoking the user object in root        Ognl.getvalue ("SetName (' Lilei ')", OC, Oc.getroot ());         = (String) ognl.getvalue ("GetName ()", OC, Oc.getroot ());         = (String) ognl.getvalue ("#user1. SetName (' Lucy '), #user1. GetName ()", OC, Oc.getroot ());                SYSTEM.OUT.PRINTLN (name);        System.out.println (name2);
Calling Methods
        String name = (string) ognl.getvalue ("@[email protected" (' Hello strong! ') " , OC, Oc.getroot ());         // double pi = (double) ognl.getvalue ("@[email protected]", OC, Oc.getroot ());        Double pi = (double) ognl.getvalue ("@ @PI", OC, Oc.getroot ());        SYSTEM.OUT.PRINTLN (name);        System.out.println (pi);
calling a static method
        //Create a List objectInteger size = (integer) ognl.getvalue ("{' Tom ', ' Jerry ', ' Jack ', ' Rose '}.size ()", OC, Oc.getroot ()); String name= (String) ognl.getvalue ("{' Tom ', ' Jerry ', ' Jack ', ' Rose '}[0]", OC, Oc.getroot ()); String name2= (String) ognl.getvalue ("{' Tom ', ' Jerry ', ' Jack ', ' Rose '}.get (1)", OC, Oc.getroot ()); /*System.out.println (size);        SYSTEM.OUT.PRINTLN (name); System.out.println (name2);*/        //Create a Map objectInteger size2 = (integer) ognl.getvalue ("#{' name ': ' Tom ', ' Age ': 18}.size ()", OC, Oc.getroot ()); String Name3= (String) ognl.getvalue ("#{' name ': ' Tom ', ' Age ': 18}[' name ']", OC, Oc.getroot ()); Integer Age= (Integer) ognl.getvalue ("#{' name ': ' Tom ', ' Age ': 18}.get (' age ')", OC, Oc.getroot ());        System.out.println (SIZE2);        System.out.println (Name3); System.out.println (age);
Ognl creating Objects-list|map

Ii. combination of OGNL and Struts2 1. Principle of bonding

  

Two parts of the Valuestack

  

2. Stack principle

  

The stack is simulated by ArrayList

  

Implementation of two methods in a stack

  

The properties of the access stack. From top to bottom

  

3. View two parts of the value stack (using the debug tag) 3.1Root

By default, the currently accessed action object is placed in the stack

  

3.2Context

  

  

The context section is the Actioncontext data center.

4.struts2 and OGNL combine to embody 4.1 parameter reception

How to get the value stack object, the value stack object and the Actioncontext object are referenced to each other

        // press into the top         of the stack // 1 getting the value stack        Valuestack vs = Actioncontext.getcontext (). Getvaluestack ();         // 2 pressing u into the top        of the stack Vs.push (U);
4.2 In the configuration file
<Actionname= "Demo3action"class= "Cn.itheima.d_config." Demo3action "Method= "Execute" >            <resultname= "Success"type= "Redirectaction" >                <paramname= "ActionName">Demo1action</param>                <paramname= "namespace">/</param>                <!--If you add a parameter, struts "can't understand". The redirected path is appended as a parameter. If the parameter is dynamic. You can use ${} to wrap the OGNL expression. Dynamic values -                <paramname= "Name">${name}</param>            </result>        </Action>
syntax: ${OGNL expression}5. Extension: GetAttribute method for Request object

Search Order:

  

Iii. Practice: List of customers
     PublicString list ()throwsException {//1 Accept ParametersString cust_name = Servletactioncontext.getrequest (). GetParameter ("Cust_name"); //2 Creating an offline query objectDetachedcriteria DC =detachedcriteria.forclass (Customer.class); //3 Determining the conditions for assembling parameters        if(Stringutils.isnotblank (cust_name)) {Dc.add (Restrictions.like ("Cust_name", "%" +cust_name+ "%")); }        //4 Calling service to pass an offline objectlist<customer> list =Cs.getall (DC); //5 Put the returned list into the request domain. Forward to list.jsp display//servletactioncontext.getrequest (). SetAttribute ("list", list); //put it in Actioncontext .Actioncontext.getcontext (). Put ("list", list); return"List"; }
Action Code (new Actioncontext holds data)
<s:iterator value= "#list"var= "Cust" > <TR Style= "Font-weight:normal; Font-style:normal; Background-color:white;        Text-decoration:none "> <TD> <s:property value=" #cust. Cust_name "/> </TD> <TD> <s:property value= "#cust. Cust_level"/> </TD> <TD> <s:pro Perty value= "#cust. Cust_source"/> </TD> <TD> <s:property value= "#cust. Cust_linkman        "/> </TD> <TD> <s:property value=" #cust. Cust_phone "/> </TD> <TD> <s:property value= "#cust. Cust_mobile"/> </TD> <TD> <a href= "${pagecontext.request.contextpath}/customerservlet?method=edit&custid=${customer.cust_id}" > Modify </a > &nbsp;&nbsp; <a href= "${pagecontext.request.contextpath}/customerservlet?method=delete&custid=${customer.cust_id}"    > Delete </a> </TD> </TR> </s:iterator> <%--<s:iterator value= "#list" > <TR Style= "Font-weight:normal; Font-style:normal; Background-color:white; Text-decoration:none "> <TD> <s:property value=" Cust_name "/> </TD> & Lt td> <s:property value= "Cust_level"/> </TD> <TD> <s:property value= "cu        St_source "/> </TD> <TD> <s:property value=" Cust_linkman "/> </TD> <TD> <s:property value= "Cust_phone"/> </TD> <TD> <s:propert Y value= "cust_mobile"/> </TD> <TD> <a href= "${pagecontext.request.contextpath}/CU STOMERSERVLET?METHOD=EDIT&AMP;CUSTID=${CUSTOMER.CUST_ID} "> Modify </a> &nbsp;&nbsp; <a href= "${pagecontext.request.contextpath}/customerservlet?method=delete&custid=${customer.cust_id}" > Delete </a> </TD> </TR> </s:iterator>--%>
JSP Displays the code of the data (another method is commented)

Note: <s:iterator value= "#list" var= "Cust" > each time cust exists Actioncontext

Java EE Learning--struts2_03:ognl expressions, OGNL and Struts2 combinations and exercises: List of customers

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.