開発者ドキュメント

Struts – 例

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

Struts-Logic-MessagePresent-NotPresent-Example.zip

Struts <logic:messagesPresent>タグは、指定されたメッセージまたは現在の要求に存在するエラーメッセージをチェックするために使用されます。

現在のリクエスト

  • “エラーメッセージ”は、Globals.ERROR__KEYキーの下にあるActionErrorsです。

現在のリクエスト

<logic:messagesPresent>と<logic:messagesNotPresent>の使用方法を示すいくつかの例があります。

  1. キーの下にエラーメッセージまたはメッセージが存在する場合

“Globals.ERROR

KEY”または “Globals.MESSAGE

KEY”では、タグの本体が実行されます。

    このページにはエラーがあります!


    このページにエラーはありません!
  1. 「common.email.err」というエラーメッセージまたはメッセージがある場合は、

キー “Globals.ERROR__KEY”の下に存在する場合、タグの本体が実行されます。

    電子メールアドレスにエラーメッセージがあります。 Globals.ERROR__KEY


    電子メールアドレスにエラーメッセージはありません! -  Globals.ERROR__KEY
  1. 「common.email.err」というエラーメッセージまたはメッセージがある場合は、

キー “Globals.MESSAGE__KEY”の下に存在する場合、タグの本体が実行されます。

    電子メールアドレスにエラーメッセージがあります。 -  Globals.MESSAGE__KEY


    電子メールアドレスにエラーメッセージはありません! -  Globals.MESSAGE__KEY
  • LogicExampleAction.java **

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;

public class LogicExampleAction extends Action{

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

       //do nothing

        return mapping.findForward("success");
    }

}
  • EmailForm.java ** – エラーメッセージを返すActionForm – ActionErrors。

package com.mkyong.common.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class EmailForm extends ActionForm{

    String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {

       ActionErrors errors = new ActionErrors();

       errors.add("common.email.err",
        new ActionMessage("error.common.email.required"));

       return errors;

    }
}
  • Common.properties **

#common module error message
error.common.email.required = Email is required.
  • LogicExample.jsp **

Struts - <logic:messagesPresent> & <logic:messagesNotPresent>


    There are errors on this page!


    There are no errors on this page!







    Email address has error messages! Globals.ERROR__KEY


    Email address has no error messages! - Globals.ERROR__KEY







    Email address has error messages! - Globals.MESSAGE__KEY


    Email address has no error messages! - Globals.MESSAGE__KEY
  • 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>

    <form-beans>

        <form-bean name="emailForm"
            type="com.mkyong.common.form.EmailForm"></form-bean>

    </form-beans>

    <action-mappings>

        <action
            path="/LogicTest"
            type="com.mkyong.common.action.LogicExampleAction"
            name="emailForm"
            validate="true"
            input="/pages/LogicExample.jsp"
            >

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

        </action>

    </action-mappings>

    <message-resources
        parameter="com.mkyong.common.Common"/>


</struts-config>

結果



Struts - <logic:messagesPresent> & <logic:messagesNotPresent>
There are errors on this page!

Email address has error messages! Globals.ERROR__KEY

Email address has no error messages! - Globals.MESSAGE__KEY
モバイルバージョンを終了