Struts - <logic: empty> & <logic: notEmpty> Beispiel

Struts - & Beispiel

Laden Sie dieses Beispiel herunter -Struts-Logic-Empty-NotEmpty-Example.zip

Struts wird nur ausgeführt, wenn die angegebene Eigenschaft null oder eine Zeichenfolge mit der Länge null ist oder nicht vorhanden ist. Während die Struts das Gegenteil tun. Wenn die Bedingung erfüllt ist, wird der Text des Tags ausgeführt.

Hier ist das Beispiel, um die Verwendung der Struts und zu zeigen und mit den folgenden drei Listen zu testen.

  1. listMsg0 - Eine Liste enthält Werte.

  2. listMsg1 - Eine leere Liste.

  3. listMsg2 - Eine Liste, die nicht existiert

LogicExampleAction.java

package com.example.common.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LogicExampleAction extends Action{

    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response)
        throws Exception {

        //listMsg0 - A list contains values
        List listMsg0 = new ArrayList();

        listMsg0.add("Message A");
        listMsg0.add("Message B");
        listMsg0.add("Message C");
        listMsg0.add("Message D");

        request.setAttribute("listMsg0", listMsg0);

        //listMsg1 - An empty list
        List listMsg1 = new ArrayList();
        request.setAttribute("listMsg1", listMsg1);

                //listMsg2 - A list which is doesn't exists

        return mapping.findForward("success");
    }

}

LogicExample.jsp

Struts - Test 







     (adsbygoogle = window.adsbygoogle || []).push({});

listMag0 is empty



    listMag1 is empty



    listMag2 is empty


Struts - Test 


    listMag0 is not empty


        List Messages 0 -






    listMag1 is not empty


        List Messages 1 -





    listMag2 is not empty


        List Messages 2 -

struts-config.xml





  

     

        

    

  

Ergebnis

Struts-logic-empty-notempty-example

In Struts – Test <logic:empty> werden nur listMsg1 und listMsg2 angezeigt. Dies liegt daran, dass listMag1 eine leere Liste ist, während listMag2 überhaupt nicht vorhanden ist.

In Struts – Test <logic:notEmpty> wird nur listMsg0 angezeigt, da dies die einzige Liste ist, die Werte enthält.