Struts2でServletContextを取得する方法

1. 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

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;
}
}

Comments are closed.