Struts1.3.10深入源代码之核心类Action类

Action类是用户请求和业务处理逻辑之间的桥梁。每个Action类充当客户的一项业务代理。在RequestProcessor类预处理请求时,在创建Action的实例后,就调用自身的 processActionPerform()方法,该方法在调用Action类的execute()方法。
在Struts中所有的Action都继承org.apache.struts.action.Action类。
package org.apache.struts.action;
import org.apache.struts.Globals;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.TokenProcessor;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Locale;

public class Action {
/**
* <p>An instance of <code>TokenProcessor</code> to use for token
* functionality.</p>
*/
private static TokenProcessor token = TokenProcessor.getInstance();

// NOTE: We can make the tken variable protected and remove Action's
// token methods or leave it private and allow the token methods to
// delegate their calls.
// ----------------------------------------------------- Instance Variables

/**
* <p>The servlet to which we are attached.</p>
*/
protected transient ActionServlet servlet = null;

// ------------------------------------------------------------- Properties

/**
* <p>Return the servlet instance to which we are attached.</p>
*
* @return The servlet instance to which we are attached.
*/
public ActionServlet getServlet() {
return (this.servlet);
}

/**
* <p>Set the servlet instance to which we are attached (if
* <code>servlet</code> is non-null), or release any allocated resources
* (if <code>servlet</code> is null).</p>
*
* @param servlet The new controller servlet, if any
*/
public void setServlet(ActionServlet servlet) {
this.servlet = servlet;

// :FIXME: Is this suppose to release resources?
}

// --------------------------------------------------------- Public Methods

/**
* <p>Process the specified non-HTTP request, and create the corresponding
* non-HTTP response (or forward to another web component that will create
* it), with provision for handling exceptions thrown by the business
* logic. Return an {@link ActionForward} instance describing where and
* how control should be forwarded, or <code>null</code> if the response
* has already been completed.</p>
*
* <p>The default implementation attempts to forward to the HTTP version
* of this method.</p>
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The non-HTTP request we are processing
* @param response The non-HTTP response we are creating
* @return The forward to which control should be transferred, or
* <code>null</code> if the response has been completed.
* @throws Exception if the application business logic throws an
* exception.
* @since Struts 1.1
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
ServletRequest request, ServletResponse response)
throws Exception {
try {
return execute(mapping, form, (HttpServletRequest) request,
(HttpServletResponse) response);
} catch (ClassCastException e) {
return null;
}
}

/**
* <p>Process the specified HTTP request, and create the corresponding
* HTTP response (or forward to another web component that will create
* it), with provision for handling exceptions thrown by the business
* logic. Return an {@link ActionForward} instance describing where and
* how control should be forwarded, or <code>null</code> if the response
* has already been completed.</p>
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
* @return The forward to which control should be transferred, or
* <code>null</code> if the response has been completed.
* @throws Exception if the application business logic throws an
* exception
* @since Struts 1.1
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return null;
}

// ---------------------------------------------------- Protected Methods

/**
* Adds the specified messages keys into the appropriate request attribute
* for use by the <html:messages> tag (if messages="true" is set),
* if any messages are required. Initialize the attribute if it has not
* already been. Otherwise, ensure that the request attribute is not set.
*
* @param request The servlet request we are processing
* @param messages Messages object
* @since Struts 1.2.1
*/
protected void addMessages(HttpServletRequest request,
ActionMessages messages) {
if (messages == null) {
// bad programmer! *slap*
return;
}

// get any existing messages from the request, or make a new one
ActionMessages requestMessages =
(ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);

if (requestMessages == null) {
requestMessages = new ActionMessages();
}

// add incoming messages
requestMessages.add(messages);

// if still empty, just wipe it out from the request
if (requestMessages.isEmpty()) {
request.removeAttribute(Globals.MESSAGE_KEY);

return;
}

// Save the messages
request.setAttribute(Globals.MESSAGE_KEY, requestMessages);
}

/**
* Adds the specified errors keys into the appropriate request attribute
* for use by the <html:errors> tag, if any messages are required.
* Initialize the attribute if it has not already been. Otherwise, ensure
* that the request attribute is not set.
*
* @param request The servlet request we are processing
* @param errors Errors object
* @since Struts 1.2.1
*/
protected void addErrors(HttpServletRequest request, ActionMessages errors) {
if (errors == null) {
// bad programmer! *slap*
return;
}

// get any existing errors from the request, or make a new one
ActionMessages requestErrors =
(ActionMessages) request.getAttribute(Globals.ERROR_KEY);

if (requestErrors == null) {
requestErrors = new ActionMessages();
}

// add incoming errors
requestErrors.add(errors);

// if still empty, just wipe it out from the request
if (requestErrors.isEmpty()) {
request.removeAttribute(Globals.ERROR_KEY);

return;
}

// Save the errors
request.setAttribute(Globals.ERROR_KEY, requestErrors);
}

/**
* <p>Generate a new transaction token, to be used for enforcing a single
* request for a particular transaction.</p>
*
* @param request The request we are processing
* @return The new transaction token.
*/
protected String generateToken(HttpServletRequest request) {
return token.generateToken(request);
}

/**
* Retrieves any existing errors placed in the request by previous
* actions. This method could be called instead of creating a <code>new
* ActionMessages()</code> at the beginning of an <code>Action</code>.
* This will prevent saveErrors() from wiping out any existing Errors
*
* @param request The servlet request we are processing
* @return the Errors that already exist in the request, or a new
* ActionMessages object if empty.
* @since Struts 1.2.1
*/
protected ActionMessages getErrors(HttpServletRequest request) {
ActionMessages errors =
(ActionMessages) request.getAttribute(Globals.ERROR_KEY);

if (errors == null) {
errors = new ActionMessages();
}

return errors;
}

/**
* <p>Return the user's currently selected Locale.</p>
*
* @param request The request we are processing
* @return The user's currently selected Locale.
*/
protected Locale getLocale(HttpServletRequest request) {
return RequestUtils.getUserLocale(request, null);
}

/**
* <p> Retrieves any existing messages placed in the request by previous
* actions. This method could be called instead of creating a <code>new
* ActionMessages()</code> at the beginning of an <code>Action</code> This
* will prevent saveMessages() from wiping out any existing Messages </p>
*
* @param request The servlet request we are processing
* @return the Messages that already exist in the request, or a new
* ActionMessages object if empty.
* @since Struts 1.2.1
*/
protected ActionMessages getMessages(HttpServletRequest request) {
ActionMessages messages =
(ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);

if (messages == null) {
messages = new ActionMessages();
}

return messages;
}

/**
* <p>Return the default message resources for the current module.</p>
*
* @param request The servlet request we are processing
* @return The default message resources for the current module.
* @since Struts 1.1
*/
protected MessageResources getResources(HttpServletRequest request) {
return ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY));
}

/**
* <p>Return the specified message resources for the current module.</p>
*
* @param request The servlet request we are processing
* @param key The key specified in the message-resources element for
* the requested bundle.
* @return The specified message resource for the current module.
* @since Struts 1.1
*/
protected MessageResources getResources(HttpServletRequest request,
String key) {
// Identify the current module
ServletContext context = getServlet().getServletContext();
ModuleConfig moduleConfig =
ModuleUtils.getInstance().getModuleConfig(request, context);

// Return the requested message resources instance
return (MessageResources) context.getAttribute(key
+ moduleConfig.getPrefix());
}

/**
* <p>Returns <code>true</code> if the current form's cancel button was
* pressed. This method will check if the <code>Globals.CANCEL_KEY</code>
* request attribute has been set, which normally occurs if the cancel
* button generated by <strong>CancelTag</strong> was pressed by the user
* in the current request. If <code>true</code>, validation performed by
* an <strong>ActionForm</strong>'s <code>validate()</code> method will
* have been skipped by the controller servlet.</p>
*
* <p> Since Action 1.3.0, the mapping for a cancellable Action must also have
* the new "cancellable" property set to true. If "cancellable" is not set, and
* the magic Cancel token is found in the request, the standard Composable
* Request Processor will throw an InvalidCancelException. </p>
*
* @param request The servlet request we are processing
* @return <code>true</code> if the cancel button was pressed;
* <code>false</code> otherwise.
*/
protected boolean isCancelled(HttpServletRequest request) {
return (request.getAttribute(Globals.CANCEL_KEY) != null);
}

/**
* <p>Return <code>true</code> if there is a transaction token stored in
* the user's current session, and the value submitted as a request
* parameter with this action matches it. Returns <code>false</code> under
* any of the following circumstances:</p>
*
* <ul>
*
* <li>No session associated with this request</li>
*
* <li>No transaction token saved in the session</li>
*
* <li>No transaction token included as a request parameter</li>
*
* <li>The included transaction token value does not match the transaction
* token in the user's session</li>
*
* </ul>
*
* @param request The servlet request we are processing
* @return <code>true</code> if there is a transaction token and it is
* valid; <code>false</code> otherwise.
*/
protected boolean isTokenValid(HttpServletRequest request) {
return token.isTokenValid(request, false);
}

/**
* <p>Return <code>true</code> if there is a transaction token stored in
* the user's current session, and the value submitted as a request
* parameter with this action matches it. Returns <code>false</code> under
* any of the following circumstances:</p>
*
* <ul>
*
* <li>No session associated with this request</li> <li>No transaction
* token saved in the session</li>
*
* <li>No transaction token included as a request parameter</li>
*
* <li>The included transaction token value does not match the transaction
* token in the user's session</li>
*
* </ul>
*
* @param request The servlet request we are processing
* @param reset Should we reset the token after checking it?
* @return <code>true</code> if there is a transaction token and it is
* valid; <code>false</code> otherwise.
*/
protected boolean isTokenValid(HttpServletRequest request, boolean reset) {
return token.isTokenValid(request, reset);
}

/**
* <p>Reset the saved transaction token in the user's session. This
* indicates that transactional token checking will not be needed on the
* next request that is submitted.</p>
*
* @param request The servlet request we are processing
*/
protected void resetToken(HttpServletRequest request) {
token.resetToken(request);
}

/**
* <p>Save the specified error messages keys into the appropriate request
* attribute for use by the <html:errors> tag, if any messages are
* required. Otherwise, ensure that the request attribute is not
* created.</p>
*
* @param request The servlet request we are processing
* @param errors Error messages object
* @since Struts 1.2
*/
protected void saveErrors(HttpServletRequest request, ActionMessages errors) {
// Remove any error messages attribute if none are required
if ((errors == null) || errors.isEmpty()) {
request.removeAttribute(Globals.ERROR_KEY);

return;
}

// Save the error messages we need
request.setAttribute(Globals.ERROR_KEY, errors);
}

/**
* <p>Save the specified messages keys into the appropriate request
* attribute for use by the <html:messages> tag (if messages="true"
* is set), if any messages are required. Otherwise, ensure that the
* request attribute is not created.</p>
*
* @param request The servlet request we are processing.
* @param messages The messages to save. <code>null</code> or empty
* messages removes any existing ActionMessages in the
* request.
* @since Struts 1.1
*/
protected void saveMessages(HttpServletRequest request,
ActionMessages messages) {
// Remove any messages attribute if none are required
if ((messages == null) || messages.isEmpty()) {
request.removeAttribute(Globals.MESSAGE_KEY);

return;
}

// Save the messages we need
request.setAttribute(Globals.MESSAGE_KEY, messages);
}

/**
* <p>Save the specified messages keys into the appropriate session
* attribute for use by the <html:messages> tag (if messages="true"
* is set), if any messages are required. Otherwise, ensure that the
* session attribute is not created.</p>
*
* @param session The session to save the messages in.
* @param messages The messages to save. <code>null</code> or empty
* messages removes any existing ActionMessages in the
* session.
* @since Struts 1.2
*/
protected void saveMessages(HttpSession session, ActionMessages messages) {
// Remove any messages attribute if none are required
if ((messages == null) || messages.isEmpty()) {
session.removeAttribute(Globals.MESSAGE_KEY);

return;
}

// Save the messages we need
session.setAttribute(Globals.MESSAGE_KEY, messages);
}

/**
* <p>Save the specified error messages keys into the appropriate session
* attribute for use by the <html:messages> tag (if
* messages="false") or <html:errors>, if any error messages are
* required. Otherwise, ensure that the session attribute is empty.</p>
*
* @param session The session to save the error messages in.
* @param errors The error messages to save. <code>null</code> or empty
* messages removes any existing error ActionMessages in
* the session.
* @since Struts 1.3
*/
protected void saveErrors(HttpSession session, ActionMessages errors) {
// Remove the error attribute if none are required
if ((errors == null) || errors.isEmpty()) {
session.removeAttribute(Globals.ERROR_KEY);

return;
}

// Save the errors we need
session.setAttribute(Globals.ERROR_KEY, errors);
}

/**
* <p>Save a new transaction token in the user's current session, creating
* a new session if necessary.</p>
*
* @param request The servlet request we are processing
*/
protected void saveToken(HttpServletRequest request) {
token.saveToken(request);
}

/**
* <p>Set the user's currently selected <code>Locale</code> into their
* <code>HttpSession</code>.</p>
*
* @param request The request we are processing
* @param locale The user's selected Locale to be set, or null to select
* the server's default Locale
*/
protected void setLocale(HttpServletRequest request, Locale locale) {
HttpSession session = request.getSession();

if (locale == null) {
locale = Locale.getDefault();
}

session.setAttribute(Globals.LOCALE_KEY, locale);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
,发送别,概率,以及物体在相机坐标系下的xyz.zip目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的别。由于各物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分问题:判断图像中的目标属于哪个别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分 基于深度学习的目标检测算法主要分为两大: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的别。由于各物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分问题:判断图像中的目标属于哪个别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分 基于深度学习的目标检测算法主要分为两大: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的别。由于各物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分问题:判断图像中的目标属于哪个别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分 基于深度学习的目标检测算法主要分为两大: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值