Struts1 学习笔记2
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*.getUnknown()) {
mapping = (ActionMapping) configs*;
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);
}
}