|
17、为ActionForm填充数据。
processPopulate(request, response, form, mapping);
1、首先判定form是否为null,假如是则直接return。
if (form == null) {
return;
}
2、往log里写入一句话提示从这里开始填充formBean
if (log.isDebugEnabled()) {
log.debug(" Populating bean properties from this request");
}
3、设置Servletservletservletservletservletservletservletservletstrutsservletservletstrutsstrutsservletstrutsservletservletservletservletservletservletservletservletservlet。
form.set(this.);
(不知道详细作用)
4、执行reset方法重置表单。默认reset方法不做任何事情。这个方法是为了利便扩展。可以继续ActionForm类重写reset方法,这个方法可以用来做设置一些默认值等工作。
form.reset(mapping, request);
5、假如是上传表单,则获取上传类。(不甚了解)
if (mapping.getMultipartClass() != null) {
request.setAttribute(Globals.MULTIPART_KEY,
mapping.getMultipartClass());
}
6、填充form
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(), request);
1、建立一个HashMap 用于存放属性
HashMap properties = new HashMap();
2、建立一个Enumeration用于存放参数名
Enumeration names = null;
3、建立一个Map来存放multipart参数
Map multipartParameters = null;
4、获取哀求的ContentType和Method。并设置multipart表示为false。
String contentType = request.getContentType();
String method = request.getMethod();
boolean isMultipart = false;
5、假如是multipart表单则做上传处理(不甚了解)
if (bean instanceof ActionForm) {
((ActionForm) bean).setMultipartRequestHandler(null);
}
MultipartRequestHandler multipartHandler = null;
if ((contentType != null)&& (contentType.startsWith("multipart/form-data"))&& (method.equalsIgnoreCase("OST"))) {
ActionWrapper ;
if (bean instanceof ActionForm) {
= ((ActionForm) bean).getWrapper();
} else {
throw new Exception("bean that's supposed to be "
+ "populated from a multipart request is not of type "
+ "\"org.apache..action.ActionForm\", but type "
+ "\"" + bean.getClass().getName() + "\"");
}
multipartHandler = getMultipartHandler(request);
if (multipartHandler != null) {
isMultipart = true;
.setFor(multipartHandler);
multipartHandler.setMapping((ActionMapping) request .getAttribute(Globals.MAPPING_KEY));
multipartHandler.handleRequest(request);
Boolean maxLengthExceeded =
(Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null)
&& (maxLengthExceeded.booleanValue())) {
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
return;
}
multipartParameters =
getAllParametersForMultipartRequest(request,
multipartHandler);
names = Collections.enumeration(multipartParameters.keySet());
}
}
6、假如不是上传,则把参数名存到names枚举里面。
if (!isMultipart) {
names = request.getParameterNames();
}
7、遍历这个枚举
while (names.hasMoreElements())
1、把名字拿出来存到name和stripped变量里
String name = (String) names.nextElement();
String stripped = name;
2、去掉name的前缀和后缀(假如有的话(配置文件里可以配置))
if (prefix != null) {
if (!stripped.startsWith(prefix)) {
continue;
}
stripped = stripped.substring(prefix.length());
}
if (suffix != null) {
if (!stripped.endsWith(suffix)) {
continue;
}
stripped = stripped.substring(0, stripped.length() - suffix.length());
}
3、获取参数值,分上传和非上传两种方式
Object parameterValue = null;
if (isMultipart) {
parameterValue = multipartParameters.get(name);
parameterValue = rationalizeMultipleFileProperty(bean, name, parameterValue);
} else {
parameterValue = request.getParameterValues(name);
}
4、假如参数名去掉了前缀后缀不是以org.Apache.开头则把参数存到定义好的HashMap里
if (!(stripped.startsWith("org.apache.."))) {
properties.put(stripped, parameterValue);
}
8、调用BeanUtils的方法把formBean的属性填充进去
try {
BeanUtils.populate(bean, properties);
} catch (Exception e) {
throw new Exception("BeanUtils.populate", e);
} finally {
if (multipartHandler != null) {
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
}
}
7、加入合适的话就把退出属性设置到request里;
if ((request.getParameter(Globals.CANCEL_PROPERTY) != null)
|| (request.getParameter(Globals.CANCEL_PROPERTY_X) != null)) {
request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
}
18、验证表单输入的正当性。假如有不正当的则return。(一般不用的表单级验证)
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 (Exception e) {
throw e;
}
19、处理mapping指定的forward 和 include
if (!processForward(request, response, mapping)) {
return;
}
if (!processInclude(request, response, mapping)) {
return;
}
20、创建或者获取一个Action的实例来处理哀求。
Action action = processActionCreate(request, response, mapping);
1、从mapping里掏出配置的Action类名
String className = mapping.getType();
2、把查找Action实例的动作记入到日志里
if (log.isDebugEnabled()) {
log.debug(" Looking for Action instance for class " + className);
}
3、在拿到Action实例之前先线程同步synchronized (actions) ,保证只有一个Action实例
4、从map里掏出Action返回,(假如有的话),并把结果写入日志
nstance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
}
5、假如上面的操纵没进行,那说明要新建一个Action实例,把新建实例的动作记实到日志里
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
}
6、创建出Action实例,把实例放到map里并返回实例
try {
instance = (Action) RequestUtils.applicationInstance(className);
} catch (Exception e) {
log.error(getInternal().getMessage("actionCreate",
mapping.getPath()), e);
response.sendError(HttpResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("actionCreate", mapping.getPath()));
return (null);
}
actions.put(className, instance);
if (instance.get() == null) {
instance.set(this.);
}
}
return (instance)
21、再次判定Action是否创建成功,假如没有则方法直接return
if (action == null) {
return;
}
22、执行Action的excute方法,获得ActionForward
ActionForward forward = processActionPerform(request, response, action, form, mapping);
//其中processActionPerform方法调用了action的excute方法:
protected ActionForward processActionPerform(HttpRequest request,
HttpResponse response, Action action, ActionForm form,
ActionMapping mapping)
throws IOException, Exception {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response, e, form, mapping));
}
}
这里也做了一个处理,假如要在执行excute方法之前做一些操纵,就可以笼盖processActionPerform方法。
23、更具Actionforward进行转发
processForwardConfig(request, response, forward)
1、假如ActionForward为空,则方法直接返回
if (forward == null) {
return;
}
2、把接下来处理forward的操纵记实到日志里
if (log.isDebugEnabled()) {
log.debug("processForwardConfig(" + forward + ")");
}
3、从mapping里获取forward对应的url,默认用forward的方式转发,假如配了redirect,则用redirect重定向
String forwardPath = forward.getPath();
String uri;
String actionIdPath = RequestUtils.actionIdURL(forward, request, );
if (actionIdPath != null) {
forwardPath = actionIdPath;
ForwardConfig actionIdForward = new ForwardConfig(forward);
actionIdForward.setPath(actionIdPath);
forward = actionIdForward;
}
if (forwardPath.startsWith("/")) {
uri = RequestUtils.forwardURL(request, forward, null);
} else {
uri = forwardPath;
}
if (forward.getRedirect()) {
if (uri.startsWith("/")) {
uri = request.getContextPath() + uri;
}
response.sendRedirect(response.encodeRedirectURL(uri));
} else {
doForward(uri, request, response);
|
|
|