public String intercept(ActionInvocation ai) It is summoned at each demand, it is utilized to characterize the demand handling rationale. On the off chance that it returns string, result page will be summoned, in the event that it returns conjure() technique for ActionInvocation interface, next interceptor or activity will be summoned.
public void destroy()JAVA Tutorial Video
[embed]https://www.youtube.com/watch?v=Mb8nG-NH6gg&t=2835s[/embed]Case to make custom interceptor :In this illustration, we will make custom that believers ask for preparing information into capitalized letter.You have to take after 2 stages1) Create an interceptorBy this interceptor, we are changing over the name property of activity class into capitalized letter.The getStack() technique for ActionInvocation restores the reference of ValueStack.We are getting the esteem set in the name property by find String technique for ValueStack. The set technique for ValueStack sets the name property by the predefined esteem. In such case, we are changing over the estimation of name property into capitalized letter and putting away it into the valuestack.
MyInterceptor.javapackage com;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;import com.opensymphony.xwork2.util.ValueStack;public class MyInterceptor implements Interceptor{public void init() {}public String intercept(ActionInvocation ai) throws Exception {ValueStack stack=ai.getStack();String s=stack.findString("name");stack.set("name",s.toUpperCase());return ai.invoke();}public void destroy() {}}2) Define the entry in the struts.xml file
To characterize the interceptor, we have to pronounce an interceptor first. The component of bundle is utilized to determine interceptors. The component of interceptors is utilized to characterize the custom interceptor. Here, we are characterizing the custom .The interceptor-ref subelement of activity determines the that will be connected for this activity. Here, we are determining the defaultstack interceptors .struts.xmlThe params interceptor otherwise called parameters interceptor is utilized to set all parameters on the valuestack. It is found in the default stack bydefault. So you don't have to indicate it explicitely.
Inner working of params interceptor
It gets all parameters by calling the getParameters() strategy for ActionContext and sets it on the valuestack by calling the setValue() technique for ValueStack.
Parameters of params interceptor
Parameter |
ordered |
paramNameMaxLength |
excludeParams |
acceptParamNames |
The execAndWait interceptor otherwise called execute and hold up interceptor is utilized to show the middle of the road result.
It is prescribed to use for long running activity.
It is not found in the default stack by default. So you have to determine it explicitly.
On the off chance that you don't determine "hold up" result, struts system shows a middle outcome until the point when your demand is finished.
For the custom middle of the road result, you have to characterize "hold up" result in struts.xml document. In your page, you can show handling picture and so forth. In this way, it is smarter to determine the custom outcome.
Parameters of execAndWait interceptor
Parameter |
delay |
delaySleepInterval |
threadPriority |
Parameters of prepare interceptor
Parameter | Description |
alwaysInvokePrepare | It is set to true bydefault. |
Action class
package com.javaspot;import com.opensymphony.xwork2.Preparable;public class LoginAction implements Preparable{private String name,password;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public void prepare() throws Exception {System.out.println("preparation logic");}public String execute(){System.out.println("actual logic");return "success";}}modelDriven interceptor The modelDriven interceptor makes other model question as the default protest of valuestack.To utilize the modelDriven interceptor, you have to actualize ModelDriven interface in your activity class and abrogate its strategy getModel(). It is found in the default stack by-default. So you don't have to determine it explicitly. In our web application, there might happen special case anytime.Exception Handling - exception interceptorTo defeat this issue, struts 2 gives an instrument of worldwide special case taking care of where we can show a worldwide outcome to the client.Struts 2 consequently log the uncaught exemptions and sidetracks the client to the blunder handler page.Understanding the inside working of special case interceptor On the off chance that there happens special case, it is wrapped in ExceptionHolder and pushed in the valuestack with the goal that we can without much of a stretch get to exemption question from the outcome.Parameters of exception interceptor
Parameter |
logEnabled |
logLevel |
logCategory |
The fileUpload interceptor naturally works for every one of the solicitations that incorporates documents. We can utilize this interceptor to control the working of document transfer in struts2, for example, characterizing permitted sorts, most extreme record estimate.
Parameters of fileupload interceptor
Parameter | Description |
maximumSize | specifies maximum size of the file to be uploaded. |
allowedTypes | specifies allowed types. It may be image/png, image/jpg etc. |
Automatically added parameters
- String fileName represents the filename of the file.
- String contentType specifies the content type of the file.