struts标签

<html:html>标签
属性的作用:
lang: 值为true时,就根据存储在HttpSession中的Locale对象来输出网页使用的语言。如果不存在session或session中没有Locale对象, 就以Http请求头中的Accept-language属性来设置输出语言。如果请求头中没有Accept-language,就使用默认的Locale来输出语言
示例:
<html:html lang="true"/>

<html:form>标签:生成HTML<form action="">元素
属性的作用:
method:表单提交的方法
action:指定表单提交后,处理该请求的Action组件名称
示例:
<html:form method="POST" action="/index.do">

<html:text>标签:生成一个HTML<input type="text"/>
属性的作用:
property:与ActionFormBean中的属性名相对应,表单提交时会value属性中的值赋给相应的ActionFormBean中的属性。(REQUIRED)
value: 指定文本框的默认值。
name:ActionForm的名称,或其他javabean的名称,用来给该控件提供数据。如果没有指定,那么将使用form标签中相应的ActionForm Bean
示例:
<html:text property="name"/>

<html:password>标签:生成一个HTML<input type="password"/>
属性的作用:
property:与ActionFormBean中的属性名相对应,表单提交时会将value属性中的值赋给相应的ActionFormBean中的属性(REQUIRED)
value: 指定密码文本框的默认值。
name:ActionForm的名称,或其他javabean的名称,用来给该控件提供数据。如果没有指定,那么将使用form标签中相应的ActionForm Bean
redisplay:取值为true或false。在密码框中填入内容后,从新刷新(请求)该页面是否仍保留已经填写过的密码。推荐选择false
示例:
<html:password redisplay="false" property="password"/>
<html:radio>标签:生成一HTML <input type="radio"/>
属性的作用:
property:与ActionFormBean中的属性名相对应,表单提交时会将value属性的中值赋给相应的ActionFormBean中的属性(REQUIRED)
value: 指定单选按钮的默认值。(REQUIRED)
name: ActionForm的名称,或其他javabean的名称,用来给该控件提供数据。如果没有指定,那么将使用form标签中相应的ActionForm Bean
bundle: 指定特定的Resource Bundle,它与struts-config.xml中
<message-resources key=""/>元素中的key中的值对应
示例:
<html:radio value="boy" property="sex">boy</html:radio>
<html:radio value="girl" property="sex">girl</html:radio>
<html:radio>使用了同一个property,表示它们是一个组,只能从其中任选 一个。其对应的ActionForm Bean中的属性为sex,sex中的值为被选中的 单选按钮中的值,如果没有一个单选按钮被选中,那么sex中的值为null

<html:multibox>标签:生成一个HTML <input type="checkbox"/>
属性的作用:
property:与ActionFormBean中的属性名相对应,表单提交时会将value属性中的值赋给相应的ActionFormBean中的属性。(REQUIRED)
value: 指定复选框的默认值。
name: ActionForm的名称,或其他javabean的名称,用来给该控件提供数据。如果没有指定,那么将使用form标签中相应的 ActionForm Bean
bundle: 指定特定的Resource Bundle,它与struts-config.xml中
<message-resources key=""/>元素中的key中的值对应
示例:
<html:multibox property="favorite" value="movie" />
<html:multibox property="favorite" value="Video Game" />
<html:multibox property="favorite" value="BasketBall" />
相对应的ActionBean Form中的属性及相应的getter与setter方法如下:

private String favorite[]=new String[0];
public void setFavorite(String favorite[])
{
this.favorite = favorite;
}
public String[] getFavorite()
{
return favorite;
}
表单提交时(如果全部选中),会将value属性中的值保存至数组中,那么 favorite数组中的内 容为{"movie","Video Game","BasketBall"}。如果 "Video Game" 没有被选中,那么提交后favorite数组中的内容为:
{"movie","BasketBall"}

<html:select>标签:生成一个HTML<select name="select" multiple="">
语法格式:
<html:select property="country" multiple=“”size=“” >
<html:option value="Canda"/>
<html:option value="China" />
<html:option value="American" />
</html:select>
属性的作用:
property:与ActionFormBean中的属性名相对应,表单提交时会将value属性中的值赋给相应的ActionFormBean中的属性。(REQUIRED)
multiple: 指定是否多选,如果设置为true,就表示多选列表,支持多项选择;如果设置为false,则表示下拉列表,支持单项选择。默认为 false。
name: ActionForm的名称,或其他javabean的名称,用来给该控件提供数据。如果没有指定,那么将使用form标签中相应的 ActionForm Bean
size: 指定每次在网页上可以显示的选项的数目
示例:
<html:select property="country" multiple="false">
其所对应ActioFrom Bean中的 String country; 属性
<html:select property="skill" multiple="true" size="3">
其所对应ActioFrom Bean中的属性及getter和setter方法如下:
private String skill[];
public String[] getSkill()
{
return skill;
}
public void setSkill(String[] skill)
{
this.skill = skill;
}
<html:select>不单独使用,要指定<html:select>中选项的值必须使用 <html:option> 作为其子元素。下面介绍<html:option>的用法
<html:option>属性的作用:
value: 指定该组件在网页中显示的内容
bundle:指定特定的Resource Bundle,它与struts-config.xml中
<message-resources key=""/>元素中的key中的值对应
key: <message-resources>元素中配置的Resource Bundle文件中消息key相对应
示例:
1、直接赋值
<html:option value="SQLServer"/>
2、采用Resource Bundle文件绑定
struts-config.xml中配置如下
<message-resources
key="myresource"
parameter="com.accp.struts.ApplicationResources"
/>
ApplicationResources文件内容如下:
form.skill=SQL Server
<html:option value="form.skill"
bundle="myresource"
key="form.skill"/>

<html:link>标签:生成一个超链接
属性的作用:
page:要链接页面的相对路径,一般用于链接同一应用程序中的页面
href:要链接页面的完全路径,一般用于链接不同应用程序中的页面
forward:将请求转发给struts-config.xml中所配置的<global-forwards>元素下的<forward>,再由其转发给要链接的页面
action:将请求转发给任意一个Action
示例:
<html:link page="/other.jsp">
<html:link href="/MyHtml/other.jsp">
<html:link forward="other">
与其相对应的是
<global-forwards>
<forward name="other" path="/other.jsp"></forward>
</global-forwards>
<html:link action="/other.do">
与其相对应的是:
<action-mappings>
<action forward="/other.jsp" path="/other"/>
</action-mappings>

<html:errors>输出保存在ActionMessages中的错误消息
属性的作用:
name:指定ActionMessages对象存放在request,session中的key,如果未指定。默认为Globals.ERROR_KEY.(一般不要指定)
property:指定消息的属性。如果为设置,将显示ActionMessages中的所有信息。
bundle: 指定Resource Bundle。如果没有设置,则使用默认的Resource Bundle
使用步骤:
1) 创建资源文件 ApplicationResources.properties,向其中指定一些预定义信息
errors.error.one=<font color/="red">this is a
ActionErrors's message</font><br/>
errors.error.three=<font color/="blue">the third
ActionErrors's message</font><br/>
errors.error.two=<font color/="green">the second ActionErrors's message</font><br/>
注:<html:errors>可以自动识别html元素
ApplicationResources.properties文件在struts-config.xml中按如下方式配置
<message-resources
key="form" --注意:这里指定了该文件的key
parameter="com.accp.resource.ApplicationResources"
/>
2) 在ActionForm Bean中的validate()方法中加入如下代码:
ActionErrors errors= new ActionErrors();
errors.add(Globals.ERROR_KEY,
new ActionMessage("errors.error.one"));
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.error.two"));
errors.add("threeError",
new ActionMessage("errors.error.three"));
注意:
(a) 三个add()方法中第一个参数不同,Globals.ERROR_KEY与 ActionMessages.GLOBAL_MESSAGE都是系统默认的常量, 其功能一样。"threeError"为自定义值。
(b) 以上代码也可以写入Action类中的execute()方法中,但是 后面要加上一下代码
this.addErrors(request, errors);
3) 在网页中显示<html:errors>中的内容
a) 显示所有信息:
<html:errors bundle="form" />
注意:<html:errors>中的bundle属性中的值"form" 与struts- config.xml 文件中
<message-resources
key="form"
parameter="com.accp.resource.ApplicationResources"
/>
中的key="form"相匹配
如果struts-config.xml文件中<message-resources>并没有指定key属性,那么<html:errors>中的bundle属性就可以省略
即:
<message-resources
parameter="com.accp.resource.ApplicationResources"
/>
<html:errors/>
<html:errors />
b) 显示特定信息:
<html:errors property="threeError" bundle="form" />
注意:<html:errors>中的property属性中的值"threeError"与前面步骤(2)中的代码
errors.add("threeError",
new ActionMessage("errors.error.three"));
中的"threeError"相同。这样可以输出想要显示的信息

<html:messages>输出保存在ActionMessages中的错误消息
语法格式:
<html:messages id="message" >
<bean:write name="message"/>
</html:messages>
属性的作用:
name:指定ActionMessages对象存放在request,session中的key, 如果未指定。默认为Globals.MESSAGE_KEY.(一般不要指定)
id: 为从消息中检索出来的ActionMessages对象命名。该名称要和
<bean:write name="message"/>中name属性的值相同。
bundle: 指定Resource Bundle。如果没有设置,则使用默认的 Resource Bundle
message:指定消息的来源,默认值是true。如果为true,就从request,
session范围内检索key为Globals.MESSAGE_KEY的ActionMessages对象,此时name属性无效。如果为false,那么就根据name属性中的值来检索ActionMessages对象,此时若没有设置name属性,将采用默认值Globals.ERROR_KEY。
filter:是否过滤资源文件中的html标记。false为不过滤,true为过滤。
默认值为true。
注:<html:errors>不可以自动识别html元素,它会将html元素当作普 通字符。
使用步骤:
(1) 创建资源文件 ApplicationResources.properties,向其中指定一 些预定义信息
errors.message.one=<font color/="red">this is a
ActionMessage's message</font><br/>
errors.message.three=<font color/="blue">the third
ActionMessage's message</font><br/>
errors.message.two=<font color/="green">the second
ActionMessage's message </font>
ApplicationResources.properties文件在struts-config.xml中按 如下方式配置
<message-resources
key="form" --注意:这里指定了该文件的key
parameter="com.accp.resource.ApplicationResources"
/>
(2) 在Action类的execute()方法中加入如下代码:
ActionMessages message= new ActionMessages();
message.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.message.one"));
message.add(Globals.MESSAGE_KEY,
new ActionMessage("errors.message.two"));
message.add("threeMessage",
new ActionMessage("errors.message.three"));
this.saveMessages(request, message);
注意:
(a) 三个add()方法中第一个参数不同,Globals.ERROR_KEY与 ActionMessages.GLOBAL_MESSAGE都是系统默认的常量,其 功能一样。"threeMessage"为自定义值。
(b) 以上代码不可以在ActionForm Bean类中的validate()方法中使用。
(3) 在网页中显示<html:messages>中的内容
a) 显示所有信息:
<html:messages id="message" bundle="form">
<bean:write name="message"filter="false"/>
</html:messages>
注意:<html:messages >中的bundle属性中的值"form" 与 struts- config.xml 文件中
<message-resources
key="form"
parameter="com.accp.resource.ApplicationResources"
/>
中的key="form"相匹配
如果struts-config.xml文件中<message-resources>并没有指定key属性,那么<html:errors>中的bundle属性就可以省略
即:
<message-resources
parameter="com.accp.resource.ApplicationResources"
/>
<html:messages id="message" >
<bean:write name="message"filter="false"/>
</html:messages>
b) 显示特定信息:
<html:messages id="message" bundle="form"
property="threeMessage">
<bean:write name="message"/>
</html:messages>
注意:<html:messages>中的property属性中的值 "threeMessage"与 前面步骤(2)中的代码
message.add("threeMessage",
new ActionMessage("errors.message.three"));
中的"threeMessage"。
这样可以输出想要显示的信息(实际上在这里不会输出任何内容,但是若和validator验证框架一起使用便可以看到效果,这种使用方法,可以参考validator验证框架中的例子)

<bean:message>输出资源文件中的信息
属性的作用:
bundle: 指定Resource Bundle。如果没有设置,则使用默认的 Resource Bundle
key:指定Resource Bundle中的key
arg0,arg1,arg2,arg3:Resource Bundle中资源文件内容的占位符
使用步骤:
(1) 创建资源文件 ApplicationResources.properties,向其中指定一 些预定义信息
message.info=<font color/="red">this is a message form resource bundle</font><br>
message.info.args=<font color/="red">hello {0},
welcome to {1}</font><br>
ApplicationResources.properties文件在struts-config.xml中按如下方式配置
<message-resources
key="form" --注意:这里指定了该文件的key
parameter="com.accp.resource.ApplicationResources"
/>
(2) 在网页中显示<bean:messages>中的内容
(a) <bean:message bundle="form" key="message.info" />
注意:<bean:messages >中的bundle属性中的值"form"与
struts- config.xml 文件中
<message-resources
key="form"
parameter="com.accp.resource.ApplicationResources"
/>
中的key="form"相匹配
如果struts-config.xml文件中<message-resources>并没有指定key属性,那么<bean:message>中的bundle属性就可以省 略,即:
<message-resources
parameter="com.accp.resource.ApplicationResources"
/>
<bean:message key="message.info" />
(b) <bean:message bundle="form"
key="message.info.args"
arg0="jack" arg1="xfaccp"/>
其中:arg0,arg1分别对应资源文件
message.info.args=<font color/="red">hello {0},
welcome to {1}</font><br>
中的{0},{1}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值