struts框架中只有一个ActionServlet,但可以有多个客户化的requestProcesscor。对于struts来说真正做控制的是RequestProecssor和Action。
其中Action的process方法最终调用的是RequestProcessor类中的process方法。
下面是该方法的源码:
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// 如果HTTP请求方式为post,并且contentType为”multipart/form-data”开头,标准的HttpServletRequest对象将被重新包装,
// 以方便处理”multipart”类型的HTTP请求如果请求方式为get,或正congtentType属性不是”mulitipart”,就直接返回原始的HttpServletRequest对象.
request = processMultipart(request);
// 获得请求的URI的路径,这一信息可用于选择合适的Struts Action组件.
String path = processPath(request, response);
if (path == null) {
return;
}
if (log.isDebugEnabled()) {
log.debug("Processing a '" + request.getMethod() +
"' for path '" + path + "'");
}
// 当ControllerConfig对象的locale属性为true,将读取用户请求中包含的Locale信息,然后把Locale实例保存在session范围内.
processLocale(request, response);
// 读取ControllerConfig对象的conttentType属性,
// 然后调用response.setContentType(contentType)方法,设置响应结果的文档类型和字符编码.
processContent(request, response);
// 读取ControllerConfig对象的nocache属性,
// 如果nocache属性为true,在响应结果中将加入特定的头参数:Pragma,Cache-Control和Expires
processNoCache(request, response);
// 该方法不执行任何操作.直接返回true.子类可以覆盖这个方法.
// 执行客户化的预处理请求操作.
if (!processPreprocess(request, response)) {
return;
}
this.processCachedMessages(request, response);
// 寻找和用户请求的URI匹配的ActionMapping,如果不存在这样的ActionMapping,则向用户返回恰当的错误信息
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
}
// 先判断是否为Action配置了安全角色,如果配置了安全角色,就调用isUserInRole()方法判断当前用户是否具备必需的角色,
// 如果不具备,就结束请求处理流程.,向用户返回恰当的错误消息
if (!processRoles(request, response, mapping)) {
return;
}
// 先判断是否为ActionMapping配置了ActionForm,如果配置了ActionForm,就先从ActionForm的存在范围内(request或session)寻找改ActionForm实例,
// 如果不存在,就创建一个实例,接下来把它保存在合适的范围内,保存时使用的属性key为ActionMapping的name属性。
ActionForm form = processActionForm(request, response, mapping);
// 如果为ActionMapping配置了ActionForm,就先调用ActionForm的reset()方法,再把请求中的表单数据组装到ActionForm中
processPopulate(request, response, form, mapping);
// 如果为ActionMapping配置了ActionForm,并且ActionMapping的validate属性为true,
// 就调用ActionForm的validate()方法,如果validate方法返回的ActionErrors对象中包含ActionMessage对象,
// 说明表单验证失败。就把ActionErrors对象放在request范围内,再把请求转发到ActionMapping的input属性指定的Web组件。
// 如果ActionForm的validate方法执行表单验证成功,就继续执行下面的处理流程。
try {
if (!processValidate(request, response, form, mapping)) {
return;
}
} catch (InvalidCancelException e) {
ActionForward forward = processException(request, response, e, form, mapping);
processForwardConfig(request, response, forward);
return;
} catch (IOException e) {
throw e;
} catch (ServletException e) {
throw e;
}
// Process a forward or include specified by this mapping
if (!processForward(request, response, mapping)) {
return;
}
if (!processInclude(request, response, mapping)) {
return;
}
// 先判断是否在Action缓存中存在这个Action实例,如果没有就新建一个Action实例,
// 把它放在Action缓存中。可以看出Action也是只有一个实例在运行的
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
// Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping);
// 把你的Action的excute方法返回的ActionFoward对象作为参数传给它,
// processActionForward对象包的请求转发信息来执行请求转发或重定向。
processForwardConfig(request, response, forward);
}
下面是我自己写的一个实际使用的类,对于一般的小型系统的权限控制个人感觉够用: