この例をダウンロードする –

Struts-Logic-Empty-NotEmpty-Example.zip

Struts <logic:empty>は、指定されたプロパティがnull、長さがゼロのString、または存在しない場合にのみ実行されます。 Struts <logic:notEmpty>は逆の方法を行っています。条件が一致すると、タグの本体が実行されます。

ここでは、Struts <logic:empty>の使用方法を示す例を示します。

  1. listMsg0 – リストに値が含まれています.

  2. listMsg1 – 空のリスト.

  3. listMsg2 – 存在しないリスト

package com.mkyong.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<String> listMsg0 = new ArrayList<String>();

        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<String> listMsg1 = new ArrayList<String>();
        request.setAttribute("listMsg1", listMsg1);

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

        return mapping.findForward("success");
    }

}

  • LogicExample.jsp **

Struts - Test <logic:empty>





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






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



    listMag2 is empty


Struts - Test <logic:notEmpty>


    listMag0 is not empty


        List Messages 0 -






    listMag1 is not empty


        List Messages 1 -





    listMag2 is not empty


        List Messages 2 -

  • struts-config.xml **

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd">

<struts-config>
  <action-mappings>

     <action
        path="/LogicTest"
        type="com.mkyong.common.action.LogicExampleAction">

        <forward name="success" path="/pages/LogicExample.jsp"/>

    </action>

  </action-mappings>
</struts-config>

結果


Struts-logic-empty-notempty-example、title = "Struts-logic-empty-notempty-example"

  • Struts – Test <logic:empty> ** では、listMag1とlistMsg2だけが表示されます。これは、listMag1は空のリストであり、listMag2はまったく存在しないためです。

  • Struts – Test <logic:notEmpty> ** では、唯一のリストに値が含まれているため、listMsg0のみが表示されます。