详细剖析Struts工作流程
1.Web客户端提交数据到Tomcat,在form表单中需说明表单提交的action是*.do或*.action,mothod是post或get;
2.Tomcat接收Web客户端提交的表单,将表单数据打包到HttpServletRequest和HttpServletResponse对象中,然后通过doPost或doGet方式把request、response提交到ActionServlet(ActionServlet是Struts内部封装好的);
要使用Struts封装的ActionServlet,需要在web.xml中配置ActionServlet,配置信息如下:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.ActionServlet是struts的中央控制器,它任务如下:
(1)它负责截取Web客户端提交的URL。比如login.jsp的action是login.do,然后根据URL从struts-config.xml中获得配置信息。配置信息需要手动在struts-config.xml中配置,配置信息如下:
<action-mappings>
<action path="/login" type="com.tgb.struts.LoginAction"
scope="request">
<forward path="/login_success.jsp"></forward>
<forward path="/login_error.jsp"></forward>
</action>
</action-mappings>
login.jsp中action的url名必须和配置信息中的path名一致,这样ActionServlet才能根据URL找到对应的Action,完成请求响应。
(2)创建ActionForm类的对象,用于收集表单数据,ActionForm类代码如下:
package com.tgb.struts;
import org.apache.struts.action.ActionForm;
/**
* 登录ActionForm,负责表单收集数据
* 表单的属性必须和ActionForm中的get、set属性一致
* @author quwenzhe
*
*/
public class LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
创建后需要在struts-config.xml中配置ActionForm信息,这样struts才能检测到有ActionForm的存在,配置信息如下:
<form-beans>
<form-bean type="com.tgb.struts.LoginActionForm" />
</form-beans>
通过action-mappings中的scope属性,把表单数据赋值给ActionForm。
(3)创建Action,Action类代码如下:
package com.tgb.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 登录Action 负责获得表单数据,调用业务逻辑,返回转向视图
*
* @author quwenzhe
*
*/
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm) form;
String username = laf.getUsername();
String password = laf.getPassword();
if ("admin".equals(username) && "admin".equals(password)) {
// 登录成功
return mapping.findForward("success");
} else {
// 登录失败
return mapping.findForward("error");
}
}
}
action的信息已经在strut-config.xml中的action-mappings中配置。并且在配置信息中我们已经说明,forward name为"success"的对应login_success.jsp页面,forward name为"error"的对应login_error.jsp页面。
(4)调用action的execute方法,将ActionForm中的信息提交到业务层控制器Action中处理