要开始在你的项目中使用注释,确保WebContent/WEB-INF/lib文件夹中的jar文件包括以下:
struts2-convention-plugin-x.y.z.jar
asm-x.y.jar
antlr-x.y.z.jar
commons-fileupload-x.y.z.jar
commons-io-x.y.z.jar
commons-lang-x.y.jar
commons-logging-x.y.z.jar
commons-logging-api-x.y.jar
freemarker-x.y.z.jar
javassist-.xy.z.GA
ognl-x.y.z.jar
struts2-core-x.y.z.jar
xwork-core.x.y.z.jar
现在,让我们看看你如何能做到配置在struts.xml文件,取而代之的是注解。
Struts2注释的概念的解释,我们需要重新考虑我们的验证为例说明在 Struts2的验证 一章中。
在这里,我们将采取一个例子,雇员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;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> org</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">apache</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;">convention</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">annotation</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">Action</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;"> org</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">apache</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;">convention</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">annotation</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">Result</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;"> org</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">apache</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;">convention</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">annotation</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">Results</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="pln" style="margin: 0px; padding: 0px;">validator</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">annotations</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;">@Results</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;">@Result</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="str" style="margin: 0px; padding: 0px;">"success"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> location</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/success.jsp"</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;">@Result</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="str" style="margin: 0px; padding: 0px;">"input"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> location</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/index.jsp"</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;">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="lit" style="margin: 0px; padding: 0px;">@Action</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/empinfo"</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="lit" style="margin: 0px; padding: 0px;">@RequiredFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"The name is required"</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="lit" style="margin: 0px; padding: 0px;">@IntRangeFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" 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;"> min </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"29"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> max </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" 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="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>
在这个例子中,我们已经使用了一些注解。让我逐个说明:
首先,我们已经Result注解。结果注解的结果是一个集合。结果注解下,我们有两个结果注释。结果注释的名称对应的执行方法的结果。它们还含有一个视图应担任相应的execute() 返回值的位置。
下一个注解是行动注解。这是用来修饰 execute()方法。操作方法也需要一个值,该URL上调用操作。
最后,使用两个验证的注解。已经配置了所需的字段验证的年龄字段"name“字段和整数范围验证。也指定了自定义验证消息。
我们不需要struts.xml 配置文件,让我们删除该文件,并让我们检查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;"><init-param></span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;"><param-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts.devMode</span><span class="tag" style="margin: 0px; padding: 0px;"></param-name></span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;"><param-value></span><span class="pln" style="margin: 0px; padding: 0px;">true</span><span class="tag" style="margin: 0px; padding: 0px;"></param-value></span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;"></init-param></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。这会给出以下画面:
现在不输入任何所需信息,只需点击“Submit ”按钮。将看到以下结果:
输入所需的信息,但输入了错误的“From ”字段,让我们说“test”和年龄为30名,最后点击“Submit ”按钮。将看到以下结果:
@ Namespace注释允许在Action类中,而不是基于零配置的约定动作的命名空间的定义。
<span class="lit" style="margin: 0px; padding: 0px;">@Namespace</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="str" style="margin: 0px; padding: 0px;">"/content"</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="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>
@ Result注解允许在Action类中,而不是一个XML文件中定义的动作结果。
<span class="lit" style="margin: 0px; padding: 0px;">@Result</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="str" style="margin: 0px; padding: 0px;">"success"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/success.jsp"</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="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>
@ Results注解定义了一套动作的结果。
<span class="lit" style="margin: 0px; padding: 0px;">@Results</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;">@Result</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="str" style="margin: 0px; padding: 0px;">"success"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/success.jsp"</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;">@Result</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="str" style="margin: 0px; padding: 0px;">"error"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/error.jsp"</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;">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="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>
@After注解标志着一个需要调用后的主要操作方法和执行结果的操作方法。返回值将被忽略。
<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="lit" style="margin: 0px; padding: 0px;">@After</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;"> isValid</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;">throws</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ValidationException</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="com" style="margin: 0px; padding: 0px;">// validate model object, throw exception if failed</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;"> 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="com" style="margin: 0px; padding: 0px;">// perform secure action</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="pun" style="margin: 0px; padding: 0px;">}</span>
@ Before注释标记需要一个操作方法的主要操作方法之前被调用执行结果。返回值将被忽略。
<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="lit" style="margin: 0px; padding: 0px;">@Before</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;"> isAuthorized</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;">throws</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">AuthenticationException</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="com" style="margin: 0px; padding: 0px;">// authorize request, throw exception if failed</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;"> 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="com" style="margin: 0px; padding: 0px;">// perform secure action</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="pun" style="margin: 0px; padding: 0px;">}</span>
@ BeforeResult注解标志着一个结果之前需要执行的操作方法。返回值将被忽略。
<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="lit" style="margin: 0px; padding: 0px;">@BeforeResult</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;"> isValid</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;">throws</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ValidationException</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="com" style="margin: 0px; padding: 0px;">// validate model object, throw exception if failed</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;"> 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="com" style="margin: 0px; padding: 0px;">// perform action</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="pun" 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="lit" style="margin: 0px; padding: 0px;">@ConversionErrorFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</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="pun" 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="lit" style="margin: 0px; padding: 0px;">@DateRangeFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> min </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"2005/01/01"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> max </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"2005/12/31"</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;"> getDOB</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;"> dob</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="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="lit" style="margin: 0px; padding: 0px;">@DoubleRangeFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> minInclusive </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"0.123"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> maxInclusive </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"99.987"</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;"> getIncome</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;"> income</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>
这验证注解检查一个字段是一个有效的E-mail地址,如果它包含一个非空的字符串。
<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="lit" style="margin: 0px; padding: 0px;">@EmailValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</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;"> getEmail</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;"> email</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="lit" style="margin: 0px; padding: 0px;">@ExpressionValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> expression </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"an OGNL expression"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" 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="lit" style="margin: 0px; padding: 0px;">@IntRangeFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> min </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"0"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> max </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"42"</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;"> 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="pun" style="margin: 0px; padding: 0px;">}</span>
这个注解验证一个字符串字段,使用正则表达式。
<span class="lit" style="margin: 0px; padding: 0px;">@RegexFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"regex.field"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> expression </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"yourregexp"</span><span class="pun" 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="lit" style="margin: 0px; padding: 0px;">@RequiredFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</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;"> 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="pun" style="margin: 0px; padding: 0px;">}</span>
这验证注解检查一个字符串字段不为空(即非空,长度> 0)。
<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="lit" style="margin: 0px; padding: 0px;">@RequiredStringValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</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;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">true</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="pun" style="margin: 0px; padding: 0px;">}</span>
这个验证检查字符串字段是合适的长度。假定该字段是一个字符串。如果设置既不是minLength 也不是最大长度,什么都不会做。
<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="lit" style="margin: 0px; padding: 0px;">@StringLengthFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</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;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">true</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> minLength </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"5"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> maxLength </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"12"</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="pun" style="margin: 0px; padding: 0px;">}</span>
这个验证检查一个字段是一个有效的URL。
<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="lit" style="margin: 0px; padding: 0px;">@UrlValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"Default message"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> key </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"i18n.key"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> shortCircuit </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;">true</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;"> getURL</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;"> url</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>
如果想使用多个相同类型的注释,这些注释必须嵌套在@Validations() 注释。
<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="lit" style="margin: 0px; padding: 0px;">@Validations</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> requiredFields </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="lit" style="margin: 0px; padding: 0px;">@RequiredFieldValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">type </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ValidatorType</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">SIMPLE</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> fieldName </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"customfield"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"You must enter a value for field."</span><span class="pun" style="margin: 0px; padding: 0px;">)},</span><span class="pln" style="margin: 0px; padding: 0px;"> requiredStrings </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="lit" style="margin: 0px; padding: 0px;">@RequiredStringValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">type </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ValidatorType</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">SIMPLE</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> fieldName </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"stringisrequired"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> message </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"You must enter a value for string."</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="pun" style="margin: 0px; padding: 0px;">}</span>
这个注解可以用于自定义验证。使用ValidationParameter的注释,以提供额外的 params.
<span class="lit" style="margin: 0px; padding: 0px;">@CustomValidator</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">type </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"customValidatorName"</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> fieldName </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"myField"</span><span class="pun" style="margin: 0px; padding: 0px;">)</span>
这是一个标记注释类型转换类型级别。转换注释必须应用在类型级别。
<span class="lit" style="margin: 0px; padding: 0px;">@Conversion</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;">ConversionAction</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">implements</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Action</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>
这个注解设置类型转换CreateIfNull。必须应用在域或方法级CreateIfNull注解。
<span class="lit" style="margin: 0px; padding: 0px;">@CreateIfNull</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> value </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;">true</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;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">List</span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="typ" style="margin: 0px; padding: 0px;">User</span><span class="pun" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"> users</span><span class="pun" style="margin: 0px; padding: 0px;">;</span>
这个注解设置元素进行类型转换。必须应用在字段域或方法级元素的注解。
<span class="lit" style="margin: 0px; padding: 0px;">@Element</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> value </span><span class="pun" style="margin: 0px; padding: 0px;">=</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;">acme</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">User</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;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">List</span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="typ" style="margin: 0px; padding: 0px;">User</span><span class="pun" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"> userList</span><span class="pun" style="margin: 0px; padding: 0px;">;</span>
这个注解设置进行类型转换的关键。必须应用在域或方法级的关键注解。
<span class="lit" style="margin: 0px; padding: 0px;">@Key</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> value </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> java</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">lang</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">Long</span><span class="pun" 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="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;">Map</span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="typ" style="margin: 0px; padding: 0px;">Long</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">User</span><span class="pun" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"> userMap</span><span class="pun" style="margin: 0px; padding: 0px;">;</span>
这个注解设置类型转换KeyProperty。必须应用在域或方法级KeyProperty注解。
<span class="lit" style="margin: 0px; padding: 0px;">@KeyProperty</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> value </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"userName"</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;">protected</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">List</span><span class="pun" style="margin: 0px; padding: 0px;"><</span><span class="typ" style="margin: 0px; padding: 0px;">User</span><span class="pun" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"> users </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="pun" style="margin: 0px; padding: 0px;">;</span>
这个注解的注解是用于类和应用程序的转换规则。注解可以应用于TypeConversion在属性和方法的级别。
<span class="lit" style="margin: 0px; padding: 0px;">@TypeConversion</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;">rule </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ConversionRule</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">COLLECTION</span><span class="pun" style="margin: 0px; padding: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px;"> converter </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="str" style="margin: 0px; padding: 0px;">"java.util.String"</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;"> setUsers</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">List</span><span class="pln" style="margin: 0px; padding: 0px;"> users </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;">users </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> users</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>