1、初始化工作
http://127.0.0.1:8080/struts_login/login.do?
username="admin"&password="admin"
path = request.getServletPath();
path=/login.do?username="admin"&password="admin"
int slash = path.lastIndexOf("/");
slash =0;
int period = path.lastIndexOf(".");
period=6;
if ((period >= 0) && (period > slash)) {
path = path.substring(0, period);
path=/login
}
return (path);
<action-mappings>
<action path="/login"
type="com.struts.LoginAction"
name="loginForm"
scope="request"
validate="true"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
---------------------------------------------------------------------protected ActionForm processActionForm(HttpServletRequest request,HttpServletResponse response, ActionMapping mapping) {
ActionForm instance = RequestUtils.createActionForm
(request, mapping, moduleConfig, servlet);
if (instance == null) {
return (null);
}
if ("request".equals(mapping.getScope())) {
request.setAttribute(mapping.getAttribute(), instance);
} else {
HttpSession session = request.getSession();
session.setAttribute(mapping.getAttribute(), instance);
}
return (instance);
}
public static ActionForm createActionForm(HttpServletRequest request,ActionMapping mapping, ModuleConfig moduleConfig,ActionServlet servlet) {
String attribute = mapping.getAttribute();
if (attribute == null) {
return (null);
}
String name = mapping.getName();
//到formBeans的Map中取得FormBeanConfig对象
FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
if (config == null) {return (null);}
ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());
try {if (instance != null ) {return (instance); }
return createActionForm(config, servlet);
}
private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope)
{// Look up any existing form bean instance
ActionForm instance = null;
HttpSession session = null;
if ("request".equals(scope)) {
instance = (ActionForm) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (ActionForm) session.getAttribute(attribute);
}
return (instance);
}
-------------------------------------------------------------------------------------------------------------------------------
protected void processPopulate(HttpServletRequest req,HttpServletResponse response,
ActionForm form,
ActionMapping mapping)
throws ServletException {
if (form == null) {
return;
}
form.reset(mapping, request);//收集表单数据前对 表单bean的属性初始化
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
request);
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),request);
public static void populate(Object bean,String prefix,String suffix,HttpServletRequest request)
throws ServletException {
HashMap properties = new HashMap();
Enumeration names = null;
names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String stripped = name;
Object parameterValue = null;
parameterValue = request.getParameterValues(na
me);
properties.put(stripped, parameterValue);
}
BeanUtils.populate(bean, properties);
}
public static void populate(Object bean, Map properties)
throws IllegalAccessException, InvocationTargetException {
if ((bean == null) || (properties == null)) {
return;
}
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String)
names.next();
Object value = properties.get(name);
setProperty(bean, name, value);
//收集表单数据后对表单bean的属性值进行验证
if (!processValidate(request, response, form, mapping)) {
return;
}
processActionCreate创建action对象(这里actions是HashMap对象,用map实现创建action对象是单例,而且在创建的过程是加锁防止多个线程在用一个时刻访问同一个action请求)
protected ActionForward processActionPerform(HttpServletRequest request,HttpServletResponse response,Action action,ActionForm form,ActionMapping mapping)throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response,
e, form, mapping));
}
}
---------------------------------------------------------------------protected void processForwardConfig(HttpServletRequest request,
HttpServletResponse response,ForwardConfig forward)throws IOException, ServletException {
if (forward == null) { return;}
String forwardPath = forward.getPath();
String uri = null;
uri = forwardPath;
if (forward.getRedirect()) //如果为重定向
{
response.sendRedirect( uri);//客户端跳转
} else {
doForward(uri, request, response); //服务端端跳转
}
}
protected void doForward(
String uri,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
rd.forward(request, response);
}