struts的基本编写流程:
1 web.xml中指定
*.do ===== org.apache.struts.action.ActionServlet
2 要求客户端发送请求必须遵循*.do的方式
3 web处理类 extends Action{
execute(ActionMapping,ActionForm,HttpServletRequest,HttpServletResponse) {
//获取客户端页面中的参数
//req.getParameter("uname");
MyForm f = (MyForm)form;
f.getUname();
//调用业务层的方法
biz.**();
//根据返回结果,决定跳转的目标资源
return mapping.findForward("逻辑名称");
}
}
[4 编写formBean]
** extends ActionForm{
提供私有的属性,属性名必须和页面中控件的名字一样
提供上述属性对应的共有set/get
}
5 配置struts-config.xml
<form-bean name="formBean的逻辑名称" type="formBean的类路径">
<action path="客户端发送的请求" name="formBean的逻辑名称" type="action的类路径">
path接受信息,查找name属性是否存在,存在,则使用页面中的form表单中的各个控件值填充formBean的各个同名属性值。然后查找type属性对应的action信息.
ForwardAction的作用及编写方式:
作用:页面跳转页面,为了符合mvc的核心控制思想
页面-----ForwardAction----页面
编写:
1 页面:
<a href="page.do">
2 struts-config.xml中
<action path="/page" type="org.apache.struts.actions.ForwardAction"
parameter="真实资源路径">
ActionForm在项目的数量:
0
1
n
add.jsp ----->
AllForm -----> TestAllFormAction
testAllForm.jsp ----->
ActionForm和页面的对应关系:
任意页面----0个form:
在Action中获取参数,只能手动抓取:req.getPrameter();
一个页面---1 个form
在Action中获取参数,可以手动抓取,也可以通过form获取
多个页面---1 个form
在Action中获取参数,可以手动抓取,也可以通过form获取
struts1的缺陷:
1 struts1标签直接耦合了ActionForm
2 基于struts1框架的web项目,和struts1 api强耦合
注意:
ActionForm中会保存历史页面中的相关参数信息
为了防止数据冲突,则需要在formBean,覆盖父类ActionForm的方法reset
在reset方法中,将当前formBean中的所有属性值设为默认值。
如:
@Override
public void reset(ActionMapping mapping, HttpServletRequest req) {
this.uname = null;
this.birthday = null;
this.stuNo = null;
this.var1 = 0;
this.var2 = null;
}
校验:
1 客户端校验 javaScript
服务器端校验 ActionForm biz层
2 格式校验 javaScript ActionForm
业务校验 biz层
ActionForm的作用:
1 自动接受客户端输入的数据,并且自动类型转换
2 针对客户端提交的数据进行服务器端格式化校验
ActionForm服务器端格式化校验的编写流程:
1 覆盖ActionForm父类中的validate方法,将校验规则写在该方法中
public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
ActionErrors errors = new ActionErrors();
if(this.getUname() == ""){
ActionMessage error = new ActionMessage("error.uname.empty");
errors.add("", error);
}
if(this.getUname().length() > 2){
ActionMessage error = new ActionMessage("error.uname.length");
errors.add("", error);
}
return errors;
}
2 编写资源文件messageReourse.properties,填写错误描述信息
error.uname.empty=/u59d3/u540d/u4e0d/u80fd/u4e3a/u7a7a
error.uname.length=/u59d3/u540d/u7684/u957f/u5ea6/u5fc5/u987b/u5c0f/u4e8e2
3 配置资源文件的路径
<message-resources parameter="messageResourses"></message-resources>
4 <action path="请求信息" name="formBean的逻辑名称"
validate="true" input="验证失败跳转的资源路径" type="action的类路径">
5 在页面中显示错误描述信息
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<html:errors/>
多个业务流程对应同一个action:
all.do---->AllAction
add.jsp ---all.do?name=add
delete.jsp ---all.do?name=delete
AllAction extends Action{
execute(mapping,form,req,resp){
String methodName = req.getParameter("name");
if(methodName.equals("add")){
add(mapping,form,req,resp)
}
if(methodName.equals("delete")){
delete(mapping,form,req,resp)
}
}
public add(mapping,form,req,resp){
...add
}
public delete(mapping,form,req,resp){
...delete
}
}
add.jsp-----/add.do?methodName=add-------------->StuMgmtAction--addStu方法
index.jsp---/findAllStus.do?methodName=findAll-->StuMgmtAction--findAll方法
org.apache.struts.actions.DispatchAction
分发处理类,功能:
可以将一组相关的业务操作,组织一个类中,便于维护和管理
编写流程:
1 web处理类
StuMgmtAction extends DispatchAction{
不能覆盖execute方法
必须提供多个和execute方法签名一样的,业务方法,方法名自定义
public ActionForward findAll(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
IStuMgmtBiz biz = new StuMgmtBizImpl();
Set<Student> stus = null;
try{
stus = biz.showAllStudents();
}catch(Exception e){
e.printStackTrace();
return mapping.findForward("errorPage");
}
req.setAttribute("stus", stus);
return mapping.findForward("showStusPage");
}
...
}
2 页面中需要指定一个特殊的参数,该参数的值是为了查找StuMgmtAction具体的处理方法
1)operate.do?methodName=addStu
2)<input type="hidden" name="methodName" value="addStu">
3 配置文件中,所有的请求只需要配置给同一个处理类
<action path="/operate" type="com.sinojava.stuMgmt.web.dispathAction.StuMgmtAction" parameter="methodName">
<forward name="showStusPage" path="/WEB-INF/page/showAll.jsp"></forward>
<forward name="errorPage" path="/WEB-INF/page/error.jsp"></forward>
<forward name="indexPage" path="/index.jsp"></forward>
</action>
============================================
paramter参数的值 <==> 页面中指定的特殊参数名
============================================