org.apache.struts.action.RequestProcessor.process(request, response)方法:
首先分析request的地址,得到path例如/login,然后做一些local,cache处理
接着// Identify the mapping for this request
ActionMapping mapping = processMapping(request, response, path);
根据path在moduleConfig里查找ActionConfig强制转换为ActionMapping
protected ActionMapping processMapping(HttpServletRequest request,
HttpServletResponse response, String path)
throws IOException {
// Is there a mapping for this path?
ActionMapping mapping =
(ActionMapping) moduleConfig.findActionConfig(path);
// If a mapping is found, put it in the request and return it
if (mapping != null) {
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
// Locate the mapping for unknown paths (if any)
ActionConfig[] configs = moduleConfig.findActionConfigs();
for (int i = 0; i < configs.length; i++) {
if (configs[i].getUnknown()) {
mapping = (ActionMapping) configs[i];
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
}
// No mapping can be found to process this request
String msg = getInternal().getMessage("processInvalid");
log.error(msg + " " + path);
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
return null;
}
找到ActionMapping后角色权限判断,创建FormBean
ActionForm form = processActionForm(request, response, mapping);
如果mapping里设置了scope为request就把这个formBean放到request里,否则就放到session里。
processPopulate(request, response, form, mapping);
先执行form.setServlet(this.servlet);
form.reset(mapping, request);
进入方法RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(), request);
开始收集表单数据
public static void populate(Object bean, String prefix, String suffix,
HttpServletRequest request)
throws ServletException {
// Build a list of relevant request parameters from this request
HashMap properties = new HashMap();
// Iterator of parameter names
Enumeration names = null;
// Map for multipart parameters
Map multipartParameters = null;
String contentType = request.getContentType();
String method = request.getMethod();
boolean isMultipart = false;
if (bean instanceof ActionForm) {
((ActionForm) bean).setMultipartRequestHandler(null);
}
MultipartRequestHandler multipartHandler = null;
if ((contentType != null)
&& (contentType.startsWith("multipart/form-data"))
&& (method.equalsIgnoreCase("POST"))) {
//如果是上传文件,这里省略,以后由时间再详细读
}
if (!isMultipart) {//如果不是上传,得到所有属性名
names = request.getParameterNames();
}
//遍历属性名得到它的所有取值
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String stripped = name;
......
//分析处理属性名
......
Object parameterValue = null;
if (isMultipart) {
parameterValue = multipartParameters.get(name);
} else {
//这里使用getParameterValues得到一个数组如多选框的数值
parameterValue = request.getParameterValues(name);
}
// Populate parameters, except "standard" struts attributes
// such as 'org.apache.struts.action.CANCEL'
if (!(stripped.startsWith("org.apache.struts."))) {
//把request里所有键值对存放到HashMap里
properties.put(stripped, parameterValue);
}
}
// Set the corresponding相应的 properties of our bean
try {
BeanUtils.populate(bean, properties);
} catch (Exception e) {
throw new ServletException("BeanUtils.populate", e);
} finally {
if (multipartHandler != null) {
// Set the multipart request handler for our ActionForm.
// If the bean isn't an ActionForm, an exception would have been
// thrown earlier, so it's safe to assume that our bean is
// in fact an ActionForm.
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
}
}
}
将表单数据收集到form中后就调用验证机制
然后判断mapping里有没有配置转向信息,有的话就转向,没由就继续
// Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
根据mapping里配置的type创建或得到Action实例,单例模式
// Call the Action instance itself
ActionForward forward = processActionPerform(request, response, action, form, mapping);
这里就到了我们自己写的Action处理代码了,所以我们的execute里有传递过来的参数request,form,response,mapping
protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response, Action action, ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
//处理完成返回ActionForward
//mapping.findForward("success");
//根据这个字符串ForwardConfig config = (ForwardConfig)forwards.get("success");
//如果config为空到ModuleConfig里找并转换为ActionForward
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response, e, form, mapping));
}
}
// 最后处理转向Process the returned ActionForward instance
processForwardConfig(request, response, forward);
protected void processForwardConfig(HttpServletRequest request,
HttpServletResponse response, ForwardConfig forward)
throws IOException, ServletException {
......
String forwardPath = forward.getPath();//如:/success.jsp
String uri;
// If the forward can be unaliased into an action, then use the path of the action是否转到其他的Action上
String actionIdPath = RequestUtils.actionIdURL(forward, request, servlet);
if (actionIdPath != null) {
forwardPath = actionIdPath;
ForwardConfig actionIdForward = new ForwardConfig(forward);
actionIdForward.setPath(actionIdPath);
forward = actionIdForward;
}
// paths not starting with / should be passed through without any
// processing (ie. they're absolute)
if (forwardPath.startsWith("/")) {
// get module relative uri
uri = RequestUtils.forwardURL(request, forward, null);
} else {
uri = forwardPath;
}
//判断转发或重定向
if (forward.getRedirect()) {
// only prepend context path for relative uri
if (uri.startsWith("/")) {
uri = request.getContextPath() + uri;
}
response.sendRedirect(response.encodeRedirectURL(uri));
} else {
doForward(uri, request, response);
}
}
分享到:
相关推荐
struts2学习笔记总结
Struts2学习笔记,介绍了struts2的基础部分
struts2学习笔记,非本人所写,但有学习的价值,总结的很好,分享一个!
1. struts2框架的引入 1)把struts2的相关jar包导入到项目中去 2)把struts2框架的配置文件struts.xml复制粘贴到项目中的src下面(同时也可以把log4j.properties放到src下) 在这里我们主要是要的这个struts.xml文件...
struts2学习笔记struts2学习笔记struts2学习笔记
这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用
本人学习struts2的笔记,希望大家可以多多学习以后共同交流
struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换struts2学习笔记3数据类型转换
struts2.0学习笔记1 自己动手做的还算可以的 ]struts2.0学习笔记1 自己动手做的还算可以的struts2.0学习笔记1 自己动手做的还算可以的struts2.0学习笔记1 自己动手做的还算可以的
structs2很详细的学习笔记,structs2的建造,工作原理,例子,逐步讲解,纯文字的
Struts1及14. Struts2学习笔记,可快速入门。内含实例。 更多:http://download.csdn.net/user/daillo/all
struts2四天的学习笔记。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
namespace :对应与项目名称后面的"/"(例如Struts2_0100_Introduction后面的"/") (http://localhost:8080/Struts2_0100_Introduction/) 四、 标签 是用来解决重名的问题,例如当系统的前台和后台都有一个action...
Struts1学习笔记总结.pdfStruts1学习笔记总结.pdfStruts1学习笔记总结.pdfStruts1学习笔记总结.pdfStruts1学习笔记总结.pdf
个人收藏,纯属备份作用,做个记录,方便需要时候查看