http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com http://vietjack.com/ Trang chia sẻ các bài học online miễn phí Page 1 prepare Interceptor trong Struts 2 prepare Intercep
Trang 1http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com
http://vietjack.com/ Trang chia sẻ các bài học online miễn phí Page 1
prepare Interceptor trong Struts 2
prepare Interceptor gọi phương thức prepare() trên action nếu nó triển khai Preparable Interface
Nó gọi phương thức prepare() trước khi gọi phương thức execute()
Để sử dụng prepare Interceptor, bạn cần triển khai Preparable Interface trong lớp action của bạn
và ghi đè phương thức prepare của nó Theo mặc định, nó được tìm thấy trong default stack, vì thế bạn không cần xác định nó một cách tường minh
prepare Interceptor chỉ định nghĩa một tham số, đó là:
alwaysInvokePrepare: theo mặc định, nó được thiết lập là true
<action name = "login" class = "com.vietjack.LoginAction" >
<interceptor-ref name = "params" />
<interceptor-ref name = "prepare" />
<result name = "success" >login-success.jsp</result>
</action>
Lớp Action
Lớp Action phải triển khai Prepare Interface và ghi đè phương thức prepare()
package com vietjack ;
import com opensymphony xwork2 Preparable ;
public class LoginAction implements Preparable {
private String name , password ;
public String getName ()
return name ;
}
public void setName ( String name ) {
this name = name ;
}
public String getPassword ()
Trang 2http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com
http://vietjack.com/ Trang chia sẻ các bài học online miễn phí Page 2
return password ;
}
public void setPassword ( String password ) {
this password = password ;
}
public void prepare () throws Exception
System out println ( "Trinh prepare logi" );
}
public String execute (){
System out println ( "Trinh logic thuc su" );
return "success" ;
}
}