Struts(13)验证框架

现在,我们将看看如何的Struts验证框架。在Struts的核心有验证框架,协助应用程序的运行规则来执行验证执行之前的操作方法。

通常是使用Javascript来实现客户端验证。但不应单独依赖于客户端验证。最佳实践表明,验证应引入各级应用程序框架。现在,让我们来看看两种方式添加验证我们的Struts项目。

在这里,我们将采取一个例子,Employee 将被捕获的姓名和年龄使用一个简单的页面,我们将会把两个验证,以确保使用总是进入一个名字和年龄应该是在28和65之间。所以,让我们先从主JSP页面的例子。

创建主页面:

让我们写主JSP页面文件的index.jsp,这将被用来收集上述员工的相关信息。

<span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> page language</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"java"</span><span class="pln" style="margin: 0px; padding: 0px;"> contentType</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"text/html; charset=ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">
   pageEncoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> taglib prefix</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"s"</span><span class="pln" style="margin: 0px; padding: 0px;"> uri</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/struts-tags"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><html></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><title></span><span class="pln" style="margin: 0px; padding: 0px;">Employee Form</span><span class="tag" style="margin: 0px; padding: 0px;"></title></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></head></span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="tag" style="margin: 0px; padding: 0px;"><body></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><s:form</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">action</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"empinfo"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">method</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"post"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><s:textfield</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">label</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"Name"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">size</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"20"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><s:textfield</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"age"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">label</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"Age"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">size</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"20"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><s:submit</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"submit"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">label</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"Submit"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">align</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"center"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></s:form></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></body></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></html></span>

在index.jsp使用Struts的标签,我们还没有涉及,但我们将研究这些标签相关的章节。但现在,假设 s:textfield 标签打印一个输入字段s:submit打印一个提交按钮。我们已经使用label属性标签,每个标签每个标签创建。 

创建视图:

我们将使用JSP文件的success.jsp将调用的情况下定义的动作返回SUCCESS。

<span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> page language</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"java"</span><span class="pln" style="margin: 0px; padding: 0px;"> contentType</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"text/html; charset=ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">
	pageEncoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> taglib prefix</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"s"</span><span class="pln" style="margin: 0px; padding: 0px;"> uri</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/struts-tags"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><html></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><title></span><span class="pln" style="margin: 0px; padding: 0px;">Success</span><span class="tag" style="margin: 0px; padding: 0px;"></title></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><body></span><span class="pln" style="margin: 0px; padding: 0px;">
   Employee Information is captured successfully.
</span><span class="tag" style="margin: 0px; padding: 0px;"></body></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></html></span>

创建动作:

因此,让我们定义一个小小的动作类Employee,然后添加一个方法称为validate(),如下所示在Employee.java文件。请确保操作类扩展ActionSupport类,否则validate方法将不会被执行。

<span class="kwd" style="margin: 0px; padding: 0px;">package</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">yiibai</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">class</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Employee</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">extends</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> execute</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> 
   </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> SUCCESS</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> getName</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setName</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> getAge</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setAge</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">age </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> validate</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="kwd" style="margin: 0px; padding: 0px;">if</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">==</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">null</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">||</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">trim</span><span class="pun" style="margin: 0px; padding: 0px;">().</span><span class="pln" style="margin: 0px; padding: 0px;">equals</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">""</span><span class="pun" style="margin: 0px; padding: 0px;">))</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
         addFieldError</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"name"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="str" style="margin: 0px; padding: 0px;">"The name is required"</span><span class="pun" style="margin: 0px; padding: 0px;">);</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="kwd" style="margin: 0px; padding: 0px;">if</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">age </span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="lit" style="margin: 0px; padding: 0px;">28</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">||</span><span class="pln" style="margin: 0px; padding: 0px;"> age </span><span class="pun" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="lit" style="margin: 0px; padding: 0px;">65</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
         addFieldError</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"age"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="str" style="margin: 0px; padding: 0px;">"Age must be in between 28 and 65"</span><span class="pun" style="margin: 0px; padding: 0px;">);</span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px;">}</span>

如在上面的例子所示,“Name”字段的验证方法检查是否有一个值,或不。如果没有值已经提供,我们添加一个带有自定义错误消息“Age”字段的字段错误。其次,我们检查,如果输入的值是在28和65之间或不为“Age”字段,如果这个条件不符合我们以上验证字段添加一个错误。

配置文件:

最后,让我们把所有东西一起使用struts.xml的配置文件如下:

<span class="pun" style="margin: 0px; padding: 0px;"><?</span><span class="pln" style="margin: 0px; padding: 0px;">xml version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"1.0"</span><span class="pln" style="margin: 0px; padding: 0px;"> encoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"UTF-8"</span><span class="pun" style="margin: 0px; padding: 0px;">?></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="tag" style="margin: 0px; padding: 0px;"><struts></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><constant</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts.devMode"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"true"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><package</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"helloworld"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">extends</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts-default"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">

      </span><span class="tag" style="margin: 0px; padding: 0px;"><action</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"empinfo"</span><span class="pln" style="margin: 0px; padding: 0px;"> 
         </span><span class="atn" style="margin: 0px; padding: 0px;">class</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"com.yiibai.struts2.Employee"</span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="atn" style="margin: 0px; padding: 0px;">method</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"execute"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"input"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"success"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/success.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"></action></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"></package></span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="tag" style="margin: 0px; padding: 0px;"></struts></span>

以下是web.xml文件中的内容:

<span class="pun" style="margin: 0px; padding: 0px;"><?</span><span class="pln" style="margin: 0px; padding: 0px;">xml version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"1.0"</span><span class="pln" style="margin: 0px; padding: 0px;"> encoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"UTF-8"</span><span class="pun" style="margin: 0px; padding: 0px;">?></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><web-app</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns:xsi</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://www.w3.org/2001/XMLSchema-instance"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns:web</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">xsi:schemaLocation</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="atn" style="margin: 0px; padding: 0px;">id</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"WebApp_ID"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"3.0"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"><display-name></span><span class="pln" style="margin: 0px; padding: 0px;">Struts 2</span><span class="tag" style="margin: 0px; padding: 0px;"></display-name></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><welcome-file-list></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><welcome-file></span><span class="pln" style="margin: 0px; padding: 0px;">index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></welcome-file></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></welcome-file-list></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"><filter></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-class></span><span class="pln" style="margin: 0px; padding: 0px;">
         org.apache.struts2.dispatcher.FilterDispatcher
      </span><span class="tag" style="margin: 0px; padding: 0px;"></filter-class></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></filter></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-mapping></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><url-pattern></span><span class="pln" style="margin: 0px; padding: 0px;">/*</span><span class="tag" style="margin: 0px; padding: 0px;"></url-pattern></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></filter-mapping></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></web-app></span>

现在,右键点击项目名称,并单击Export > WAR File创建一个WAR文件。然后部署此WAR在Tomcat的webapps目录下。最后,启动Tomcat服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。这会给出以下画面:

Email User Input

现在不输入任何所需信息,只需点击“Submit ”按钮。将看到以下结果:

Error

输入所需的信息,但输入了错误的From字段,让我们说“test”和年龄为30,最后点击“Submit ”按钮。将看到以下结果:

Success

此验证是如何工作的?

当用户按下提交按钮时,Struts2会自动执行的验证方法,如果任何一个if语句里面的方法列出,Struts 2调用addFieldError方法。如果有任何错误已加入Struts 2将不会进行调用execute方法。而Struts 2框架将返回输入作为调用该行动的结果。

因此,验证失败时Struts2返回输入,Struts 2框架将重新显示index.jsp文件。因为我们使用了Struts 2的表单标签,Struts2中会自动添加错误消息,只是上面的形式提交。

这些错误消息是我们addFieldError方法调用中指定的。addFieldError方法有两个参数。首先是表单字段名错误,第二个是错误信息,上面显示该表单字段。

<span class="pln" style="margin: 0px; padding: 0px;">addFieldError</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"name"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="str" style="margin: 0px; padding: 0px;">"The name is required"</span><span class="pun" style="margin: 0px; padding: 0px;">);</span>

要处理的返回值输入,我们需要添加以下的结果,以我们的动作节点在struts.xml。

<span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"input"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span>

基于XML的验证:

在进行验证的第二个方法是通过将一个xml文件的动作类。Struts2的基于XML验证的验证提供了更多的选择,如电子邮件验证,整数范围验证,表单验证字段,表达式验证,正则表达式验证,需要验证,验证所需的字符串,字符串长度的验证等。

XML文件需要被命名为'[action-class]'-validation.xml。所以,在我们的例子中,我们创建一个文件,名为 Employee-validation.xml包含以下内容:

<span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE validators PUBLIC 
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="tag" style="margin: 0px; padding: 0px;"><validators></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"><field</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"><field-validator</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">type</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"required"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><message></span><span class="pln" style="margin: 0px; padding: 0px;">
            The name is required.
         </span><span class="tag" style="margin: 0px; padding: 0px;"></message></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"></field-validator></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></field></span><span class="pln" style="margin: 0px; padding: 0px;">

   </span><span class="tag" style="margin: 0px; padding: 0px;"><field</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"age"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
     </span><span class="tag" style="margin: 0px; padding: 0px;"><field-validator</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">type</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"int"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><param</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"min"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">29</span><span class="tag" style="margin: 0px; padding: 0px;"></param></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><param</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"max"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">64</span><span class="tag" style="margin: 0px; padding: 0px;"></param></span><span class="pln" style="margin: 0px; padding: 0px;">
         </span><span class="tag" style="margin: 0px; padding: 0px;"><message></span><span class="pln" style="margin: 0px; padding: 0px;">
            Age must be in between 28 and 65
         </span><span class="tag" style="margin: 0px; padding: 0px;"></message></span><span class="pln" style="margin: 0px; padding: 0px;">
      </span><span class="tag" style="margin: 0px; padding: 0px;"></field-validator></span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="tag" style="margin: 0px; padding: 0px;"></field></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></validators></span>

上面的XML文件会被保存在CLASSPATH 沿着类文件。让我们有我们的雇员动作类没有validate()方法如下:

<span class="kwd" style="margin: 0px; padding: 0px;">package</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">yiibai</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">

</span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">class</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Employee</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">extends</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> execute</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> 
   </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> SUCCESS</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> getName</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setName</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> getAge</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setAge</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;">
       </span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">age </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;">
   </span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px;">}</span>

其余的设置将保持,因为它是我前面的例子,现在,如果运行应用程序,它会产生相同的结果是什么,我们在前面的例子:

xml文件来存储配置的优点是允许的验证从应用程序代码的分离。可以让开发人员编写的代码和业务分析师建立验证xml文件。要注意的是另一件事是默认提供的验证类型。有大量的验证,默认情况下,使用Struts。常见的验证包括验证日期,正则表达式验证字符串长度的验证。检查以下链接更多细节 Struts - 基于XML的校验.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值