How to get the ServletContext in Struts 2
1) ServletActionContext - Get the ServletContext object directly from org.apache.struts2.ServletActionContext.
2) ServletContextAware - implements the org.apache.struts2.util.ServletContextAware interface.
When Struts 2 ‘servlet-config’ interceptor is seeing that an Action class is implemented the ServletContextAware interface, it will pass a ServletContext reference to the requested Action class via the setServletContext() method.
1) ServletActionContext - Get the ServletContext object directly from org.apache.struts2.ServletActionContext.
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport{
public String execute() throws Exception {
ServletContext context = ServletActionContext.getServletContext();
return SUCCESS;
}
}
2) ServletContextAware - implements the org.apache.struts2.util.ServletContextAware interface.
When Struts 2 ‘servlet-config’ interceptor is seeing that an Action class is implemented the ServletContextAware interface, it will pass a ServletContext reference to the requested Action class via the setServletContext() method.
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction
extends ActionSupport implements ServletContextAware{
ServletContext context;
public String execute() throws Exception {
return SUCCESS;
}
public void setServletContext(ServletContext context) {
this.context = context;
}
}
No comments:
Post a Comment