Did you mean to have only one interceptor in your stack?
You stack has only one interceptor and none which invokes the validate()
method. Perhaps you can try having your stack like this:
<interceptors>
<interceptor-stack name="sessionIntercpetorStack">
<interceptor-ref name="defaultStack">
<interceptor-ref name="sessionInterceptor" />
</interceptor-stack>
</interceptors>
OR better soln is
You can copy over the default stack and replace the WorkFlowInterceptor with
your own.
Also, ActionSupport already implements the Validateable, so you only need to
extend it (no need for "implements Validateable").
-Pankaj
-----Original Message-----
From: Nikul Suthar [mailto:[email protected]]
Sent: Wednesday, June 08, 2011 10:03 AM
To: [email protected]
Subject: Validate method not working in Action after using Interceptor
Hi There,
I'm pretty new to using Struts 2 for developing a web application. So I
would be very thankful if someone can clear out this roadblock for me.
I'm trying to write some custom validation code in method *public void
validate()* in the Action class. I also created an interceptor extending *
DefaultWorkflowInterceptor* checking for session object validity. But since
I implemented the interceptor my validate method does not get executed at
all. I need the validate method to be executed to implement custom field
validations every time the request is received. Since I'm new to using
Struts 2 this is the best design I could come up with. Your help in making
the validate method execute every time the request is received without
removing the interceptor will be highly appreciated. Following is my code:
*ShowChangePassword2Of2Action*
*
*
public class ShowChangePassword2Of2Action extends ActionSupport implements
Validateable{
[Field variables]
public String show(){
[My business logic]
}
public void validate(){
[My validation logic]
}
[Field variables getters and setters]
}
*SessionInterceptor*
*
*
public class SessionInterceptor extends DefaultWorkflowInterceptor{
private static final long serialVersionUID = 1L;
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext context = invocation.getInvocationContext();
Map<String, Object> sessionMap = context.getSession();
if(sessionMap == null || sessionMap.isEmpty()){
System.out.println("Session expired...");
return "sessionExpired";
}//if
else{
return invocation.invoke();
}//else
}
public void destroy() {
System.out.println("Destroying Session Interceptor...");
}
public void init() {
System.out.println("Initializing Session Interceptor...");
}
}
*struts.xml*
*
*
<struts>
<package name="default" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"
/>
</result-types>
<interceptors>
<interceptor name="sessionInterceptor"
class="org.zssinfo.interceptor.SessionInterceptor"></interceptor>
<interceptor-stack name="sessionInterceptorStack">
<interceptor-ref name="sessionInterceptor" />
</interceptor-stack>
</interceptors>
<global-results>
<result name="sessionExpired" type="tiles">welcome</result>
</global-results>
<action name="*ChangePassword2Of2" method="{1}"
class="org.zssinfo.action.ShowChangePassword2Of2Action">
<interceptor-ref name="sessionInterceptorStack"></interceptor-ref>
<result name="success" type="tiles">showChangePassword2Of2</result>
<result name="showChangePassword2Of2Error" type="tiles">errorPage</result>
<result name="input" type="tiles">showChangePassword1Of2</result>
</action>
</package>
</struts>
Thank you very much in advance.
Thanks,
Nikul
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]