<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
通常是配合bean标签一起使用,是struts中最常用的标签之一。
对于struts表单乱码设置有两种方法:
一是通过设置过滤器
二是在XXXXForm中的reset()方法中设置:request.setCharacterEncoding("gbk");
★注意:在struts表单中,给单选按钮,复选框,下拉列表框等赋初始值,不能在标签中使用 checked关键字,要通过在XXXForm.java中设置相应初始值才行。
例如:一个性别单选按钮表单:
<html:radio property="sex" value="男">男</html:radio>
<html:radio property="sex" value="女">女</html:radio>
在XXXForm.java中是这样定义sex变量的:private String sex;
你要这样改成:private String sex = "男";
这时你再打开表单页面,男选项就自动默认选中了。
但是复选框就有点不一样了,因为复选框选中之后返回的是一个字符串数组,所以定义的时候就要定义成
数组:
private String fruit[];
表单内容为:
选择所爱吃的水果:
<html:checkbox property="fruit" value="orange"/>橘子
<html:checkbox property="fruit" value="banana"/>香蕉
<html:checkbox property="fruit" value="apple"/>苹果
<html:checkbox property="fruit" value="watermelon"/>西瓜
<html:checkbox property="fruit" value="cherry"/>樱桃
因为输出的结果是一个数组,所以这样输出:
for(int i=0;i<tag_htmlForm.getFruit().length;i++)
{
System.out.println("你爱吃的水果是:"+tag_htmlForm.getFruit()[i]);
}
那如何设置复选框的默认选项呢??在struts中这种情况要使用<html:multibox>标签并配合<logic:iterate>一起使用才行:
表单具体内容现在变为:
选择所爱吃的水果:
<logic:iterate id="fru" name="tag_htmlForm" property="fruit">
<html:multibox property="select">${fru}</html:multibox>${fru}
</logic:iterate>
那么上面的<html:multibox>又该如何设置呢?变量定义直接变为如下了:
private String sex ;
private String city ;
private String fruit[] = {"橘子","苹果","cherry","香蕉","watermelon"} ;
private String select[] = {"橘子","苹果","cherry"} ;
当然这时输出就得从tag_htmlForm.getSelect()中输出了:
for(int i=0;i<tag_htmlForm.getSelect().length;i++) {
System.out.println("你爱吃的水果是:"+tag_htmlForm.getSelect()[i]);
}
1.<html:form/>
作用:对html的form表单进行简单的封装,满足struts中表单请求的处理
<html:form action="/login">
</html:form>
如果你有上述一个标签,那么你的Struts配置文件的元素中必须有一个如下内容:
<action-mappings>
<action input="/login.jsp" name="loginForm" path="/login"
type="action.LoginAction" validate="true">
<forward name="success" path="/success.jsp" />
<forward name="error" path="/error.jsp" />
</action>
</action-mappings>
这就是说一个form标签是和form bean相关联的。
任何包含在<form>中用来接收用户输入的标签
(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)
必须在相关的form bean中有一个指定的属性值。<form>标签中method属性的缺省值是POST
2.<html:link>
作用:超文本连接
属性:page,指定一个页面的路径,必须以/开始。
Example:
当前页面跳转到/bean/parameter.jsp
需要提供参数:
<bean:define id="beanName" value="beanValue"></bean:define>
<html:link page="/bean/parameter.jsp"
paramId="action" paramName="beanName">
<html:param name="hobby" value="sports"></html:param>
跳转到页面bean:parameter.jsp
</html:link>
这等价于:
http://localhost:8080/strutsTaglib/bean/parameter.jsp?hobby=sports&action=beanValue
3.<html:image>与<html:img>
作用:在页面中产生图像的输出
最重要的属性page:图象文件的路径,前面必须带有一个斜线。
其它属性:height、width、alt。
Example:
<html:image page="/f4icmu.jpg" alt="软件工程之通俗版"></html:image>
<html:img page="/f4icmu.jpg"/>
4.<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。
5.<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>
6.<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)提供列表
<%
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("options", options);
%>
其中,
public class LabelValueBean {
private String label;
private String value;
}
2)提供多选标签
<html:select property="favors" multiple="true">
<html:options collection="options" property="value"
labelProperty="label" />
</html:select>
Example3:联系方式多选
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>