Struts2_在Action中获取Servlet原生对象
ServletActionContext
- getRequest
- getActionContext(HttpServletRequest)
- getServletContext
- getResponse
ServletRequestAware
ServletResponseAware
ServletContextAware

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Product3 implements ServletRequestAware, ServletResponseAware, ServletContextAware {
private Integer productId;
private String productName;
private String productDesc;
private Double productDouble;
public Product3(){}
public Product3(Integer productId, String productName, String productDesc, Double productDouble) {
this.productId = productId;
this.productName = productName;
this.productDesc = productDesc;
this.productDouble = productDouble;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public Double getProductDouble() {
return productDouble;
}
public void setProductDouble(Double productDouble) {
this.productDouble = productDouble;
}
@Override
public String toString() {
return "Product{" +
"productId=" + productId +
", productName='" + productName + '\'' +
", productDesc='" + productDesc + '\'' +
", productDouble=" + productDouble +
'}';
}
public String save(){
HttpServletRequest request = ServletActionContext.getRequest();
ActionContext actionContext = ServletActionContext.getActionContext(request);
ServletContext servletContext = ServletActionContext.getServletContext();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
return "success";
}
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
}
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
}
@Override
public void setServletContext(ServletContext servletContext) {
}
}
