Struts 2 prepare interceptor example

The prepare interceptor calls prepre() method on the activity in the event that it actualizes Preparable interface. It calls get ready() technique before the execute() method.

To utilize the get ready interceptor, you have to actualize Preparable interface in your activity class and abrogate its strategy prepare.

It is found in the default stack bydefault. So you don't have to determine it explicitely.

Parameters of get ready interceptor

There is just 1 parameter characterized for get ready interceptor.

Parameters of prepare interceptor

There is only 1 parameter defined for prepare interceptor.

alwaysInvokePrepare It is set to true bydefault.

Example of prepare interceptor

  1. <action name="login" class="com.Javacodegeeks.LoginAction">  
  2.     <interceptor-ref name="params"/>  
  3.     <interceptor-ref name="prepare"/>  
  4.     <result name="success">login-success.jsp</result>  
  5. </action>  

Action class

The action class must implement the Preparable interface and override its method prepare().

  1. package com.Javacodegeeks;  
  2. import com.opensymphony.xwork2.Preparable;  
  3. public class LoginAction implements Preparable{  
  4. private String name,password;  
  6. public String getName() {  
  7.     return name;  
  8. }  
  9. public void setName(String name) {  
  10. = name;  
  11. }  
  12. public String getPassword() {  
  13.     return password;  
  14. }  
  15. public void setPassword(String password) {  
  16.     this.password = password;  
  17. }  
  18. public void prepare() throws Exception {  
  19.     System.out.println("preparation logic");  
  20. }  
  22. public String execute(){  
  23.     System.out.println("actual logic");  
  24.     return "success";  
  25. }  
  26. }  

You can see output on the console, preparation logic is printed before the actual logic.

struts 2 prepare interceptor example output

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition