{"id":3724,"date":"2012-05-11T17:42:54","date_gmt":"2012-05-11T08:42:54","guid":{"rendered":"http:\/\/arison.jp\/blog\/2012\/05\/11\/asaeyaaaasaestruts-2a\/"},"modified":"2017-02-25T22:10:26","modified_gmt":"2017-02-25T13:10:26","slug":"struts2-regexfieldvalidator","status":"publish","type":"post","link":"https:\/\/arison.jp\/blog\/2012\/05\/11\/struts2-regexfieldvalidator\/","title":{"rendered":"\u3010Java\u3011Struts2 RegexFieldValidator \u30c8\u30ea\u30e0\u4e8b\u4ef6"},"content":{"rendered":"<p>\u4eca\u65e5\u306e\u306f\u307e\u308a\u3002<\/p>\n<p><span class=\"font24px\">Struts 2\u306evalidate \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e\u554f\u984c\u3002<\/span><\/p>\n<p><span class=\"font20px\">RegexFieldValidator\u304c\u5185\u90e8\u3067\u52dd\u624b\u306b\u30c8\u30ea\u30e0\u3059\u308b\u304b\u3089\u534a\u89d2\u30b9\u30da\u30fc\u30b9\u304c\u5148\u982d\u3084\u672b\u5c3e\u306b\u3042\u308b\u304b\u306e\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3067\u304d\u306a\u304b\u3063\u305f\u3002 <\/span><\/p>\n<p><!--more--><\/p>\n<p><span class=\"font20px\">RegexFieldValidator\u306e\u30bd\u30fc\u30b9\u3092\u898b\u308b\u3068\u52dd\u624b\u306b\u30c8\u30ea\u30e0\u3057\u3066\u3044\u308b\u3002<\/span><\/p>\n<p>\u30d1\u30e9\u30e1\u30bf\u3067\u30c8\u30ea\u30e0\u306e\u5b9f\u884c\u6709\u7121\u3092\u5224\u5b9a\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u306e\u306f\u4f55\u304b\u306e\u7406\u7531\u304c\u3042\u308b\u306e\u3060\u308d\u3046\u304b\u3002<\/p>\n<p><span class=\"font20px\">Struts 2\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c2\u3064\u5229\u7528\u3067\u304d\u306a\u3044\u306e\u304c\u6b8b\u5ff5\u3002<br \/>\n\u6b63\u898f\u8868\u73fe\u306f\u3001\u300c\u307e\u305f\u306f\u300d\u3092\u7528\u3044\u308b\u3057\u304b\u306a\u3044\u306e\u3060\u308d\u3046\u304b\u3002<\/span><\/p>\n<p>\u3010\u5f8c\u65e5\u8ac7\u3011<\/p>\n<p>\u6700\u7d42\u7684\u306bCustomValidator\u3092\u4f7f\u3063\u3066Validate\u3055\u305b\u305f\u3002<\/p>\n<p>RegexFieldValidator\u30af\u30e9\u30b9 \u3092 extends \u3057\u3066 XxxxRegexFieldValidator \u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u305f\u3002<br \/>\nvalidate\u30e1\u30bd\u30c3\u30c9\u3092Override\u3057\u305f\u3002<\/p>\n<p>\u3010\u30bd\u30fc\u30b9\u4e00\u90e8\u629c\u7c8b\u3011<\/p>\n<p>[java]<br \/>\npublic class XxxxRegexFieldValidator extends RegexFieldValidator {<\/p>\n<p>    private boolean doTrim = true;<\/p>\n<p>    public void setTrim(boolean trim) {<br \/>\n        doTrim = trim;<br \/>\n    }<\/p>\n<p>    public boolean getTrim() {<br \/>\n        return doTrim;<br \/>\n    }<\/p>\n<p>\t@Override<br \/>\n    public void validate(Object object) throws ValidationException {<br \/>\n        String fieldName = getFieldName();<br \/>\n        Object value = this.getFieldValue(fieldName, object);<br \/>\n        if (value == null || getExpression() == null) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        if (!(value instanceof String)) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        String str = ((String) value).trim();<br \/>\n        if (str.length() == 0) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        Pattern pattern;<br \/>\n        if (isCaseSensitive()) {<br \/>\n            pattern = Pattern.compile(getExpression());<br \/>\n        } else {<br \/>\n            pattern = Pattern.compile(getExpression(), Pattern.CASE_INSENSITIVE);<br \/>\n        }<\/p>\n<p>        String compare = (String) value;<br \/>\n        if ( doTrim ) {<br \/>\n            compare = compare.trim();<br \/>\n        }<br \/>\n        Matcher matcher = pattern.matcher( compare );<\/p>\n<p>        if (!matcher.matches()) {<br \/>\n            addFieldError(fieldName, object);<br \/>\n        }<br \/>\n    }<\/p>\n<p>\t\/**<br \/>\n     * doTrim\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<br \/>\n     * @return doTrim<br \/>\n     *\/<br \/>\n    public boolean isDoTrim() {<br \/>\n        return doTrim;<br \/>\n    }<\/p>\n<p>\t\/**<br \/>\n     * doTrim\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br \/>\n     * @param doTrim doTrim<br \/>\n     *\/<br \/>\n    public void setDoTrim(boolean doTrim) {<br \/>\n        this.doTrim = doTrim;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>\u3010validators.xml\u3011<br \/>\n[xml]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;!DOCTYPE validators PUBLIC<br \/>\n        &quot;-\/\/Apache Struts\/\/XWork Validator Config 1.0\/\/EN&quot;<br \/>\n        &quot;http:\/\/struts.apache.org\/dtds\/xwork-validator-config-1.0.dtd&quot;&gt;<br \/>\n&lt;validators&gt;<br \/>\n    &lt;validator name=&quot;xxxxregex&quot; class=&quot;jp.arison.validator.validators.XxxxRegexFieldValidator&quot;\/&gt;<br \/>\n&lt;\/validators&gt;<br \/>\n[\/xml]<br \/>\n\u3010\u4f7f\u7528\u65b9\u6cd5 \u4e00\u90e8\u629c\u7c8b\u3011<\/p>\n<p>[java]<br \/>\npublic class AriModel {<\/p>\n<p>\t\/** ID *\/<br \/>\n\tprivate String id;<\/p>\n<p>\tpublic String getId() {<br \/>\n\t\treturn id;<br \/>\n\t}<\/p>\n<p>@RequiredStringValidator(message = &quot;${getText(&#8216;\u30ea\u30bd\u30fc\u30b9\u30ad\u30fc&#8217;,{getText(&#8216;\u88dc\u9593\u7528\u306e\u30ea\u30bd\u30fc\u30b9\u30ad\u30fc&#8217;)})}&quot;)<br \/>\n@CustomValidator(type = &quot;xxxxregex&quot;, key = &quot;${getText(&#8216;\u30ea\u30bd\u30fc\u30b9\u30ad\u30fc&#8217;)}&quot;,<br \/>\n\tparameters = {<br \/>\n\t\t@ValidationParameter(name = &quot;trim&quot;, value = &quot;false&quot;),<br \/>\n\t\t@ValidationParameter(name = &quot;expression&quot;, value = &quot;\u6b63\u898f\u8868\u73fe&quot;)<br \/>\n\t})<br \/>\n\tpublic void setId(String id) {<br \/>\n\t\tthis.id = id;<br \/>\n\t}<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u65e5\u306e\u306f\u307e\u308a\u3002 Struts 2\u306evalidate \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e\u554f\u984c\u3002 RegexFieldValidator\u304c\u5185\u90e8\u3067\u52dd\u624b\u306b\u30c8\u30ea\u30e0\u3059\u308b\u304b\u3089\u534a\u89d2\u30b9\u30da\u30fc\u30b9\u304c\u5148\u982d\u3084\u672b\u5c3e\u306b\u3042\u308b\u304b\u306e\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3067\u304d\u306a\u304b\u3063\u305f\u3002<\/p>\n","protected":false},"author":2,"featured_media":17178,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4],"tags":[703,120],"class_list":["post-3724","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-diary","tag-java","tag-struts2"],"_links":{"self":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/3724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/comments?post=3724"}],"version-history":[{"count":0,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/3724\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media\/17178"}],"wp:attachment":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media?parent=3724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/categories?post=3724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/tags?post=3724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}