📄 baseaction.java
字号:
package cn.myapps.base.action;
import java.util.Iterator;
import java.util.Map;
import cn.myapps.base.dao.DataPackage;
import cn.myapps.base.dao.ValueObject;
import cn.myapps.base.ejb.BaseProcess;
import cn.myapps.constans.Environment;
import cn.myapps.constans.Web;
import cn.myapps.core.user.action.WebUser;
import cn.myapps.core.user.ejb.UserVO;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
/**
* BaseAction class.
*
* @author huangnaizhou
* @since JDK1.4
*/
public abstract class BaseAction extends ActionSupport implements Action {
/** valueobject */
private ValueObject content = null;
/** DataPackage */
private DataPackage datas = null;
/** BaseProcess */
protected BaseProcess proxy = null;
protected String[] _selects = null;
protected String application;
private static final ThreadLocal _params = new ThreadLocal();
/**
* BaseAction constructor
*
* @param proxy
* BaseProcess
* @param content
* ValueObject
*/
public BaseAction(BaseProcess proxy, ValueObject content) {
this.proxy = proxy;
this.content = content;
}
/**
* Get WebUser Object.
*
* @return WebUser Object user
* @throws Exception
*/
public WebUser getUser() throws Exception {
Map session = getContext().getSession();
WebUser user = null;
if (session == null || session.get(Web.SESSION_ATTRIBUTE_USER) == null) {
UserVO vo = new UserVO();
vo.getId();
vo.setName("GUEST");
vo.setLoginno("guest");
vo.setLoginpwd("");
vo.setRoles(null);
vo.setEmail("");
vo.setLanguageType(1);
user = new WebUser(vo);
} else {
user = (WebUser) session.get(Web.SESSION_ATTRIBUTE_USER);
}
return user;
}
public String doBack() throws Exception {
return null;
}
/**
* doNew method is executed.
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doNew() throws Exception {
return SUCCESS;
}
/**
* edit Value Object
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doEdit() throws Exception {
Map params = getContext().getParameters();
Object obj = params.get("id");
String id = ((String[]) obj)[0];
ValueObject contentVO = proxy.doView(id);
setContent(contentVO);
return SUCCESS;
}
/**
* save Value Object
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doSave() throws Exception {
if (content.getId() == null || content.getId().equals("")) {
proxy.doCreate(content);
} else {
proxy.doUpdate(content);
}
return SUCCESS;
}
/**
* show a Value Object View .
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doView() throws Exception {
Map params = getContext().getParameters();
String[] ids = (String[]) (params.get("id"));
String id = null;
if (ids != null && ids.length > 0) {
id = ids[0];
}
ValueObject contentVO = proxy.doView(id);
setContent(contentVO);
return SUCCESS;
}
/**
* delete value object by _selects
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doDelete() throws Exception {
if (_selects != null)
proxy.doRemove(_selects);
/*
* for (int i = 0; i < _selects.length; i++) { String id = _selects[i];
* try{ proxy.doRemove(id); }catch (Exception e){l e.printStackTrace(); } }
*/
return SUCCESS;
}
/**
* a list of datas
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doList() throws Exception {
ParamsTable params = getParams();
WebUser user = getUser();
datas = this.proxy.doQuery(params, user);
return SUCCESS;
}
/**
* doDefault method is executed.
*
* @return If the action execution was successful.return "SUCCESS".Show an
* success view .
* <p>
* If the action execution was a failure. return "ERROR".Show an
* error view, possibly asking the user to retry entering data.
* <p>
* The "INPUT" is also used if the given input params are invalid,
* meaning the user should try providing input again.
*
* @throws Exception
*/
public String doDefault() throws Exception {
return INPUT;
}
/**
* Get the ActionContext
*
* @return ActionContext
*/
public static ActionContext getContext() {
ActionContext context = ActionContext.getContext();
return context;
}
public Environment getEnvironment() {
String ctxPath = ServletActionContext.getRequest().getContextPath();
Environment evt = (Environment) ActionContext.getContext()
.getApplication().get(Environment.class.getName());
evt.setContextPath(ctxPath);
return evt;
}
public String execute() throws Exception {
// TODO Auto-generated method stub
return null;
}
/**
* Get the VallueObject.
*
* @return Returns the content.
*/
public ValueObject getContent() {
return content;
}
/**
* Set the ValueObject
*
* @param content
* The content to set.
*/
public void setContent(ValueObject content) {
this.content = content;
}
/**
* Get the ParamsTable
*
* @return ParamsTable
*/
public ParamsTable getParams() {
ParamsTable pm = null;
if (_params.get() != null) {
pm = (ParamsTable) _params.get();
} else {
synchronized (this) {
pm = new ParamsTable();
Map m = getContext().getParameters();
Iterator iter = m.keySet().iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
Object obj = m.get(name);
if (obj instanceof String[] && ((String[]) obj).length > 0) {
String tmp[] = (String[]) obj;
pm.setParameter(name, tmp[0]);
} else {
pm.setParameter(name, obj + "");
}
}
// set the application param to ParamsTable
if (getApplication() != null) {
pm.setParameter("application", getApplication());
}
if (pm.getParameter("_pagelines") == null) {
pm.setParameter("_pagelines", Web.DEFAULT_LINES_PER_PAGE);
}
}
}
return pm;
}
/**
* Get the DataPackage
*
* @return the DataPackage
*/
public DataPackage getDatas() {
return datas;
}
public void setDatas(DataPackage datas) {
this.datas = datas;
}
/**
* Get the selects.
*
* @return the selects.
*/
public String[] get_selects() {
return _selects;
}
/**
* Set the selects.
*
* @param selects
*
*/
public void set_selects(String[] selects) {
this._selects = selects;
}
public String getApplication() {
if (application != null && application.trim().length() > 0) {
return application;
} else {
return (String) getContext().getSession().get("APPLICATION");
}
}
public void setApplication(String application) throws Exception {
this.application = application;
getContent().setApplicationid(application);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -