Diplay struts2 error in html page

前端 未结 1 546
太阳男子
太阳男子 2020-12-07 05:44

I added my error and message in struts2 action class using addActionMessage and addActionError method. Now I want code for displaying this message

1条回答
  •  萌比男神i
    2020-12-07 05:56

    Put

    
    

    and

    
    

    tags inside your JSP

    AND, for a better graphical result, use a particular div container (one red, one green for example) and check before if you have messages or errors (to print the div only if not empty):

    
       

    and in CSS

    .feedError{
        width: 100%;
        border: 10px solid red;
    }
    
    .feedOk{
        width: 100%;
        border: 10px solid green;
    }
    

    EDIT: no, as far as I know you can't get Action Errors and Messages directly from HTML. You can make ajax calls to an action that retrieves them from Session and returns them in JSon, but this is really overkill, bad designed and not necessary.

    As a solution, you can use another view technology instead of JSP; it isn't just plain HTML, but at least you can't use Scriptlets inside pages, if that's is your problem with using JSP.

    You can use FreeMarker Template, for example.

    The previous JSP snippet would become something like this (untested), in the .ftl file:

    <#if (actionErrors?size>0)>
        
    <@s.actionerror />
    <#if (actionMessages?size>0)>
    <@s.actionmessage />

    Or Velocity (again, untested):

    #if( $actionErrors.size() > 0 )
       
    #foreach( $msg in $actionErrors ) [$msg]
    #end
    #end #if( $actionMessages.size() > 0 )
    #foreach( $msg in $actionMessages ) [$msg]
    #end
    #end

    NOTE: if the reason for not using JSPs is to prevent scriptlets, you can configure this behaviour in web.xml adding

    
            
                    *.jsp
                    true
            
     
    

    Using JSP with Struts2 is not the only way, but it is the easiest and the fastest, imho.

    0 讨论(0)
提交回复
热议问题