Struts 2 Interview Questions and Answers

March 21, 2015
Find the struts 2 interview questions and answers. This will help us for struts 2 interview preparations.

Qns-1: What is the role of StrutsPrepareAndExecuteFilter in Struts 2

Ans: StrutsPrepareAndExecuteFilter is a Struts 2 filter which is configured in web.xml. We need to provide init parameter as actionPackages that is the package where all the action classes has been kept. StrutsPrepareAndExecuteFilter scans the action classes. Find the sample web.xml for Struts 2 with StrutsPrepareAndExecuteFilter configuration.
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.concretepage.action</param-value>
        </init-param>
    </filter>
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping> 

Qns-2: How to create Action class in Struts 2 using Annotation?

Ans: To create action class we need to use different annotations like @Namespace, @Action, @ResultPath and @Result etc. Find the sample action class.
@Namespace("/emp")
@Action("/login")
@ResultPath(value="/")
@Result(name="success",location="login.jsp")
public class LoginAction extends ActionSupport{
	public String execute() {
	    return SUCCESS;
	}
} 
com.opensymphony.xwork2.ActionSupport is a class that defines some String constant like SUCCESS and FAILURE to redirect the page.

Qns-3: How to redirect to a page in struts 2 annotation?

Ans: Using @Results, we assign a result page. Result page can be a success page or error page. Create action method and return the name of result.
@Results({
	  @Result(name="success", location="/user/user.jsp")
	})
public class EmpAction  {
	@Action("/one")
	public String one() {
	    return "success";
	}
} 
Using @Result, we can define many pages to redirect as required. The location properties defines the page location.

Qns-4: How to define result page using XML in Struts 2?

Ans: After execution of action method, we need to go to result page. In struts 2, using XML, we can do as below.
	<action name="result" class="com.concretepage.action.ResultAction">
	<result name="SUCCESS">result.jsp</result>
	<result name="ERROR">error.jsp</result>	     
Within the action tag, define the result tag in which we will assign values to the name attribute.

Qns-5: How to access values on JSP set by action class?

Ans: Create a setter and getter method for a property in action class and access in JSP by configuring struts tag library. Define the tag library as
<%@ taglib prefix="s" uri="/struts-tags" %>
Then we need to access in JSP by property attribute of tag.
<s:property value="userName"/> userName is a property in action class with setter and getter method.

Qns-6: How to do validation in Struts 2 using annotation?

Ans: Struts 2 provides different annotations for validation like @RequiredStringValidator, @EmailValidator and @IntRangeFieldValidator etc. In our code we can use it as
@RequiredStringValidator(message = "Enter your Name.")
public String getName() {
	return name;
} 

Qns-7: How to use Message Resource in struts 2?

Ans: Message Resource can be global or action class specific. For action class specific, create a configuration file with action class name and language code. Suppose we have an action named as StudentAction, our property file name for English code will be as StudentAction_en.properties where we define key value pair for messages. It can be like student.name= Mohan, we can use it in action class as
@RequiredStringValidator(key="student.name")
public String getName() {
	return name;
} 
And in JSP we can use it as <s:textfield name="name" key=" student.name "/>

Qns-8: How to create custom interceptor in struts 2?

Ans: To implement a custom interceptor, we need to follow below steps.
1. Create a class implementing com.opensymphony.xwork2.interceptor.Interceptor interface and define its methods that are destroy(), init() and intercept().
2. Define interceptor in struts.xml using interceptors tag as
    <interceptors>   
      <interceptor name="myCustomInterceptor" class="com.concretepage.interceptor.MyCustomInterceptor" />
         <interceptor-stack name="customStack">
            <interceptor-ref name="myCustomInterceptor"/>
         </interceptor-stack>
    </interceptors> 
3. Use this interceptor in action class with @InterceptorRef annotation as
@InterceptorRef(value="customStack")
@ParentPackage(value ="default")
public class ResultAction {
}

Qns- 9: How to do exception handling in struts 2?

Ans: To handle exception in struts 2 means, if our code is throwing exception, in that case which page we need to display. Struts 2 facilitates that for a specific type of exception, we can redirect to specific page. Struts 2 provides @ExceptionMappings annotation which can be used as
@ExceptionMappings({
    @ExceptionMapping(exception = "java.lang.NullPointerException", result = "failure", 
	                                    params = {"param1", "val1"})
})
It means, if code throws NullPointerException, then result property are being set to any result name and the associated page with that result name will be redirected. The result can be defined as
@Results({
	  @Result(name="failure", location="error.jsp"),
})

Qns-10: How to upload file in struts 2?

To upload file in struts, @InterceptorRef annotation is used in which we can define allowedTypes and maximumSize etc as below.
@InterceptorRef(
	params={"allowedTypes","image/jpeg,application/zip",
	    "maximumSize","1000000"}, 
	    value="fileUpload"
)







©2024 concretepage.com | Privacy Policy | Contact Us