Explore topic-wise InterviewSolutions in Current Affairs.

This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.

1.

What Is The Default Location Of Result Pages And How Can We Change It?

Answer»

By DEFAULT Struts2 looks for result pages in {WEBAPP-ROOT}/{Namespace}/ DIRECTORY but sometimes we want to keep result pages in another LOCATION, we can provide struts.convention.result.path constant value in Struts2 configuration FILE to change the result pages location. 

Another way is to use @ResultPath annotation in action CLASSES to provide the result pages location.

By default Struts2 looks for result pages in {WEBAPP-ROOT}/{Namespace}/ directory but sometimes we want to keep result pages in another location, we can provide struts.convention.result.path constant value in Struts2 configuration file to change the result pages location. 

Another way is to use @ResultPath annotation in action classes to provide the result pages location.

2.

What Are Best Practices To Follow While Developing Struts2 Application?

Answer»

Some of the best practices while developing Struts2 application are: 

  • Always try to extend struts-default package while creating your package to AVOID code redundancy in configuring interceptors. 
  • For common tasks across the application, such as logging request params, try to use interceptors. 
  • Always keep action classes java bean properties in a separate bean for code reuse and implement ModelDriven interface. 
  • If you have custom interceptor that you will use in multiple actions, create interceptor stack for that and then use it. 
  • Try to divide your application in different modules with namespace CONFIGURATION BASED on functional areas. 
  • Try to use Struts2 tags in result pages for code CLARIFY, if needed create your own type converters. 
  • Use DEVELOPMENT mode for faster development, however make sure production code doesn’t run in dev mode. 
  • Use Struts2 i18n support for resource bundles and to support localization. 
  • Struts2 provides a lot of places where you can have resource bundles but try to keep one global resource bundle and one for action class to avoid confusion. 
  • struts-default package configures all the interceptors and creates different interceptor stacks. Try to use only what is needed, for example if you don’t have localization requirement, you can avoid i18n interceptor.

Some of the best practices while developing Struts2 application are: 

3.

Which Interceptor Is Responsible For Mapping Request Parameters To Action Class Java Bean Properties?

Answer»

com.opensymphony.xwork2.INTERCEPTOR.ParametersInterceptor interceptor is responsible for MAPPING request parameters to the ACTION class java bean properties. This interceptor is CONFIGURED in struts-default package with name “params”. This interceptor is PART of basicStack and defaultStack interceptors stack.

com.opensymphony.xwork2.interceptor.ParametersInterceptor interceptor is responsible for mapping request parameters to the Action class java bean properties. This interceptor is configured in struts-default package with name “params”. This interceptor is part of basicStack and defaultStack interceptors stack.

4.

What Is The Use Of Namespace In Action Mapping In Struts2?

Answer»

Struts2 namespace configuration allows us to create modules easily. We can USE namespace to separate our action classes BASED on their FUNCTIONALITY, for EXAMPLE admin, USER, customer etc.

Struts2 namespace configuration allows us to create modules easily. We can use namespace to separate our action classes based on their functionality, for example admin, user, customer etc.

5.

How Can We Upload Files In Struts2 Application?

Answer»

File Upload is one of the common TASK in a web APPLICATION. Thats why Struts2 provides built in support for file upload through FileUploadInterceptor. This INTERCEPTOR is CONFIGURED in struts-default package and PROVIDE options to set the maximum size of a file and file types that can be uploaded to the server.

File Upload is one of the common task in a web application. Thats why Struts2 provides built in support for file upload through FileUploadInterceptor. This interceptor is configured in struts-default package and provide options to set the maximum size of a file and file types that can be uploaded to the server.

6.

Provide Some Important Struts2 Constants That You Have Used?

Answer»

Some of the Struts2 constants that I have used are: 

  • struts.devMode to run our APPLICATION in DEVELOPMENT mode. This mode does reload properties files and provides extra LOGGING and debugging feature. It’s very useful while developing our application but we should turn it off while MOVING our code to production. 
  • struts.convention.result.path to configure the location of result PAGES. By default Struts2 look for result pages at {WEBAPP-ROOT}/{Namespace}/ and we can change the location with this constant. 
  • struts.custom.i18n.resources to define global resource bundle for i18n support. 
  • struts.action.extension to configure the URL suffix to for Struts2 application. Default suffix is .action but sometimes we might want to change it to . do or something else. 

We can configure above constants in struts.xml file like below. 

< constant name="struts.devMode" value="true" > < /constant >

< constant name="struts.action.extension" value="action,do" > < /constant >

< constant name="struts.custom.i18n.resources" value="global" > < /constant >

< constant name="struts.convention.result.path" value="/" > < /constant >

Some of the Struts2 constants that I have used are: 

We can configure above constants in struts.xml file like below. 

< constant name="struts.devMode" value="true" > < /constant >

< constant name="struts.action.extension" value="action,do" > < /constant >

< constant name="struts.custom.i18n.resources" value="global" > < /constant >

< constant name="struts.convention.result.path" value="/" > < /constant >

7.

What Is The Default Suffix For Struts2 Action Uri And How Can We Change It?

Answer»

The default URI suffix for STRUTS2 action is .action, in Struts1 default suffix was .do. We can change this suffix by defining struts.action.extension constant VALUE in our Struts2 configuration file as:

< constant name="struts.action.extension" value="action,do" &GT; < /constant >

The default URI suffix for Struts2 action is .action, in Struts1 default suffix was .do. We can change this suffix by defining struts.action.extension constant value in our Struts2 configuration file as:

< constant name="struts.action.extension" value="action,do" > < /constant >

8.

What Is An Interceptor Stack?

Answer»

An interceptor stack helps us to group TOGETHER multiple INTERCEPTORS in a package for further use. struts-default package creates some of the MOSTLY used interceptor stack – basicStack and defaultStack. We can create our own interceptor stack at the start of the package and then configure our ACTION classes to use it.

An interceptor stack helps us to group together multiple interceptors in a package for further use. struts-default package creates some of the mostly used interceptor stack – basicStack and defaultStack. We can create our own interceptor stack at the start of the package and then configure our action classes to use it.

9.

Name Some Useful Annotations Introduced In Struts2?

Answer»

Some of the important ANNOTATIONS introduced in Struts2 are: 

  • @Action to CREATE action CLASS
  • @Actions to configure single class for MULTIPLE actions
  • @Namespace and @Namespaces for creating different modules
  • @Result for result PAGES
  • @ResultPath for configuring result pages location

Some of the important annotations introduced in Struts2 are: 

10.

What Is Struts-default Package And What Are It’s Benefits?

Answer»

struts-default is an abstract package that defines all the Struts2 interceptors and commonly used interceptor stack. It is advisable to extend this package while configuring our application package to avoid configuring interceptors again. This is PROVIDED to HELP DEVELOPERS by eliminating the TRIVIAL task of configuring interceptor and result pages in our application.

struts-default is an abstract package that defines all the Struts2 interceptors and commonly used interceptor stack. It is advisable to extend this package while configuring our application package to avoid configuring interceptors again. This is provided to help developers by eliminating the trivial task of configuring interceptor and result pages in our application.

11.

What Is Life Cycle Of An Interceptor?

Answer»

Interceptor interface defines three methods – INIT(), destroy() and intercept(). init and destroy are the life CYCLE methods of an interceptor. Interceptors are SINGLETON classes and STRUTS2 initialize a new thread to handle each request. init() method is called when interceptor instance is CREATED and we can initialize any resources in this method. destroy() method is called when application is shutting down and we can release any resources in this method. 

Interceptor interface defines three methods – init(), destroy() and intercept(). init and destroy are the life cycle methods of an interceptor. Interceptors are Singleton classes and Struts2 initialize a new thread to handle each request. init() method is called when interceptor instance is created and we can initialize any resources in this method. destroy() method is called when application is shutting down and we can release any resources in this method. 

12.

What Is Valuestack And Ognl?

Answer»

VALUESTACK is the storage area where the application data is stored by Struts2 for processing the client requests. The data is stored in ACTIONCONTEXT objects that use ThreadLocal to have values specific to the particular request thread. 

Object-Graph Navigation Language (OGNL) is a POWERFUL Expression Language that is USED to manipulate data stored on the ValueStack. As you can SEE in architecture diagram, both interceptors and result pages can access data stored on ValueStack using OGNL.

ValueStack is the storage area where the application data is stored by Struts2 for processing the client requests. The data is stored in ActionContext objects that use ThreadLocal to have values specific to the particular request thread. 

Object-Graph Navigation Language (OGNL) is a powerful Expression Language that is used to manipulate data stored on the ValueStack. As you can see in architecture diagram, both interceptors and result pages can access data stored on ValueStack using OGNL.

13.

What Are The Benefits Of Interceptors In Struts2?

Answer»

Some of the benefits of interceptors are: 

  • INTERCEPTOR plays a crucial ROLE in achieving high level of separation of concerns.
  • Struts2 interceptors are configurable, we can configure it for any action we WANT.
  • We can create our own custom interceptors to perform some COMMON tasks such as request params logging, authentication etc. This helps us in taking care of common tasks at a single LOCATION, achieving low maintenance cost.
  • We can create interceptors stack to use with different actions.

Some of the benefits of interceptors are: 

14.

What Is Custom Type Converter In Struts2?

Answer»

Struts2 SUPPORT OGNL expression language and it performs two important tasks in Struts 2 – data transfer and type conversion. 

OGNL is flexible and we can easily extend it to create our own custom converter class. Creating and CONFIGURING custom type converter class is very easy, FIRST step is to fix the input format for the custom class. Second step is to implement the converter class. Type converter classes should implement com.opensymphony.xwork2.conversion.TypeConverter interface. SINCE in web application, we always get the request in form of String and SEND response in the form of String, Struts 2 API provides a default implementation of TypeConverter interface, StrutsTypeConverter. StrutsTypeConverter contains two abstract methods – convertFromString to convert String to Object and convertToString to convert Object to String.

Struts2 support OGNL expression language and it performs two important tasks in Struts 2 – data transfer and type conversion. 

OGNL is flexible and we can easily extend it to create our own custom converter class. Creating and configuring custom type converter class is very easy, first step is to fix the input format for the custom class. Second step is to implement the converter class. Type converter classes should implement com.opensymphony.xwork2.conversion.TypeConverter interface. Since in web application, we always get the request in form of String and send response in the form of String, Struts 2 API provides a default implementation of TypeConverter interface, StrutsTypeConverter. StrutsTypeConverter contains two abstract methods – convertFromString to convert String to Object and convertToString to convert Object to String.

15.

What Are Different Struts2 Tags? How Can We Use Them?

Answer»

Struts2 provides a LOT of custom TAGS that we can USE in result pages to create views for client request. These tags are broadly divided into three categories- DATA tags, Control tags and UI tags. 

We can use these tags by adding these in JSP pages USING taglib directive.

< %@ taglib uri="/struts-tags" prefix="s" % >>

Struts2 provides a lot of custom tags that we can use in result pages to create views for client request. These tags are broadly divided into three categories- Data tags, Control tags and UI tags. 

We can use these tags by adding these in JSP pages using taglib directive.

< %@ taglib uri="/struts-tags" prefix="s" % >>

16.

Which Class Is The Front Controller In Struts2?

Answer»

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter is the Front CONTROLLER class in Struts2 and EVERY request PROCESSING STARTS from this class. Earlier versions of Struts2 uses org.apache.struts2.dispatcher.FilterDispatcher as Front Controller class

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter is the Front Controller class in Struts2 and every request processing starts from this class. Earlier versions of Struts2 uses org.apache.struts2.dispatcher.FilterDispatcher as Front Controller class

17.

How Can We Integrate Log4j In Struts2 Application?

Answer»

Struts2 PROVIDES EASY integration of log4j API for LOGGING purpose, all we NEED to have is log4j CONFIGURATION file in the WEB-INF/classes directory.

Struts2 provides easy integration of log4j API for logging purpose, all we need to have is log4j configuration file in the WEB-INF/classes directory.

18.

What Is The Use Of Token Interceptor In Struts2?

Answer»

One of the major problems with web applications is the double form submission. If not taken care, double form submission COULD result in CHARGING double amount to CUSTOMER or updating database values twice. We can use TOKEN interceptor to SOLVE the double form submission problem. This interceptor is defined in struts-default package but it’s not part of any interceptor stack, so we need to include it manually in our action classes.

One of the major problems with web applications is the double form submission. If not taken care, double form submission could result in charging double amount to customer or updating database values twice. We can use token interceptor to solve the double form submission problem. This interceptor is defined in struts-default package but it’s not part of any interceptor stack, so we need to include it manually in our action classes.

19.

Does Struts2 Action And Interceptors Are Thread Safe?

Answer»
  • STRUTS2 ACTION classes are thread safe because an object is INSTANTIATED for every request to handle it. 
  • Struts2 interceptors are SINGLETON classes and a new thread is created to handle the request, so it’s not thread safe and we need to implement them carefully to avoid any issues with shared data.

20.

What Is The Use Of Execandwait Interceptor?

Answer»

Struts2 PROVIDES execAndWait interceptor for long running action classes. We can use this interceptor to return an intermediate RESPONSE page to the client and once the processing is FINISHED, final response is RETURNED to the client. This interceptor is defined in the struts-default package and implementation is present in ExecuteAndWaitInterceptor class.

Struts2 provides execAndWait interceptor for long running action classes. We can use this interceptor to return an intermediate response page to the client and once the processing is finished, final response is returned to the client. This interceptor is defined in the struts-default package and implementation is present in ExecuteAndWaitInterceptor class.

21.

What Are Different Ways To Create Action Classes In Struts2?

Answer»

Struts2 PROVIDE different WAYS to create action classes. 

  • By implementing Action interface
  • Using Struts2 @Action annotation
  • By extending ACTIONSUPPORT class
  • Any normal java class with execute() method returning String can be CONFIGURED as Action class.

Struts2 provide different ways to create action classes. 

22.

How Can We Get Servlet Api Request, Response, Httpsession Etc Objects In Action Classes?

Answer»

Struts2 action CLASSES doesn’t PROVIDE direct access to Servlet API components such as Request, Response and Session. However sometimes we need these access in action classes such as checking HTTP METHOD or setting cookies in response. 

That's why Struts2 API provides a bunch of *Aware interfaces that we can IMPLEMENT to access these objects. Struts2 API uses DEPENDENCY injection to inject Servlet API components in action classes. Some of the important Aware interfaces are SessionAware, ApplicationAware, ServletRequestAware and ServletResponseAware.

Struts2 action classes doesn’t provide direct access to Servlet API components such as Request, Response and Session. However sometimes we need these access in action classes such as checking HTTP method or setting cookies in response. 

That's why Struts2 API provides a bunch of *Aware interfaces that we can implement to access these objects. Struts2 API uses dependency injection to inject Servlet API components in action classes. Some of the important Aware interfaces are SessionAware, ApplicationAware, ServletRequestAware and ServletResponseAware.

23.

What Is The Difference In Using Action Interface And Actionsupport Class For Our Action Classes, Which One You Would Prefer?

Answer»

We can implement Action interface to CREATE our action classes. This interface has a single method execute() that we NEED to implement. The only benefit of using this interface is that it contains some constants that we can use for result pages, these constants are SUCCESS, ERROR, NONE, INPUT and LOGIN. 

ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. ActionSupport class implements Action, Validateable, ValidationAware, TextProvider and LocaleProvider interfaces. We can override validate() method of ActionSupport class to include field LEVEL validation login in our action classes. 

Depending on the REQUIREMENTS, we can use any of the approaches to create struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n LOGIC easily in action classes.

We can implement Action interface to create our action classes. This interface has a single method execute() that we need to implement. The only benefit of using this interface is that it contains some constants that we can use for result pages, these constants are SUCCESS, ERROR, NONE, INPUT and LOGIN. 

ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. ActionSupport class implements Action, Validateable, ValidationAware, TextProvider and LocaleProvider interfaces. We can override validate() method of ActionSupport class to include field level validation login in our action classes. 

Depending on the requirements, we can use any of the approaches to create struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n logic easily in action classes.

24.

Which Interceptor Is Responsible For I18n Support?

Answer»

com.opensymphony.xwork2.interceptor.I18nInterceptor interceptor is RESPONSIBLE for I18N SUPPORT in STRUTS2 applications. This interceptor is CONFIGURED in struts-default package with name “i18n” and it’s part of i18nStack and defaultStack.

com.opensymphony.xwork2.interceptor.I18nInterceptor interceptor is responsible for i18n support in Struts2 applications. This interceptor is configured in struts-default package with name “i18n” and it’s part of i18nStack and defaultStack.

25.

What Are Struts2 Core Components?

Answer»

Struts2 CORE components are: A.Action CLASSES B.Interceptors C.Result Pages, JSP of FreeMarker TEMPLATES D.ValueStack, OGNL and TAG LIBRARIES .

Struts2 core components are: A.Action Classes B.Interceptors C.Result Pages, JSP of FreeMarker templates D.ValueStack, OGNL and Tag Libraries .

26.

What Is The Purpose Of @typeconversion Annotation Annotation?

Answer»

This ANNOTATION annotation is used for class and APPLICATION wide CONVERSION rules. The TypeConversion annotation can be applied at property and method level.

@TypeConversion(rule = ConversionRule.COLLECTION, 

CONVERTER = "java.util.String")

public VOID setUsers( List users ) {

this.users = users;

}

This annotation annotation is used for class and application wide conversion rules. The TypeConversion annotation can be applied at property and method level.

@TypeConversion(rule = ConversionRule.COLLECTION, 

converter = "java.util.String")

public void setUsers( List users ) {

this.users = users;

}

27.

What Is The Purpose Of @keyproperty Annotation Annotation?

Answer»

This annotation SETS the KeyProperty for type conversion. The KeyProperty annotation must be applied at field or method level.

@KeyProperty( value = "USERNAME" )

protected List<User> USERS = NULL;

This annotation sets the KeyProperty for type conversion. The KeyProperty annotation must be applied at field or method level.

@KeyProperty( value = "userName" )

protected List<User> users = null;

28.

What Is The Purpose Of @key Annotation Annotation?

Answer»

This ANNOTATION SETS the KEY for type CONVERSION. The Key annotation must be applied at field or method level.

@Key( value = java.lang.Long.class )

private Map<Long, USER> userMap;

This annotation sets the Key for type conversion. The Key annotation must be applied at field or method level.

@Key( value = java.lang.Long.class )

private Map<Long, User> userMap;

29.

What Is The Purpose Of @element Annotation Annotation?

Answer»

This ANNOTATION sets the Element for type conversion. The Element annotation must be applied at FIELD or method level.

@Element( VALUE = com.acme.User )

private List&LT;User> userList;

This annotation sets the Element for type conversion. The Element annotation must be applied at field or method level.

@Element( value = com.acme.User )

private List<User> userList;

30.

What Is The Purpose Of @createifnull Annotation Annotation?

Answer»

This annotation sets the CreateIfNull for type CONVERSION. The CreateIfNull annotation must be applied at field or METHOD level.

@CreateIfNull( value = true )

private List&LT;User> users;

This annotation sets the CreateIfNull for type conversion. The CreateIfNull annotation must be applied at field or method level.

@CreateIfNull( value = true )

private List<User> users;

31.

What Is The Purpose Of @conversion Annotation Annotation?

Answer»

This is a marker ANNOTATION for TYPE conversions at Type level. The Conversion annotation must be applied at Type level.

@Conversion()

PUBLIC class ConversionAction implements Action {

}

This is a marker annotation for type conversions at Type level. The Conversion annotation must be applied at Type level.

@Conversion()

public class ConversionAction implements Action {

}

32.

What Is The Purpose Of @customvalidator Annotation?

Answer»

This ANNOTATION can be used for custom validators. Use the ValidationParameter annotation to SUPPLY ADDITIONAL PARAMS.

@CustomValidator(type ="customValidatorName", fieldName = "myField")

This annotation can be used for custom validators. Use the ValidationParameter annotation to supply additional params.

@CustomValidator(type ="customValidatorName", fieldName = "myField")

33.

What Is The Purpose Of @validations Annotation?

Answer»

If you want to use several ANNOTATIONS of the same TYPE, these annotation must be NESTED within the @Validations() annotation.

public class Employee EXTENDS ActionSupport{

@Validations(

requiredFields =

{@RequiredFieldValidator(type = ValidatorType.SIMPLE, 

fieldName = "customfield", 

message = "You must enter a value for field.")},

requiredStrings =

{@RequiredStringValidator(type = ValidatorType.SIMPLE, 

fieldName = "stringisrequired", 

message = "You must enter a value for string.")}

)

public String getName() {

return name;

}

}

If you want to use several annotations of the same type, these annotation must be nested within the @Validations() annotation.

public class Employee extends ActionSupport{

@Validations(

requiredFields =

{@RequiredFieldValidator(type = ValidatorType.SIMPLE, 

fieldName = "customfield", 

message = "You must enter a value for field.")},

requiredStrings =

{@RequiredStringValidator(type = ValidatorType.SIMPLE, 

fieldName = "stringisrequired", 

message = "You must enter a value for string.")}

)

public String getName() {

return name;

}

}

34.

What Is The Purpose Of @urlvalidator Annotation?

Answer»

This validator checks that a FIELD is a VALID URL.

public class Employee EXTENDS ActionSupport{

@UrlValidator(message = "Default message", 

key = "i18n.key", SHORTCIRCUIT = true)

public String getURL() {

RETURN url;

}

}

This validator checks that a field is a valid URL.

public class Employee extends ActionSupport{

@UrlValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true)

public String getURL() {

return url;

}

}

35.

What Is The Purpose Of @stringlengthfieldvalidator Annotation?

Answer»

This validator checks that a STRING field is of the right length. It ASSUMES that the field is a String. If neither minLength nor maxLength is set, nothing will be done.

public class EMPLOYEE extends ActionSupport{

@StringLengthFieldValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true, 

trim = true, minLength = "5", maxLength = "12")

public String getName() {

return NAME;

}

}

This validator checks that a String field is of the right length. It assumes that the field is a String. If neither minLength nor maxLength is set, nothing will be done.

public class Employee extends ActionSupport{

@StringLengthFieldValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true, 

trim = true, minLength = "5", maxLength = "12")

public String getName() {

return name;

}

}

36.

What Is The Purpose Of @requiredstringvalidator Annotation?

Answer»

This validation ANNOTATION checks that a String field is not empty (i.e. non-null with a length > 0).

public class Employee EXTENDS ActionSupport{

@RequiredStringValidator(message = "Default message", 

key = "i18n.key", SHORTCIRCUIT = TRUE, trim = true)

public String getName() {

RETURN name;

}

}

This validation annotation checks that a String field is not empty (i.e. non-null with a length > 0).

public class Employee extends ActionSupport{

@RequiredStringValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true, trim = true)

public String getName() {

return name;

}

}

37.

What Is The Purpose Of @intrangefieldvalidator Annotation?

Answer»

This validation annotation checks that a numeric field has a value within a specified range. If neither MIN nor max is set, nothing will be DONE.

PUBLIC class Employee extends ActionSupport{

@IntRangeFieldValidator(message = "Default message", 

KEY = "i18n.key", shortCircuit = true, 

min = "0", max = "42")

public String GETAGE() {

return age;

}

}

This validation annotation checks that a numeric field has a value within a specified range. If neither min nor max is set, nothing will be done.

public class Employee extends ActionSupport{

@IntRangeFieldValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true, 

min = "0", max = "42")

public String getAge() {

return age;

}

}

38.

What Is The Purpose Of @expressionvalidator Annotation?

Answer»

This non-field level VALIDATOR validates a SUPPLIED regular expression.

@ExpressionValidator(message = "DEFAULT message", key = "i18n.key", shortCircuit = TRUE, expression = "an OGNL expression" )

This non-field level validator validates a supplied regular expression.

@ExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression" )

39.

What Is The Purpose Of @emailvalidator Annotation?

Answer»

This validation annotation checks that a field is a valid e-mail address if it contains a non-empty String.

public class Employee extends ActionSupport{

@EmailValidator(MESSAGE = "DEFAULT message", 

KEY = "i18n.key", shortCircuit = TRUE)

public String getEmail() {

return email;

}

}

This validation annotation checks that a field is a valid e-mail address if it contains a non-empty String.

public class Employee extends ActionSupport{

@EmailValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true)

public String getEmail() {

return email;

}

}

40.

What Is The Purpose Of @doublerangefieldvalidator Annotation?

Answer»

his validation annotation CHECKS that a double field has a value within a specified range. If NEITHER min nor max is set, nothing will be done.

public class Employee extends ACTIONSUPPORT{

@DoubleRangeFieldValidator(MESSAGE = "Default message", 

key = "i18n.key", shortCircuit = true, 

minInclusive = "0.123", maxInclusive = "99.987")

public STRING getIncome() {

return income;

}

}

his validation annotation checks that a double field has a value within a specified range. If neither min nor max is set, nothing will be done.

public class Employee extends ActionSupport{

@DoubleRangeFieldValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true, 

minInclusive = "0.123", maxInclusive = "99.987")

public String getIncome() {

return income;

}

}

41.

What Is The Purpose Of @conversionerrorfieldvalidator Annotation?

Answer»

This VALIDATION ANNOTATION checks if there are any CONVERSION errors for a field and applies them if they EXIST.

public class EMPLOYEE extends ActionSupport{

@ConversionErrorFieldValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true)

public String getName() {

return name;

}

}

This validation annotation checks if there are any conversion errors for a field and applies them if they exist.

public class Employee extends ActionSupport{

@ConversionErrorFieldValidator(message = "Default message", 

key = "i18n.key", shortCircuit = true)

public String getName() {

return name;

}

}

42.

What Is The Purpose Of @beforeresult Annotation?

Answer»

The @BeforeResult annotation MARKS a ACTION method that needs to be executed before the result. Return value is ignored.

PUBLIC class EMPLOYEE extends ActionSupport{

@BeforeResult

public void isValid() throws ValidationException {

// validate model object, throw EXCEPTION if failed

}

public String execute() {

// perform action

return SUCCESS;

}

}

The @BeforeResult annotation marks a action method that needs to be executed before the result. Return value is ignored.

public class Employee extends ActionSupport{

@BeforeResult

public void isValid() throws ValidationException {

// validate model object, throw exception if failed

}

public String execute() {

// perform action

return SUCCESS;

}

}

43.

What Is The Purpose Of @before Annotation?

Answer»

The @Before ANNOTATION marks a action METHOD that needs to be called before the main action method and the result was executed. Return value is ignored.

public class Employee extends ActionSupport{

@Before

public void isAuthorized() throws AuthenticationException {

// authorize request, throw exception if FAILED

}

public STRING execute() {

// perform secure action

return SUCCESS;

}

}

The @Before annotation marks a action method that needs to be called before the main action method and the result was executed. Return value is ignored.

public class Employee extends ActionSupport{

@Before

public void isAuthorized() throws AuthenticationException {

// authorize request, throw exception if failed

}

public String execute() {

// perform secure action

return SUCCESS;

}

}

44.

What Is The Purpose Of @after Annotation?

Answer»

The @After annotation marks a action method that needs to be called after the main action method and the result was executed. Return value is ignored.

public class EMPLOYEE extends ActionSupport{

@After

public void isValid() throws ValidationException {

// validate MODEL object, THROW exception if failed

}

public String execute() {

// PERFORM secure action

return SUCCESS;

}

}

The @After annotation marks a action method that needs to be called after the main action method and the result was executed. Return value is ignored.

public class Employee extends ActionSupport{

@After

public void isValid() throws ValidationException {

// validate model object, throw exception if failed

}

public String execute() {

// perform secure action

return SUCCESS;

}

}

45.

What Is The Purpose Of @action Annotation?

Answer»

This is used to decorate the execute() method. The Action method also TAKES in a VALUE which is the URL on which the action is invoked.

public class Employee extends ACTIONSUPPORT{

private String name;

private int AGE;

@Action(value="/empinfo")

public String execute() 

{

return SUCCESS;

}

}

This is used to decorate the execute() method. The Action method also takes in a value which is the URL on which the action is invoked.

public class Employee extends ActionSupport{

private String name;

private int age;

@Action(value="/empinfo")

public String execute() 

{

return SUCCESS;

}

}

46.

What Is The Purpose Of @result Annotation?

Answer»

The @result ANNOTATIONS have the name that correspond to the outcome of the execute method. They also contain a location as to which view should be SERVED CORRESPONDING to return VALUE from execute().

@Result(name="success", value="/success.jsp")

public CLASS Employee extends ActionSupport{

 ...

}

The @result annotations have the name that correspond to the outcome of the execute method. They also contain a location as to which view should be served corresponding to return value from execute().

@Result(name="success", value="/success.jsp")

public class Employee extends ActionSupport{

 ...

}

47.

What Is The Purpose Of @results Annotation?

Answer»

A @Results annotation is a collection of results. Under the @Results annotation, we can have multiple @Result annotations.

@Results({

@Result(name="SUCCESS", value="/success.jsp"),

@Result(name="ERROR", value="/error.jsp")

})

PUBLIC class Employee extends ACTIONSUPPORT{

 ...

}

A @Results annotation is a collection of results. Under the @Results annotation, we can have multiple @Result annotations.

@Results({

@Result(name="success", value="/success.jsp"),

@Result(name="error", value="/error.jsp")

})

public class Employee extends ActionSupport{

 ...

}

48.

How To Handle Exceptions In Structs?

Answer»

STRUTS MAKES the EXCEPTION handling easy by the use of the "exception" INTERCEPTOR. The "exception" interceptor is included as part of the DEFAULT stack, so you don't have to do anything extra to configure it. It is available out-of-the-box ready for you to use.

Struts makes the exception handling easy by the use of the "exception" interceptor. The "exception" interceptor is included as part of the default stack, so you don't have to do anything extra to configure it. It is available out-of-the-box ready for you to use.

49.

What Inbuilt Themes Are Provided By Struts2?

Answer»

Struts 2 comes with three built-in themes −

simple theme − A minimal theme with no "bells and whistles". For example, the TEXTFIELD TAG renders the HTML <input/> tag without a label, validation, error reporting, or any other formatting or functionality.

xhtml theme − This is the default theme used by Struts 2 and provides all the basics that the simple theme provides and adds several FEATURES like standard two-column table layout for the HTML, LABELS for each of the HTML, Validation and error reporting etc.

css_xhtml theme − This theme provides all the basics that the simple theme provides and adds several features like standard two-column CSS-based layout, using <div> for the HTML Struts Tags, Labels for each of the HTML Struts Tags, placed according to the CSS stylesheet.

Struts 2 comes with three built-in themes −

simple theme − A minimal theme with no "bells and whistles". For example, the textfield tag renders the HTML <input/> tag without a label, validation, error reporting, or any other formatting or functionality.

xhtml theme − This is the default theme used by Struts 2 and provides all the basics that the simple theme provides and adds several features like standard two-column table layout for the HTML, Labels for each of the HTML, Validation and error reporting etc.

css_xhtml theme − This theme provides all the basics that the simple theme provides and adds several features like standard two-column CSS-based layout, using <div> for the HTML Struts Tags, Labels for each of the HTML Struts Tags, placed according to the CSS stylesheet.

50.

Which Class Of Struts Is Responsible To Converts Data Types From String And Vice Versa?

Answer»

StrutsTypeConverter CLASS TELLS Struts how to convert Environment to a STRING and vice versa by OVERRIDING TWO methods convertFromString() and convertToString().

StrutsTypeConverter class tells Struts how to convert Environment to a String and vice versa by overriding two methods convertFromString() and convertToString().

Previous Next