当前位置: 代码迷 >> Ruby/Rails >> Struts2在重定向后ActionErrors和ActionMessage丢失有关问题
  详细解决方案

Struts2在重定向后ActionErrors和ActionMessage丢失有关问题

热度:254   发布时间:2016-04-29 02:14:16.0
Struts2在重定向后ActionErrors和ActionMessage丢失问题
Step1:把 B Action的信息储存到Session中去
        
     /**	 * Store action messages and errors.	 */	protected void storeActionMessagesAndErrors() {		Collection<String> actionMessages = this.getActionMessages();		if (CollectionUtils.isNotEmpty(actionMessages)) {			this.setToSession(Constants.SESSION_ACTION_MESSAGES,					actionMessages);		}		Collection<String> actionErrors = this.getActionErrors();		if (CollectionUtils.isNotEmpty(actionErrors)) {			this.setToSession(Constants.SESSION_ACTION_ERRORS,					this.getActionErrors());		}	}        /**	 * Set an object in the session.	 * 	 * @param key	 *            the object key	 * @param object	 *            the object to set.	 */	protected void setToSession(String key, Object object) {		ServletActionContext.getRequest().getSession()				.setAttribute(key, object);	}


Step2:在从B Action重定向A Action的时候,在从Session中取出来

         /**	 * Retrieve action messages.	 */	@SuppressWarnings("unchecked")	protected void retrieveActionMessagesAndErrors() {		Collection<String> actionErrorsRetrieved = (Collection<String>) this				.getFromSession(Constants.SESSION_ACTION_ERRORS);		Collection<String> actionMessagesRetrieved = (Collection<String>) this				.getFromSession(Constants.SESSION_ACTION_MESSAGES);		if (actionErrorsRetrieved != null) {			Collection<String> actionErrors = this.getActionErrors();			actionErrors.addAll(actionErrorsRetrieved);			this.setActionErrors(actionErrors);			this.removeSession(Constants.SESSION_ACTION_ERRORS);		}		if (actionMessagesRetrieved != null) {			Collection<String> actionMessages = this.getActionMessages();			actionMessages.addAll(actionMessagesRetrieved);			this.setActionMessages(actionMessages);			this.removeSession(Constants.SESSION_ACTION_MESSAGES);		}	}	/**	 * 	 * Get an object from Session.	 * 	 * @param key	 *            the object key.	 * @return the object.	 */	protected Object getFromSession(String key) {		return ServletActionContext.getRequest().getSession().getAttribute(key);	}	/**	 * Removes the session.	 * 	 * @param key	 *            the key	 */	protected void removeSession(String key) {		ServletActionContext.getRequest().getSession().removeAttribute(key);	}
  相关解决方案