Home  >  Struts 2

Struts 2 Annotations: @ParentPackage

By Arvind Rai, June 11, 2014
@ParentPackage annotation in struts 2 is used to define different parent struts package. It is annotated on action class. @ParentPackage uses a name which can be declared in struts xml. In this page we will see code snippet how to use @ParentPackage in our struts 2 application. Find one use case to use @ParentPackage. Suppose we need to apply interceptor for different action class. To achieve it, define interceptor inside the package in struts xml as below.
<struts>
    <package name="myPackage" namespace="/" extends="struts-default">
        <interceptors>   
        </interceptors>      
    </package>
    <constant name="struts.devMode" value="true" />
</struts> 
We can provide any name to the package. The action class needs to be annotated with @ParentPackage to get support for all configuration done inside package in struts xml.
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;

@ParentPackage(value ="myPackage")
public class ResultAction extends ActionSupport{
     public String execute() {
	    System.out.println("ResultAction: Inside execute.");
	    return SUCCESS; 
     }
}
Parent Package can also be configured in package-info.java and all the action class of that package will use ParentPackage automatically.
For complete example refer the below URL.

Struts 2 Custom Interceptor Example Using @InterceptorRef Annotation
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us