溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Struts中怎么利用validate實現(xiàn)驗證

發(fā)布時間:2021-08-06 15:23:49 來源:億速云 閱讀:88 作者:Leah 欄目:編程語言

這篇文章給大家介紹Struts中怎么利用validate實現(xiàn)驗證,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

Struts validate驗證

1. ActionErrors和ActionError都是ActionMessages的子類,ActionError存放在ActionErrors 中,ActionError對象中的參數(shù)為配置文件中配置的相應(yīng)的值,若配置文件中沒有配置或配置文件不適合都不能用< htnl:errors property=""/>輸出。

2.錯誤信息添加用

ActionErrors errors=new ActionErrors();

errors.add("error_key",new ActionError("配置文件中的相應(yīng)的值"))

注解:void add(java.lang.String property,ActionError error)

在jsp頁面中顯示錯誤:

3.在Action中使用ActionMessages 他有兩個add方法:

他有兩個add方法:

void add(ActionMessages [object Object]message)
[object Object] [object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object]Adds the meesage from the given ActionMessage object [object Object]to tjis set of meesage
void add(java.lang.String property,ActionMessage message)
[object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object]Add message to the set of message for the specifiled property.

錯誤信息添加使用errors.add("error_key",new ActionMessage("errors.loginerror"))
另外要調(diào)用this.saveMessages(request,errors)方法;
這是org.apache.structs.action.Action中 的方法,要用到的大概有兩個:

1.saveErrors(少用)

protected void saveErrors(java.servlet,http.HttoServletRequest request,ActionErrors errors)
Save the specifiled error message keys into the appropriate request attribute for use by the<html:errors>
tag,if any message are required.Otherwise,ensure that the request attribute is not created.

2.saveMessage(常用)

protect void saveMessage(javax.Servlet.http.HttpServletRequest request,ActionMessages messages)
Save the specified message keys into the appropriate request attribute for use by the<html:message>tag{
if messages="true"   is set},if any message are required,Otherwise,ensure that the request attribute is not
created.

ActionErrors actionMessages ,ActionMessage ActionMessage是繼承關(guān)系,父類在前,子類在后

if (!loadForm.getUsername().equals("123")) {

FORM里面

public ActionErrors validate(ActionMapping mapping,
[object Object][object Object] [object Object][object Object] [object Object][object Object]HttpServletRequest request) {

關(guān)于Struts中怎么利用validate實現(xiàn)驗證就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI