THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package tutorial; import java.util.Calendar; import com.opensymphony.xwork.interceptor.Interceptor; import com.opensymphony.xwork.ActionInvocation; public class GreetingInterceptor implements Interceptor { public void init() { } public void destroy() { } public String intercept(ActionInvocation invocation) throws Exception { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); String greeting = (hour < 6) ? "Good evening" : ((hour < 12) ? "Good morning": ((hour < 18) ? "Good afternoon": "Good evening")); invocation.getInvocationContext().getSession().put("greeting", greeting); String result = invocation.invoke(); return result; } } |
Coding the configuration
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd"> <xwork> <include file="action-default.xml" /> <package name="default" extends="action-default"> <interceptors> <interceptor name="greeting" class="tutorial.GreetingInterceptor" /> </interceptors> <action name="greeting" class="tutorial.GreetingAction"> <interceptor-ref name="greeting" /> <result name="success" type="velocity">greeting.vm</result> </action> <!-- other action mappings --> </package> </xwork> |
Coding the Action class
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package tutorial;
import com.opensymphony.xwork.ActionSupport;
public class GreetingAction extends ActionSupport {
public String execute() throws Exception
{
return SUCCESS;
}
}
|
Coding the result page
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<html> <head> <title>Understanding Interceptors</title> </head> <body> #set ($ses = $req.getSession()) <p><b>${ses.getAttribute('greeting')}!</b></p> </body> </html> |
How the code works
...