This is an automated email from the ASF dual-hosted git repository.
git-site-role pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/struts-site.git
The following commit(s) were added to refs/heads/asf-site by this push:
new 53fc0ef Updates production by Jenkins
53fc0ef is described below
commit 53fc0ef618ec8314806b05c55a9df9dab6769bda
Author: jenkins <[email protected]>
AuthorDate: Tue Jun 5 09:16:05 2018 +0000
Updates production by Jenkins
---
content/core-developers/validation.html | 74 ++++++++++++++++++++++++++++++++-
1 file changed, 73 insertions(+), 1 deletion(-)
diff --git a/content/core-developers/validation.html
b/content/core-developers/validation.html
index 4250408..107deb3 100644
--- a/content/core-developers/validation.html
+++ b/content/core-developers/validation.html
@@ -141,7 +141,14 @@
<li><a href="#validator-scopes" id="markdown-toc-validator-scopes">Validator
Scopes</a></li>
<li><a href="#notes" id="markdown-toc-notes">Notes</a></li>
<li><a href="#defining-validation-rules"
id="markdown-toc-defining-validation-rules">Defining Validation Rules</a></li>
- <li><a href="#localizing-and-parameterizing-messages"
id="markdown-toc-localizing-and-parameterizing-messages">Localizing and
Parameterizing Messages</a></li>
+ <li><a href="#localizing-and-parameterizing-messages"
id="markdown-toc-localizing-and-parameterizing-messages">Localizing and
Parameterizing Messages</a> <ul>
+ <li><a href="#customizing-validation-messages"
id="markdown-toc-customizing-validation-messages">Customizing validation
messages</a> <ul>
+ <li><a href="#xml" id="markdown-toc-xml">XML</a></li>
+ <li><a href="#annotations"
id="markdown-toc-annotations">Annotations</a></li>
+ </ul>
+ </li>
+ </ul>
+ </li>
<li><a href="#validator-flavor" id="markdown-toc-validator-flavor">Validator
Flavor</a></li>
<li><a href="#non-field-validator-vs-field-validator-validatortypes"
id="markdown-toc-non-field-validator-vs-field-validator-validatortypes">Non-Field
Validator Vs Field-Validator validatortypes</a></li>
<li><a href="#short-circuiting-validator"
id="markdown-toc-short-circuiting-validator">Short-Circuiting Validator</a></li>
@@ -425,6 +432,71 @@ it is possible to construct quite sophisticated
messages.</p>
</code></pre>
</div>
+<h3 id="customizing-validation-messages">Customizing validation messages</h3>
+
+<p>There is another option to customise validation messages by using
parametrized messages. Either you can use them via
+XML or with annotations.</p>
+
+<h4 id="xml">XML</h4>
+
+<p>To use this new approach you must use a proper header in a <code
class="highlighter-rouge"><ActionName>-validation.xml</code> file, see
below:</p>
+
+<div class="highlighter-rouge"><pre class="highlight"><code><span
class="cp"><?xml version="1.0"?></span>
+<span class="cp"><!DOCTYPE validators PUBLIC
+ "-//Apache Struts//XWork Validator 1.0.3//EN"
+ "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"></span>
+<span class="nt"><validators></span>
+ ...
+<span class="nt"></validators></span>
+</code></pre>
+</div>
+
+<p>Now you can define validators that will use parametrized messages as
below:</p>
+
+<div class="highlighter-rouge"><pre class="highlight"><code><span
class="nt"><field</span> <span class="na">name=</span><span
class="s">"username"</span><span class="nt">></span>
+ <span class="nt"><field-validator</span> <span
class="na">type=</span><span class="s">"requiredstring"</span><span
class="nt">></span>
+ <span class="nt"><message</span> <span class="na">key=</span><span
class="s">"errors.required"</span><span class="nt">></span>
+ <span class="nt"><param</span> <span
class="na">name=</span><span class="s">"0"</span><span
class="nt">></span>getText('username.field.name')<span
class="nt"></param></span>
+ <span class="nt"></message></span>
+ <span class="nt"></field-validator></span>
+<span class="nt"></field></span>
+</code></pre>
+</div>
+
+<blockquote>
+ <p>NOTE: Please be aware that all the parameters will be evaluated against
<code class="highlighter-rouge">ValueStack</code>, please do not reference user
+controlled values or incoming parameters in request as this can lead to a
security vulnerability</p>
+</blockquote>
+
+<p>Now you can define your properties file with localized messages:</p>
+
+<div class="highlighter-rouge"><pre
class="highlight"><code>errors.required={0} is required.
+username.field.name=Username
+</code></pre>
+</div>
+
+<p>As you can see you defined a <code
class="highlighter-rouge">errors.required</code> key with a placeholder for the
param. The names of the params are not important,
+order is important as this mechanism uses <code
class="highlighter-rouge">MessageFormat</code> to format the message.</p>
+
+<p>The final output will be as follow:</p>
+
+<div class="highlighter-rouge"><pre class="highlight"><code>Username is
required.
+</code></pre>
+</div>
+
+<h4 id="annotations">Annotations</h4>
+
+<p>The same mechanism can be used with annotations as follow:</p>
+
+<div class="highlighter-rouge"><pre class="highlight"><code><span
class="nd">@RequiredStringValidator</span><span class="o">(</span><span
class="n">key</span> <span class="o">=</span> <span
class="s">"errors.required"</span><span class="o">,</span> <span
class="n">messageParams</span> <span class="o">=</span> <span class="o">{</span>
+ <span class="s">"getText('username.field.name')"</span>
+<span class="o">})</span>
+<span class="kd">public</span> <span class="kt">void</span> <span
class="nf">setUsername</span><span class="p">(</span><span
class="n">String</span> <span class="n">username</span><span class="o">)</span>
<span class="o">{</span>
+ <span class="k">this</span><span class="o">.</span><span
class="na">username</span> <span class="o">=</span> <span
class="n">username</span><span class="o">;</span>
+<span class="o">}</span>
+</code></pre>
+</div>
+
<h2 id="validator-flavor">Validator Flavor</h2>
<p>The validators supplied by the XWork distribution (and any validators you
might write yourself) come in two different
--
To stop receiving notification emails like this one, please contact
[email protected].