Struts2 Bean Validation -


i setting stuts2 app using hibernate bean validation validate form fields on login page. have added necessary plugins , dependencies , configured struts.xml include custom stack calls beanvalidation interceptor. /logon path excluded authinterceptor. unfortunately, when form fields email , password populated, return message blank. can see in debugger email , password being set in loginaction class it's if beanvalidation doesn't have information. if switch order of interceptors have beanvalidation last, never called.

  <?xml version="1.0" encoding="utf-8"?> <!doctype struts public    "-//apache software foundation//dtd struts configuration 2.0//en"    "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>     <constant name="struts.devmode" value="false" />     <constant name="struts.action.extension" value="," />     <constant name="struts.custom.i18n.resources" value="login,signup" />     <constant name="struts.mapper.alwaysselectfullnamespace"         value="false" />     <constant name="struts.beanvalidation.providerclass" value="org.hibernate.validator.hibernatevalidator" />     <constant name="struts.beanvalidation.ignorexmlconfiguration"         value="false" />     <constant name="struts.beanvalidation.convertmessagetoutf"         value="false" />     <constant name="struts.beanvalidation.convertmessagefromencoding"         value="iso-8859-1" />     <constant name="struts.enable.slashesinactionnames" value="true" />     <package name="defaultpackage" namespace="/" extends="struts-default">         <interceptors>             <interceptor name="authinterceptor"                 class="com.mypackage.interceptor.authenticationinterceptor">             </interceptor>             <interceptor name="beanvalidation"                 class="org.apache.struts.beanvalidation.validation.interceptor.beanvalidationinterceptor" />             <interceptor-stack name="authstack">                 <interceptor-ref name="defaultstack" />                 <interceptor-ref name="beanvalidation">                     <param name="validateannotatedmethodonly">true</param>                     <param name="excludemethods">input,back,cancel,browse</param>                 </interceptor-ref>                 <interceptor-ref name="authinterceptor" />             </interceptor-stack>         </interceptors>         <default-interceptor-ref name="authstack"></default-interceptor-ref>         <global-results>             <result name="login">/jsp/login.jsp</result>         </global-results>         <action name="login" class="com.mypackage.action.loginaction"             method="login">             <interceptor-ref name="authstack">                 <param name="excludemethods">login</param>             </interceptor-ref>             <result name="login">/jsp/login.jsp</result>             <result name="input">/jsp/login.jsp</result>         </action>         <action name="logon" class="com.mypackage.action.loginaction"             method="dologon">             <interceptor-ref name="authstack">                 <param name="authinterceptor.excludemethods">dologon</param>             </interceptor-ref>             <result name="input">/jsp/login.jsp</result>             <result name="success">/jsp/login.jsp</result>         </action>         <action name="password/reset" class="com.mypackage.action.passwordresetaction"             method="resetpassword">             <interceptor-ref name="authinterceptor">                 <param name="excludemethods">resetpassword</param>             </interceptor-ref>             <result name="reset-password">/jsp/password_reset_html.jsp</result>         </action>         <action name="register" class="com.mypackage.action.registeraction"             method="register">             <interceptor-ref name="authinterceptor">                 <param name="excludemethods">register</param>             </interceptor-ref>             <result name="register">/jsp/register_html.jsp</result>         </action>         <action name="doregistration" class="com.mypackage.action.registeraction">             <interceptor-ref name="authinterceptor">                 <param name="excludemethods">resetpassword</param>             </interceptor-ref>         </action>     </package> </struts> 

html

         <div class="fields-container">             <div class="field-div">                 <div>                     <span><s:text name="login.email" /></span>                 </div>                 <label class="form-label"> <input type="text" name="email"                     value="" />                 </label>             </div>             <div class="field-div">                 <div>                     <span><s:text name="login.password" /></span>                 </div>                 <label class="form-label"> <input type="text"                     name="password" value="" />                 </label>             </div>         </div> 

action class

public class loginaction extends baseaction {  /**  *   */ private static final long serialversionuid = 1l;  private static final logger log = loggerfactory.getlogger(loginaction.class);  @notnull(message = "{login.error.email.empty}") private string email;  @notnull(message = "{login.error.password.empty}") private string password;  public string login() {     return actionconstants.login; }  /**  * perform authentication of user.  *   * @return result string maps jsp page.  */ public string dologon() {     string result = null;     return result; }  public string getemail() {     return email; }  public void setemail(string email) {     this.email = email; }  public string getpassword() {     return password; }  public void setpassword(string password) {     this.password = password; } 

edit 6/10/16

i changed order of execution interceptors defaultstack go first understanding it's responsible populating beans in actionclass. now, validation occurring should. however, if leave of fields blank, proper error message returned actionclass method dologon still being called. trying figure out how beanvalidation short circuit actionclass not called.

so appears org.apache.struts.beanvalidation.validation.interceptor. beanvalidationinterceptor not meant short circuit execution of other interceptors. in version 2.5 of struts bean validation plugin, dointercept method returns invocation.invoke() indicate if there errors, execution continue.

    protected string dointercept(actioninvocation invocation)     throws exception   {     validator validator = this.beanvalidationmanager.getvalidator();     if (validator == null)     {       log.debug("there no bean validator configured in class path. skipping bean validation..");       return invocation.invoke();     }     log.debug("starting bean validation using validator: {}", new object[] { validator.getclass() });      object action = invocation.getaction();     actionproxy actionproxy = invocation.getproxy();     string methodname = actionproxy.getmethod();     if (log.isdebugenabled()) {       log.debug("validating [{}/{}] method [{}]", new object[] { invocation.getproxy().getnamespace(), invocation.getproxy().getactionname(), methodname });     }     collection<method> annotatedmethods = annotationutils.getannotatedmethods(action.getclass(), new class[] { skipvalidation.class });     if (!annotatedmethods.contains(getactionmethod(action.getclass(), methodname))) {       performbeanvalidation(action, validator);     }     return invocation.invoke();   } 

because not want actionclass method called if there errors, created copy of beanvalidationinterceptor.class , added struts.xml. new class has instance variable storing constraintviolations , dointercept method returns "input" if there errors.

private set<constraintviolation<object>> constraintviolations;  protected string dointercept(actioninvocation invocation) throws exception {     validator validator = this.beanvalidationmanager.getvalidator();     if (validator == null) {         log.debug("there no bean validator configured in class path. skipping bean validation..");         return invocation.invoke();     }     log.debug("starting bean validation using validator: {}", new object[] { validator.getclass() });      object action = invocation.getaction();     actionproxy actionproxy = invocation.getproxy();     string methodname = actionproxy.getmethod();     if (log.isdebugenabled()) {         log.debug("validating [{}/{}] method [{}]", new object[] { invocation.getproxy().getnamespace(),                 invocation.getproxy().getactionname(), methodname });     }     collection<method> annotatedmethods = annotationutils.getannotatedmethods(action.getclass(),             new class[] { skipvalidation.class });     if (!annotatedmethods.contains(getactionmethod(action.getclass(), methodname))) {         performbeanvalidation(action, validator);     }     return this.constraintviolations.isempty() ? invocation.invoke() : "input"; } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -