离开学越来越近了,今天有一块参加培训的哥们离开了,这里祝福他一路平安。今天是Struts的第一天,也是冲刺班的开始。从今天开始,我们将在代码的海洋中度过艰苦而有意义的两个月。
下面就今天Struts知识做个简单的总结吧
1. 搭建 struts 环境
1). jar 包 --> lib, 加入到当前的 classPath 下
2). web.xml 文件和 struts-config.xml 文件 --> WEB-INF, web.xml 文件的约束声明需要该一下, 改成 2.4 以上, 可以去 tomcat 中的 web.xml 文件中复制
3). MessageResources.properties 文件 --> src
4). 需要在 web.xml 文件配置 struts 的 ActionServlet
2. Action 的 execute 方法相关参数
1). ActionForward: 和 struts-config.xml 文件中的 <forward name="success" path="/hello.jsp" /> 节点对应
2). ActionMapping: 和当前 Action 的 <action></action> 节点对应
3). ActionForm: 和当前 Action 的 name 属性指定的 ActionForm 对应
3. 请求流程:
1). hello.do --> ActionServlet
2). ActionServlet 解析 servletPath: hello 在 struts-config.xml 文件中找到对应的 action
节点
3). 如果在该 action 节点中配置了 name 属性, 会把表单字段信息封装到该 From 中, 要求该 Form 提供和表单字段相同的属性.
4). ActionServlet 会创建对应的 Action 对象, 调用其 execute() 方法
4. 简单验证的步骤
1). 覆盖 ActionForm 的 validate() 方法, 使该方法可以进行简单验证: 验证字段是否为空, 输入的值是否合法.
2). 在 MessageResources.properties 文件添加出错时的显示信息.
3). 配置 struts-config.xml 文件: validate="true"(也可以不配置, 因为默认值为 true); input="/xxx.jsp" 当 validate() 方法
验证没有通过时, 会将页面派发到 input 指定的页面.
4). 在input 指定的页面通过 <html:errors/> 显示错误信息.
5. 复杂验证的步骤
1). 在 Action 的 execute() 方法中进行复杂验证
2). 若验证不能通过需要调用 Action 的 saveErrors(request, errors) 方法保存 errors 信息.
6. 细节详解
1). 在 Action 的 execute() 方法中得到的 ActionForm 对象实际上是被存储在 request 或 session 域中, 是哪个取决于 action 节点的 scope 属性值, 该属性只有两个合法值: request, session. 默认为 session
2). 存储在 request 或 session 中键为: action 节点的 attribute 属性值. 该属性的默认值为 name 属性的值.