import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.xxx.travel.common.utils.FormatUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 表现业务的基类
* @author IXR
*
*/
@Results({
@Result(name=BaseAction.SUCCESS,location="${actionMapping.name!=null?actionMapping.name:\"index\"}/${actionMapping.method!=null?actionMapping.method:\"index\"}.jsp"),
@Result(name=BaseAction.TEXT,type="plainText"),
@Result(name=BaseAction.OK,location="/index/tsuccess?reload=${reload}",type="redirectAction"),
@Result(name=BaseAction.NO,location="/index/tfailure?error=${errorMessage}&reload=${reload}",type="redirectAction")
})
public class BaseAction extends ActionSupport implements Action,ServletRequestAware, SessionAware, ServletResponseAware{
private static final long serialVersionUID = 2461271909384319253L;
private String reload ;
protected Error error;
public String getErrorMessage() throws UnsupportedEncodingException{
if(error != null){
return URLEncoder.encode(error.getMessage(),request.getCharacterEncoding());
}else{
return null;
}
}
protected static final String TEXT = "text";
protected static final String OK = "OK";
protected static final String NO = "NO";
protected HttpServletRequest request;
protected HttpServletResponse response;
protected Map<String, Object> session;
protected ActionMapping actionMapping;
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
Object actionMapping = request.getAttribute("mapping");
if(actionMapping != null){
this.actionMapping = (ActionMapping)actionMapping;
}
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public ActionMapping getActionMapping() {
return actionMapping;
}
public void setActionMapping(ActionMapping actionMapping) {
this.actionMapping = actionMapping;
}
public String getFMT_DATE(){return FormatUtils.FMT_DATE;}
public void output(String text) throws Exception{
response.setCharacterEncoding(request.getCharacterEncoding());
PrintWriter out = response.getWriter();
out.print(text);
}
public void output(String text,String encoding) throws Exception{
response.setCharacterEncoding(encoding);
PrintWriter out = response.getWriter();
out.print(text);
}
public String getReload() {
return reload;
}
public void setReload(String reload) {
this.reload = reload;
}
}
一个强大的父类:BaseAction
最新推荐文章于 2021-02-19 14:16:34 发布