struts1的工作流程
1. 读取配置(容器启动时一次性加载)
容器读取web.xml,自动加载设置为自动启动的ActionServlet; 读取struts-config.xml配置信息
2.发送请求
客户端发送http请求,容器收到后通过web.xml上的URL通配符(一般是*.do)找到ActionServlet;
3.填充数据
ActionServlet(Action配置path,唯一标识)从ActionConfig对象查找Action类,如果没有找到则转发给JSP或静态页面,如果有找到Action类和对应ActionForm类(Action配置name,FormBeanConfig对象中查找),实例化(Action配置或ActionForm配置type,反射);
ActionForm被实例化并用HTTP请求的数据填充其属性,并将其保存在指定域(Action配置scope)中,ServletContext、session、request;
4.派发请求
ActionServlet将请求和相应FormBean传给Action的excute()方法使用。
5.处理业务
执行相应的业务逻辑,将结果放入请求,返回ActionForward对象;
6.返回响应
ActionServlet根据ActionForward对象在ForwardConfig对象 (name)查找对应的跳转(JSP或者Action);
7.响应客户
ActionServlet将请求(参数和属性)带入下一个JSP或者Action,如果是Action将重复2.发送请求(属性可带);如果是JSP,容器会将JSP生成结果为html,然后响应给客户端;
说明:
如果Action配置里面没有name属性,将不会初始化ActionForm,Action内无法使用
html:form对应到表单上的属性,名称不一致会报错
下面使用struts1模拟一个用户登录
1 创建ActionForm
package com.itmyhome.form; import org.apache.struts.action.ActionForm; public class LoginForm 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文件中进行注册:
<form-beans> <form-bean name="loginForm" type="com.itmyhome.form.LoginForm" /> </form-beans>
package com.itmyhome.action; 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; import com.itmyhome.form.LoginForm; public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception { LoginForm loginForm = (LoginForm) form; if ("zhangsan".equals(loginForm.getUsername()) & "123".equals(loginForm.getPassword())) { return mapping.findForward("success"); } else { return mapping.findForward("error"); } } }Action类也要在struts-config.xml文件中注册
<action path="/login" type="com.itmyhome.action.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp" /> <forward name="error" path="/error.jsp" /> </action>
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.itmyhome.form.LoginForm" /> </form-beans> <action-mappings> <!-- 属性name名字接收form-bean中的参数 --> <action path="/login" type="com.itmyhome.action.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp" /> <forward name="error" path="/error.jsp" /> </action> </action-mappings> </struts-config>
<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> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
config 以相对路径的方式指明struts应用程序的配置文件的位置,如不设置,则默认为/WEB-INF/struts-config.xml
如果在src下 则配置为<param-value>/struts-config.xml</param-value>
5 创建jsp页面
login.jsp
<form action="login.do" method="post"> USERNAME:<input type="text" name="username" /></br> PASSWORD: <input type="password" name="password" /></br> <input type="submit" value="LOGIN"> </form>