struts 2ベースのwebシステムでのリターン機能
8265 ワード
格納されたBackForm
form操作のツールクラス
velocityのtoolboxを構成します.xmlのtoolクラス
Struts 2のブロッキング
ページでの使用時
リストページのアクション
リストvmページで、操作ページにジャンプするActionUrlは:operate.action?id=XXX&formId=$!formId//ここでformIdはcreateFormで設定したformIdを呼び出します
入力操作Action対応操作ページには、${form.backUrl}//formが構成されたtoolboxのtoolのkeyとなり、実際に呼び出されたときFormToolが戻る.getBackUrl()メソッド
public class BackForm implements Serializable {
/** formId*/
private String formId;
/** form formId*/
private String backId;
private Map<String, Object> formParams;
/** Url*/
private String formAction;
/** form action*/
private String backAction;
}
form操作のツールクラス
public class FormUtil {
/** Id*/
public final static String BACK_FORM_ID = "backId";
/** Id*/
public final static String FORM_ID = "formId";
/** */
public final static String BACK_CONTEXT = "backFormContext";
/** */
private static MemcachedOperate memcachedOperate = new MemcachedOperateImpl();
/** key*/
private final static String FORM_KEY = "back_form_";
/** 2 */
private final static int FORM_EXP_TIME = 2 * 24 * 60;
/**
* baseAction Form
*
* @param baseAction
* @return
*/
public static BackForm createForm(BaseAction baseAction) {
BackForm backForm = null;
// , form
if (StringUtils.isNotBlank(baseAction.getBackId())) {
backForm = getBackForm(baseAction.getBackId());
if (backForm != null) {
Map<String, Object> formParams = ActionContext.getContext().getParameters();
// ,
if (formParams != null) {
for (String key : formParams.keySet()) {
if (!FORM_ID.equals(key) && !BACK_FORM_ID.equals(key)) {
backForm.getFormParams().put(key, formParams.get(key));
}
}
//
setBackForm(backForm);
}
ServletActionContext.getContext().getContextMap().put(BACK_CONTEXT, backForm);
return backForm;
}
}
backForm = createForm();
baseAction.setFormId(backForm.getFormId());
return backForm;
}
/**
* struts2 form
*
* @return
*/
public static BackForm createForm() {
HttpServletRequest request = ServletActionContext.getRequest();
String action = request.getServletPath();
BackForm backForm = new BackForm();
backForm.setFormAction(action);
Map<String, Object> formParams = ActionContext.getContext().getParameters();
if (formParams == null) {
formParams = new TreeMap<String, Object>();
}
backForm.setFormParams(formParams);
// Form formId form
Object back = formParams.get(FORM_ID);
String backId = null;
if (back != null && back.getClass().isArray() && ((Object[]) back).length > 0) {
backId = String.valueOf(((Object[]) back)[0]);
} else if (back != null && !back.getClass().isArray()) {
backId = String.valueOf(back);
}
if (StringUtils.isNotBlank(backId)) {
BackForm form = getBackForm(backId);
if (form != null) {
backForm.setBackId(form.getFormId());
backForm.setBackAction(form.getFormAction());
}
}
backForm.setFormId(UUID.randomUUID().toString().replace("-", ""));
//
ServletActionContext.getContext().getContextMap().put(BACK_CONTEXT, backForm);
setBackForm(backForm);
return backForm;
}
/**
* form
*
* @param formId
* @return
*/
public static BackForm getBackForm(String formId) {
return (BackForm) memcachedOperate.gets(FORM_KEY + formId);
}
/**
*
*
* @param backForm
*/
public static void setBackForm(BackForm backForm) {
memcachedOperate.setWithNoReply(FORM_KEY + backForm.getFormId(), FORM_EXP_TIME, backForm);
}
/**
* struts2 Form
*
* @return
*/
public static BackForm getBackForm() {
return (BackForm) ServletActionContext.getContext().getContextMap().get(BACK_CONTEXT);
}
velocityのtoolboxを構成します.xmlのtoolクラス
/**
* velocity,
* , request,
*
* @author wy
*/
public class FormTool {
/** action */
private BackForm backForm;
private final Map<String, BackForm> backFormMap = new HashMap<String, BackForm>();
public FormTool() {
}
/**
* Id
*
* @return
*/
public String getFormId() {
BackForm backForm = this.getBackForm();
if (backForm != null) {
return backForm.getFormId();
}
return null;
}
/**
* form
*
* @return
*/
public BackForm getBackForm() {
if (backForm == null) {
backForm = FormUtil.getBackForm();
}
return backForm;
}
/**
* form
*
* @param formId
* @return
*/
public BackForm getBackForm(String formId) {
BackForm form = backFormMap.get(formId);
if (form != null) {
return form;
}
form = FormUtil.getBackForm(formId);
backFormMap.put(formId, form);
return form;
}
/**
* url
*
* @return
*/
public String getBackUrl() {
BackForm backForm = this.getBackForm();
return getBackUrl(backForm);
}
/**
* Id url
*
* @param formId
* @return
*/
public String getBackUrl(String formId) {
BackForm backForm = this.getBackForm(formId);
return getBackUrl(backForm);
}
/**
* url
*
* @param backForm
* @return
*/
public String getBackUrl(BackForm backForm) {
if (backForm == null || StringUtils.isBlank(backForm.getBackId())
|| StringUtils.isBlank(backForm.getBackAction())) {
return null;
}
return backForm.getBackAction() + "?backId=" + backForm.getBackId();
}
}
Struts 2のブロッキング
public class FormParametersInterceptor extends ParametersInterceptor {
/**
* com.opensymphony.xwork2.interceptor.ParametersInterceptor , , formParam , null
*
* @param ac
* @return
*/
protected Map<String, Object> retrieveParameters(ActionContext ac) {
Map<String, Object> map = ac.getParameters();
if (map == null) {
return map;
}
// ID, ,
Object back = map.get(FormUtil.BACK_FORM_ID);
if (back == null) {
return null;
}
String backId = null;
if (back.getClass().isArray() && ((Object[]) back).length > 0) {
backId = String.valueOf(((Object[]) back)[0]);
} else if (!back.getClass().isArray()) {
backId = String.valueOf(back);
}
if (backId == null) {
return null;
}
BackForm backForm = FormUtil.getBackForm(backId);
if (backForm != null) {
//
Map<String, Object> formParams = backForm.getFormParams();
Iterator<String> it = formParams.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
if (!FormUtil.FORM_ID.equals(key) && !FormUtil.BACK_FORM_ID.equals(key)
&& map.containsKey(key)) {
it.remove();
}
}
return formParams;
}
return null;
}
}
ページでの使用時
リストページのアクション
/** Id*/
protected String formId;
/** */
protected String backId;
// Action
public String xxx() {
FormUtil.createForm(this);
return SUCCESS;
}
リストvmページで、操作ページにジャンプするActionUrlは:operate.action?id=XXX&formId=$!formId//ここでformIdはcreateFormで設定したformIdを呼び出します
入力操作Action対応操作ページには、${form.backUrl}//formが構成されたtoolboxのtoolのkeyとなり、実際に呼び出されたときFormToolが戻る.getBackUrl()メソッド