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

Struts – の例

この例をダウンロード–Struts-Logic-MessagePresent-NotPresent-Example.zip

Struts タグは、指定されたメッセージまたは現在のリクエストに存在するエラーメッセージを確認するために使用されます。

  • 「メッセージ」は、現在のリクエストのキーGlobals.MESSAGE_KEYの下のActionMessagesです。

  • 「エラーメッセージ」は、現在のリクエストのキーGlobals.ERROR_KEYの下のActionErrorsです。

およびの使用方法を示すいくつかの例を次に示します。

  1. エラーメッセージがある場合、またはキー「Globals.ERROR_KEY」または「Globals.MESSAGE_KEY」の下にメッセージが存在する場合、タグの本文が実行されます。

        There are errors on this page!
    
    
        There are no errors on this page!
  2. エラーメッセージがある場合、またはキー「Globals.ERROR_KEY」の下に「common.email.err」という名前のメッセージが存在する場合、タグの本文が実行されます。

        Email address has error messages! Globals.ERROR_KEY
    
    
        Email address has no error messages! - Globals.ERROR_KEY
  3. エラーメッセージがあるか、キー「Globals.MESSAGE_KEY」の下に「common.email.err」という名前のメッセージが存在する場合、タグの本文が実行されます。

        Email address has error messages! - Globals.MESSAGE_KEY
    
    
        Email address has no error messages! - Globals.MESSAGE_KEY

LogicExampleAction.java

package com.example.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.example.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 -  & 


    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





    

        

    

    

        

            

        

    

    


結果

Struts-logic-messages-present-notpresent-example

Struts -  & 
There are errors on this page!

Email address has error messages! Globals.ERROR_KEY

Email address has no error messages! - Globals.MESSAGE_KEY