開発者ドキュメント

Struts – 例

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

Struts-Logic-Present-NotPresent-Example.zip

Struts <logic:present>タグは、指定されたオブジェクトまたはプロパティが現在のリクエストに存在するか存在しているかをチェックするために使用されます。 <logic:notPresent>は逆の動作をします。

次に、<logic:present>と<logic:notPresent>の使用例を示します。

package com.mkyong.common;

public class User{

    String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
  • LogicExampleAction.java ** – Userオブジェクトを初期化し、urlプロパティを設定してリクエストセッションに格納します。

package com.mkyong.common.action;

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;

import com.mkyong.common.User;

public class LogicExampleAction extends Action{

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

        User user = new User();
        user.setUrl("/");

        request.setAttribute("user", user);

        return mapping.findForward("success");
    }

}
  • LogicExample.jsp **

Struts - <logic:present> & <logic:notPresent>


    User object is exists.


    User object does not exists.






    Abc object is exists.


    Abc object does not exists.






    User object, url property is exists.


    User object, url property does not exists.






    User object, email property is exists.


    User object, email property does not exists.
  • 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:present> & <logic:notPresent>
User object is exists.

Abc object does not exists.

User object, url property is exists.

User object, email property does not exists.
モバイルバージョンを終了