Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Params Interceptor
2.1.
Internal working of a params Interceptor
2.2.
Life Cycle of Methods of Params Interceptor
2.3.
Parameters of Params Interceptor
2.4.
Example
3.
DefaultStack
4.
Frequently Asked Questions
4.1.
What do you mean by ValueStack in Struts2?
4.2.
What do you mean by ActionContext in struts2?
4.3.
Which design pattern is used in Struts2?
4.4.
What is the advantage of interceptors?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Params Interceptor

Introduction

In this article, we will discuss the params Interceptor. But before preceding the params interceptor, let's briefly know what the Struts and Interceptor are.

Struts2 is the combination of the webwork framework of opensymphony and struts 1.

It supports POJO-based actions, Validation Support, AJAX Support, and Integration support to various frameworks such as Hibernate, Spring, Tiles, etc.

Interceptor is an object invoked during the preprocessing and postprocessing of a request.

Interceptors Image

Params Interceptor

The params Interceptor, also known as parameters Interceptor, is used to set all the parameters on the valuestack. It gives only static values to variables rather than dynamic values.

Params Interceptor Image

We didn't need to specify the params interceptor explicitly as it is found in the defaultStack.

Internal working of a params Interceptor

Params interceptor gets all the parameters by calling the getParameters() method of ActionContext and sets it on the valuestack by calling the setValue() method of ValueStack.

Life Cycle of Methods of Params Interceptor

Life Cycle Image


Now. let's discuss the different parameters defined for the params Interceptor.

Parameters of Params Interceptor

So basically, There are four parameters defined for the params Interceptor.

Parameters Image

Example

<action name="login" class="com.Coding Ninjas Studio.Login">  
    <interceptor-ref name="params"/>  
    <result name="success">login-success.jsp</result>  
</action>  

DefaultStack

From the above, we learned that we don't have to specify the params interceptor explicitly because the params interceptor is found in the defaultstack.

So, let's discuss some defaultStack interceptors.

<interceptor-stack name="defaultStack">
    <interceptor-ref name="exception"/>  
    <interceptor-ref name="alias"/>  
    <interceptor-ref name="servletConfig"/>  
    <interceptor-ref name="prepare"/>  
    <interceptor-ref name="i18n"/>  
    <interceptor-ref name="chain"/>  
    <interceptor-ref name="debugging"/>  
    <interceptor-ref name="profiling"/>  
    <interceptor-ref name="scopedModelDriven"/>  
    <interceptor-ref name="modelDriven"/>  
    <interceptor-ref name="fileUpload"/>  
    <interceptor-ref name="checkbox"/>  
    <interceptor-ref name="staticParams"/>  
    <interceptor-ref name="actionMappingParams"/>  
    <interceptor-ref name="params">  
        <param name="excludeParams">dojo\..*,^struts\..*</param>  
    </interceptor-ref>  
    <interceptor-ref name="conversionError"/>  
    <interceptor-ref name="validation">  
        <param name="excludeMethods">input,back,cancel,browse</param>  
    </interceptor-ref>  
    <interceptor-ref name="workflow">  
        <param name="excludeMethods">input,back,cancel,browse</param>  
    </interceptor-ref>  
</interceptor-stack>  

 

The End Image

Let's see some Frequently Asked Questions related to them.

Frequently Asked Questions

What do you mean by ValueStack in Struts2?

The valueStack is a set of several objects which keeps the following objects in the provided order.

What do you mean by ActionContext in struts2?

The ActionContext is the context in which an Action is executed, or the ActionContext is thread-local, meaning that values stored in the ActionContext are unique per thread. Each context is a container of objects an action needs for execution like the session, parameters, locale, etc.

Which design pattern is used in Struts2?

MVC (Model-View-Controller) design pattern is used in Struts2.

What is the advantage of interceptors?

The advantage of an interceptor is that it is pluggable, which means that if we need to remove any concerns such as validation, exception handling, logging, etc., from the application, we don't need to redeploy it. We only have to do is to remove the entry from the struts.xml file.

Conclusion

In this article, we have discussed one of the Interceptors, i.e., the params interceptor, including its parameter and internal working of the params interceptor.

After reading about the Params Interceptor, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See JavaJSP-XMLJSPIntroduction to JSP, and  Spring Boot to learn.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass