問題描述
我使用 addActionMessage
和 addActionError
方法在 struts2 動作類中添加了我的錯誤和消息.現在我想要在 html 頁面中顯示此消息和此錯誤的代碼?可以打印嗎?
I added my error and message in struts2 action class using addActionMessage
and addActionError
method.
Now I want code for displaying this message and this error in the html page? Is it possible to print?
推薦答案
Put
<s:actionerrors />
和
<s:actionmessages />
JSP 中的標簽
并且,為了獲得更好的圖形結果,請使用特定的 div 容器(例如,一個紅色,一個綠色)并在之前檢查您是否有消息或錯誤(僅在不為空時打印 div):
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):
<s:if test="hasActionErrors()">
<div class="feedError">
<s:actionerrors />
</div>
</s:if>
<s:if test="hasActionMessages()">
<div class="feedOk">
<s:actionmessages />
</div>
</s:if>
在 CSS 中
.feedError{
width: 100%;
border: 10px solid red;
}
.feedOk{
width: 100%;
border: 10px solid green;
}
<小時>
不,據我所知,您無法直接從 HTML 獲取操作錯誤和消息.您可以對從 Session 中檢索它們并在 JSon 中返回它們的操作進行 ajax 調用,但這確實是矯枉過正、設計不佳且沒有必要.
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.
作為一種解決方案,您可以使用另一種視圖技術來代替 JSP;它不僅僅是普通的 HTML,但至少你不能在頁面內使用 Scriptlet,如果這是你使用 JSP 的問題的話.
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.
例如,您可以使用 FreeMarker 模板.
之前的 JSP 片段在 .ftl 文件中會變成這樣(未經測試):
The previous JSP snippet would become something like this (untested), in the .ftl file:
<#if (actionErrors?size>0)>
<div class="feedError">
<@s.actionerror />
</div>
</#if>
<#if (actionMessages?size>0)>
<div class="feedOk">
<@s.actionmessage />
</div>
</#if>
或 Velocity(同樣,未經測試):
Or Velocity (again, untested):
#if( $actionErrors.size() > 0 )
<div class="feedError">
#foreach( $msg in $actionErrors )
[$msg]<br />
#end
</div>
#end
#if( $actionMessages.size() > 0 )
<div class="feedOk">
#foreach( $msg in $actionMessages )
[$msg]<br />
#end
</div>
#end
<小時>
注意:如果不使用 JSP 的原因是為了防止 scriptlet,您可以在 web.xml 添加配置此行為
NOTE: if the reason for not using JSPs is to prevent scriptlets, you can configure this behaviour in web.xml adding
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
在 Struts2 中使用 JSP 并不是唯一的方法,但它是最簡單和最快的,恕我直言.
Using JSP with Struts2 is not the only way, but it is the easiest and the fastest, imho.
這篇關于html頁面顯示struts2錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!