📄 14. struts1.x note.txt
字号:
header="messages.header" footer="messages.footer">
<tr>
<td></td>
<td><bean:write name="msg" /></td>
</tr>
</html:messages>
4.<html:image>与<html:img>
作用:在页面中产生图像的输出
最重要的属性page:图象文件的路径,前面必须带有一个斜线。
其它属性:height、width(只有 img 可以设置长和宽)、alt(不知何用)。
Example:
<html:image page="/f4icmu.jpg" alt="软件工程之通俗版"></html:image>
<html:img page="/f4icmu.jpg"/> 等价于: <input type="image" name="" src="/工程名/f4icmu.jpg" alt="软件工程详解"> <img src="/工程名/f4icmu.jpg">
5.<html:checkbox/>
生成一个checkbox。这里的value值可以是true,yes或on。
checkboxForm的属性:
private boolean one = false;
private boolean two = false;
private boolean three = false;
<html:checkbox name="checkboxForm" property="one">One</html:checkbox>
<html:checkbox name="checkboxForm" property="two">Two</html:checkbox>
<html:checkbox name="checkboxForm" property="three">Three</html:checkbox>
如果选中后被提交则相应的属性的值为true。
6.<html:radio/>
生成一个radio。主要的用法有两种。
下面的代码示例了html:radio标签的一般用法,
如果被提交则选中的radio的value值将被提交到radioForm中的id中。
<html:radio name="radioForm" property="id" value="00001"> One </html:radio>
<html:radio name="radioForm" property="id" value="00002"> Two </html:radio>
7.<html:select>标签和<html:option>标签
作用:对html中的下拉选择标签与选项标签做了封装
单选下拉
Example1:
<html:select property="singleSelect" size="3">
<html:option value="Single 0">Single 0</html:option>
<html:option value="Single 1">Single 1</html:option>
<html:option value="Single 2">Single 2</html:option>
</html:select>
Example2:性别
1)提供label和value的数组
<% String[] label = { "男", "女", "未知" };
String[] value = { "male", "female", "unknown" };
pageContext.setAttribute("label", label);
pageContext.setAttribute("value", value);
%>
2)标签使用:
<html:select property="sex" size="1">
<html:options labelName="label" name="value" />
</html:select>
Example3:兴趣爱好多选
1)提供列表
<% List options = new ArrayList();
options.add(new LabelValueBean("电脑游戏", "PCGame"));
options.add(new LabelValueBean("看电视", "TV"));
options.add(new LabelValueBean("阅读", "Reading"));
options.add(new LabelValueBean("唱歌", "Singing"));
pageContext.setAttribute("opt", options);
%>
其中,
public class LabelValueBean {
private String label;
private String value; public LabelValueBean(String label, String value){}
}
2)提供多选标签
<html:select property="favors" multiple="true">
<html:options collection="opt" property="value"
labelProperty="label" />
</html:select>
Example4:联系方式多选
1)提供多选列表
<% List myPhones=new ArrayList();
myPhones.add(new LabelValueBean("小灵通","33213322"));
myPhones.add(new LabelValueBean("固话","80512010"));
myPhones.add(new LabelValueBean("手机","13711221113"));
pageContext.setAttribute("myPhones",myPhones);
%>
2)标签的使用
<html:select property="phones" multiple="true">
<html:optionsCollection name="myPhones"/>
</html:select>
8.<html:submit>标签
<html:submit value="Submit" />
9.<hmtl:text>标签
文本输入框
<html:text property="username" />
10.<html:password>标签
<html:password property="password"/>
11.<html:cancel/>取消标签
四、Logic标记 :
该标签库包含的标签可以用来进行逻辑判断、集合迭代和流程控制。
1.<logic:iterate/>
作用:<logic:iterate/>标签用来迭代集合
Example:
<%
List stuList=new ArrayList();
Student stu=new Student();
stu.setName("Alice");
...
stuList.add(stu);
request.setAttribute("stuList",stuList);
%>
<logic:iterate id="stu" name="stuList">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.sex }</td>
<td>${stu.age }</td>
<td>${stu.desc }</td>
</tr>
</logic:iterate>
(2) logic:empty
用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。该标签用于以下情况:
当Java对象为null时
当String对象为""时
当java.util.Collection对象中的isEmpty()返回true时
当java.util.Map对象中的isEmpty()返回true时
下面的代码示例了logic:empty标签判断集合persons是否为空:
<logic:empty name="stu" property = "books">
<div>集合books为空!</div> </logic:empty>
logic:notEmpty标签的应用正好和logic:empty标签相反。
(3) logic:equal
这里要介绍的不只是logic:equal(=)标签,而是要介绍一类标签,这类标签完成比较运算,
包括:
logic:equal(=)
logic:notEqual(!=)
logic:greaterEqual(>=)
logic:lessEqual(<=)
logic:graterThan(>)
logic:lessThan(<)
Example:
<logic:equal name="stu" property="name" value="Alice">
I am Alice.
</logic:equal>
<bean:define id="count" value="168"/>
<logic:equal name="count" value="168">
Good lucky number.
</logic:equal>Day3:1 定制Action2 禁止表单重复提交3 定制Controller4 验证框架************************************************************************1 定制Action1) DispatchAction:可以将相关的一组操作放在一个Action类中(同一模块功能) 特点(优点) : 1)一个 Action可以对应多个业务方法(CRUD),而无需通过增加隐藏域的方式来处理。 2)避免Action类数量随业务复杂度而膨胀,可以共享公共的业务逻辑代码。 3)无需重写execute方法 4)DispatchAction类本身已经重写了Action类的execute方法。 实现DispatchAction的步骤: 1) 继承DispatchAction SuperGileAction extends DispatchAction 2) 定义自己的处理方法,不要覆盖execute()方法 3) struts-config.xml中action元素增加parameter属性 <action path="/super" name="superForm" parameter="method" type="com.tarena.action.SuperGileAction"> <forward name="success" path="/success.jsp"></forward> </action> 用户请求URL应有如下格式: a)Get方法 http://localhost:8080/super/SuperGileAction.do?method=<method_name> b) Post方法 在表单中添加参数来完成2) LookupDispatchAction: 特点:它是DispatchAction的子类,所以具备DispatchAction的所有特性, 支持一个表单对应多个业务方法。 实现LookupDispatchAction的步骤: 1) 继承LookupDispatchAction 2) 定义自己的处理方法,覆盖getKeyMethodMap()方法,不要覆盖execute()方法 getKeyMethodMap()完成资源文件中Key与Action中方法映射 @Override protected Map getKeyMethodMap() { Map map = new HashMap(); map.put("submit.modify", "modify"); map.put("submit.add", "register"); return map; } public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ ... } public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ ... } 3)struts-config.xml中为Action增加parameter属性 <action path="/lookupAction" name="superForm" type="com.tarena.action.SuperGirlLookupAction" parameter="method"> <forward name="success" path="/success.jsp"></forward> </action> 4) 提交页面按钮有如下格式 html:submit的property属性值与Action中的parameter属性值映射 <html:submit property="method"> <bean:message key="submit.add"/> </html:submit> <html:submit property="method"> <bean:message key="submit.modify"/> </html:submit>3) Action编程技巧(建议) : 1)尽量避免使用实例变量或静态变量 (servlet是单实例多线程,所以要自己维护成员变量的同步问题) 2)使用自己的BaseAction完成Action的公共操作,其余Action可从BaseAction派生 3)Action不要代替Model工作 在设计多层架构时,使用Business Delegate可降低层与层之间的耦合度2 禁止表单重复提交 1)客户端实现(JavaScript) 2)JSP实现(脚本) 图解 实现代码 3)Struts实现 (1)Struts内置实现,无需额外配置 (2)需要在前一个action的方法中调用saveToken()方法 saveToken(request); (3)在后一个执行业务逻辑的方法中使用isTokenValid(request)判断表单是否被重复提交 if (isTokenValid(request)) { // invoke business method //...... // reset transaction token after transaction success! this.resetToken(request); return mapping.findForward("success"); } else { System.out.println("duplicate token"); return mapping.findForward("error"); }3 定制Controller(可选) 扩展控制器功能 ActionServlet与RequestProcessor的作用 1)继承RequestProcessor 2)通过processPreprocess()增加定制功能 //自定义控制器需要派生自RequestProcessor package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.RequestProcessor; public class SecurityRequestProcessor extends RequestProcessor { @Override protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) { System.out.println("start controller ..."); request.getSession().getServletContext().log("URL="+request.getRequestURL());//写日志 String ip = request.getRemoteAddr(); if (ip.equals("127.0.0.1")) { System.out.println("valid client: 127.0.0.1"); return true; //只允许此IP登录 } int lastPoint = ip.lastIndexOf("."); String fourth = ip.substring(lastPoint + 1); int fourthInt = Integer.parseInt(fourth); if (ip.startsWith("192.168.1.") && fourthInt > 50 && fourthInt < 60) { System.out.println("invalid client: 192.168.1.[50~60]"); return false; //限制黑名单IP的登录 } return false; }} 3)struts-config.xml中配置 <controller .../> (一般只有一个controller) 写在<action-mappings /> 之后;<message-resources .../> 之前 <controller processorClass="com.controller.SecurityRequestProcessor"/> 补充说明: 通常的一个struts-config.xml只能配置一个controller 一般地,在开发时通过定制controller来增强系统功能,例如:日志、安全限制等。 但在实际的应用中比较少使用。 举例: public class SecurityRequestProcessor extends RequestProcessor { public boolean processPreprocess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response){ String ip=request.getRemoteAddr(); if(ip.equals("127.0.0.1")) return true; int lastPoint = ip.lastIndexOf("."); String fourth=ip.substring(lastPoint+1); int fourthInt=Integer.parseInt(fourth); if(ip.startsWith("192.168.1.")&&fourthInt>50&&fourthInt<60){ return true; } return false; } }4 验证(校验) 框架 (1)有三种数据验证方式: a.在ActionForm.validate() 当中进行 b.在Action.execute()方法中进行(这两种参看day2的异常处理,前两种) c.引入validator框架(apache.jakata) (2)声明式的数据验证(为什么能使用声明式?)--配置 原因:对于数据的验证有模式 (3)validator框架已经定义了一部分通用的验证规则和逻辑 开发者只需对特定数据指定具体的验证规则(业务)--使用 包含在validation.xml 使用验证框架的步骤: 1)在struts-config.xml配置校验插件(validate plugin) 放在<message-resources ../> 之后 <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> 注释: 1.验证插件: 1) validator-rules.xml :定义验证规则 规则名(如:required, maxlength.....) 验证方法 验证失败时出错提示(放到上下文中) 2)validation.xml 使用规则:对指定的表单属性进行校验 2.基本的结构: 1)验证逻辑: validateXxx()方法 2)在validator-rule.xml中定义的都是校验规则 3)在validation.xml中(声明式验证) a.引入了struts-config.xml的表单名字(form-bean的名字) b.声明域的验证依赖规则 depends = "required,Integer" 2)add validator-rule.xml and validation.xml to /WEB-INF/ copy validator-rule.xml from struts framework edit validation.xml 注意版本的一致<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -