在Struts
這兩個add方法和ActionErrors類中的add方法類似
以上三個add方法都在ValidationAware接口中定義
private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
public void addActionError(String anErrorMessage)
{ validationAware
}
public void addActionMessage(String aMessage)
{
validationAware
}
public void addFieldError(String fieldName
{
validationAware
}
下面我們來實現一個簡單的驗證程序
先來在Web根目錄建立一個主頁面(validate
<%@ page language=
<%@ taglib prefix=
<html>
<head>
<title>驗證數據title>
head>
<body>
<s:actionerror/>
<s:actionmessage/>
<s:form action=
輸入內容
<s:fielderror key=
<br/>
<s:submit/>
s:form>
body>
html>
在上面的代碼中
package action;
import javax
import com
import org
public class ValidateAction extends ActionSupport
{
private String msg;
public String execute()
{
system
return SUCCESS;
}
public void validate()
{
if(!msg
{
system
this
this
}
else
{
this
}
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this
}
}
大家從上面的代碼可以看出
最後來配置一下這個Action
<package name=
<action name=
<result name=
<result name=
action>
package>
假設應用程序的上下文路徑為demo
package com
import java
import java
public interface ValidationAware
{
void setActionErrors(Collection errorMessages);
Collection getActionErrors();
void setActionMessages(Collection messages);
Collection getActionMessages();
void setFieldErrors(Map errorMap);
Map getFieldErrors();
void addActionError(String anErrorMessage);
void addActionMessage(String aMessage);
void addFieldError(String fieldName
boolean hasActionErrors();
boolean hasActionMessages();
boolean hasErrors();
boolean hasFieldErrors();
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28735.html