📄 processaction.java
字号:
/*
* $Header: /home/cvs/jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/ProcessAction.java,v 1.13 2004/03/14 14:32:19 husted Exp $
* $Revision: 1.13 $
* $Date: 2004/03/14 14:32:19 $
*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.struts.scaffold;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.scaffold.lang.Log;
import org.apache.commons.scaffold.lang.Tokens;
import org.apache.commons.scaffold.util.ProcessBean;
import org.apache.commons.scaffold.util.ProcessResult;
import org.apache.struts.action.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* Advanced framework class to instantiate and execute helper beans
* and process the result.
*
* @version $Revision: 1.13 $ $Date: 2004/03/14 14:32:19 $
* @deprecated Use BizAction instead
*/
public class ProcessAction extends BaseHelperAction {
// --------------------------------------------------------- Public Methods
/**
* Exposes result in a servlet context.
*
* @param request the request being serviced
* @param name The name to use in scope
* @param scope The scope to set the attribute in
* @param bean The attribute to be set
*/
protected void exposeInScope(
HttpServletRequest request,
HttpServletResponse response,
String name,
String scope,
Object bean) {
if (null==scope) {
servlet.log(Log.PROCESS_BEAN_NULL_SCOPE,Log.DEBUG);
}
else if (Tokens.REQUEST.equals(scope)) {
request.setAttribute(name,bean);
}
else if (Tokens.SESSION.equals(scope)) {
request.getSession().setAttribute(name,bean);
}
else if (Tokens.APPLICATION.equals(scope)) {
servlet.getServletContext().setAttribute(name,bean);
}
else {
StringBuffer sb = new StringBuffer("exposeInScope: ");
sb.append(scope);
sb.append(Tokens.INVALID_SCOPE);
servlet.log(sb.toString(),Log.DEBUG);
throw new IllegalArgumentException(sb.toString());
}
} // end exposeInScope
/**
* Save result object to servlet context.
* <p>
* <code>result.getData()</code> must return non-null.
* If <code>result.getName()</code> is null, the mapping's attribute
* (<code>mapping.getAttribute()</code>) is used instead.
* By default, this is the <code>form-bean</code>'s name.
* <p>
* If data is a Collection, only the first element is stored.
*
* @param mapping The ActionMapping used to select this instance
* @param request The HTTP request we are processing
* @param result The ProcessResult we are handling
*/
protected void checkDataSingle(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response,
ProcessResult result) {
String name = result.getName();
if (null==name) {
// use form-bean or mapping name
name = mapping.getAttribute();
result.setName(name);
}
String scope = result.getScope();
Object bean = result.getData();
// if data is collection, use first element
if (bean instanceof Collection) {
Collection collection = (Collection) bean;
if (collection.isEmpty()) {
// for lack of a better idea, get a fresh form-bean
// this will return null if there is not a form-bean
// associated with this mapping
bean = createHelperBean(request,mapping.getName());
}
else {
bean = collection.iterator().next();
}
}
if (result.isExposed()) {
exposeInScope(request,response,name,scope,bean);
}
} // end checkDataSingle
/**
* Save result object to servlet context.
* <p>
* <code>result.getData()</code> must return non-null.
* If <code>result.getName()</code> is null, the mapping's attribute
* (<code>mapping.getAttribute()</code>) is used instead.
* By default, this is the <code>form-bean</code>'s name.
*
* @param mapping The ActionMapping used to select this instance
* @param request The HTTP request we are processing
* @param result The ProcessResult we are handling
*/
protected void checkData(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response,
ProcessResult result) {
if (result.isSingleForm()) {
checkDataSingle(mapping,request,response,result);
}
else {
String name = result.getName();
if (null==name) {
name = Tokens.LIST_KEY;
result.setName(name);
}
String scope = result.getScope();
Object bean = result.getData();
if (result.isExposed()) {
exposeInScope(request,response,name,scope,bean);
}
}
} // end checkData
/**
* Stores informational messages for display.
*
* @param mapping The ActionMapping used to select this instance
* @param request The HTTP request we are processing
* @param processResult The ProcessResult we are handling
*/
protected void checkMessages(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response,
ProcessResult processResult) {
saveMessages(request,processResult.getMessages());
} // end checkMessages
/**
* Process new dispatch advice passed by the business tier.
* <p>
* This is used to route control to another location besides
* the default "success" forward registered with the controller.
*
* The business tier can pass back either a path or the name of
* an ActionForward.
* checkDispatch() will then create an ActionForward to return
* and save it in the request under the SUCCESS token.
* The <code>findSuccess()</code> will check for this attribute
* before returning the controller's default.
*
* @param mapping The ActionMapping used to select this instance
* @param request The HTTP request we are processing
* @param processResult The ProcessResult we are handling
*/
protected void checkDispatch(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response,
ProcessResult processResult) {
String dispatch = processResult.getDispatch();
ActionForward forward = null;
if (processResult.isDispatchPath()) {
forward = new ActionForward(dispatch);
}
else {
forward = mapping.findForward(dispatch);
}
// Our findSuccess looks for this
request.setAttribute(Tokens.SUCCESS,forward);
} // end checkDispatch
// --------------------------------------------------------- Public Methods
/**
* Return the appropriate ActionForward for error or failure
* conditions.
* First checks for a FAILURE ActionForward stored in the request.
* If an override is not found, returns the result of the
* superclass method.
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request
* @param request The HTTP request we are processing
* @param response The resonse we are creating
* @return The ActionForward representing FAILURE
* or null if a FAILURE forward has not been specified.
*/
protected ActionForward findFailure(
ActionMapping mapping,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -