Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

prepare interceptor


The prepare interceptor calls prepre() method on the action if it implements Preparable interface. It calls prepare() method before the execute() method.

To use the prepare interceptor, you need to implement Preparable interface in your action class and override its method prepare.

It is found in the default stack bydefault. So you don’t need to specify it explicitely.

Parameters of prepare interceptor

There is only 1 parameter defined for prepare interceptor.

Parameter Description
alwaysInvokePrepare It is set to true bydefault.

Example of prepare interceptor

  1. <action name=“login” class=“com.Kreationnext.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.Kreationnext;
  2. import com.opensymphony.xwork2.Preparable;
  3. public class LoginAction implements Preparable{
  4. private String name,password;
  5. public String getName() {
  6.     return name;
  7. }
  8. public void setName(String name) {
  9.     this.name = name;
  10. }
  11. public String getPassword() {
  12.     return password;
  13. }
  14. public void setPassword(String password) {
  15.     this.password = password;
  16. }
  17. public void prepare() throws Exception {
  18.     System.out.println(“preparation logic”);
  19. }
  20. public String execute(){
  21.     System.out.println(“actual logic”);
  22.     return “success”;
  23. }
  24. }

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

 

Output

prepareoutput1

Now remove the entry of prepare interceptor from the struts.xml file, preparation logic will not be printed.

Comments

comments