Struts复习 Struts的HTML标签

本文详细介绍了Struts框架中的表单标签用法,包括如何使用这些标签来创建各种输入控件,如文本框、密码框、单选按钮等,并展示了如何通过集合填充下拉列表及如何设置默认值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Ø         <html:form>

相当于HTML<form>

Ø         <html:text>

相当于HTML<input type=”text”>

Ø         <html:password>  

相当于HTML<input type=”password”>

Ø         <html:submit>

相当于HTML<input type=”submit”>

Ø         <html:reset>

相当于HTML<input type=”reset”>

-----------------------------------------------------------------------------------------------------------下面是重点:

Ø         <html:select>

相当于HTML<input type=”select”>

  1.     <html:form action="demo.do" method="post">
  2.         选择所喜欢的城市:
  3.             <html:select property="city">
  4.                 <html:option value="BJ">北京</html:option>
  5.                 <html:option value="TJ">天津</html:option>
  6.                 <html:option value="NJ">南京</html:option>
  7.                 <html:option value="GD">广东</html:option>
  8.                 <html:option value="DB">东北</html:option>
  9.             </html:select>
  10.             <br>
  11.             <html:submit value="提交"/> 
  12.             <html:reset value="重置"/>
  13. </html:form>

HTML中单选按钮一般有一个默认值checked

下拉列表中,struts提供了一个专卖从集合中取数据的标签<html:optionsCollection>

集合程序之中依然是Collection,但是必须在页面中加入org.apache.struts.util.LabelValueBean导入

 

  1.   <%
  2.     Collection col = new ArrayList() ;
  3.     col.add(new org.apache.struts.util.LabelValueBean("北京","BJ")) ;
  4.     col.add(new org.apache.struts.util.LabelValueBean("天津","TJ")) ;
  5.     col.add(new org.apache.struts.util.LabelValueBean("南京","NJ")) ;
  6.     col.add(new org.apache.struts.util.LabelValueBean("东北","DB")) ;
  7.     col.add(new org.apache.struts.util.LabelValueBean("广东","GD")) ;
  8.     // 将全部的集合放在一个属性范围之中
  9.     pageContext.setAttribute("ht",col) ;
  10.   %>
  11.     <html:form action="demo.do" method="post">
  12.         选择所喜欢的城市:
  13.             <html:select property="city">
  14.                 <html:optionsCollection name="ht"/>
  15.             </html:select>
  16.             <br>
  17.             <html:submit value="提交"/> 
  18.             <html:reset value="重置"/>
  19.     </html:form>

 

Ø         <html:radio>

相当于HTML<input type=”radio>”

  1.     <html:form action="demo.do" method="post">
  2.         性别:
  3.             <html:radio property="sex" value="男"/>男
  4.             <html:radio property="sex" value="女"/>女
  5.             <br>
  6.             <html:submit value="提交"/> 
  7.             <html:reset value="重置"/>
  8.     </html:form>

如果

struts的单选按钮中要加入默认值,就要改变相应的ActioForm,为其中属性加入默认值。

在对应的ActionForm中加入默认值设置:

 

private String sex="男";

 

Ø         <html:checkbox>

相当于HTML<input type=”checkbox”>

 

  1.     <html:form action="demo.do" method="post">
  2.         选择所爱吃的水果:
  3.             <html:checkbox property="fruit" value="orange"/>橘子
  4.             <html:checkbox property="fruit" value="banana"/>香蕉
  5.             <html:checkbox property="fruit" value="apple"/>苹果
  6.             <html:checkbox property="fruit" value="watermelon"/>西瓜
  7.             <html:checkbox property="fruit" value="cherry"/>樱桃
  8.             <br>
  9.             <html:submit value="提交"/> 
  10.             <html:reset value="重置"/>
  11.     </html:form>

要使用

checkbox的默认值,必须使用<html:multibox>标签

 

  1.     <html:form action="demo.do" method="post">
  2.         选择所爱吃的水果:
  3.             <logic:iterate id="fru" name="demoForm" property="fruit">
  4.                 <html:multibox property="select">${fru}</html:multibox>${fru}
  5.             </logic:iterate>
  6.             <br>
  7.             <html:submit value="提交"/> 
  8.             <html:reset value="重置"/>
  9.     </html:form>

而且在

ActionForm加入相应的getset方法

 private String fruit[] = { "orange", "apple", "cherry", "banana",
   "watermelon" };

//checkbox设置的3个默认值
 private String select[] = { "orange", "apple", "cherry" };

 public String[] getFruit() {
  return fruit;
 }

 public void setFruit(String[] fruit) {
  this.fruit = fruit;
 }

 public String[] getSelect() {
  return select;
 }

 public void setSelect(String[] select) {
  this.select = select;
 }

 

Ø         <html:textarea>

相当于HTML<input type=”texterea”>

 默认值设置

  1.     private String content ;
  2.     public String getContent() {
  3.         return content;
  4.     }
  5.     
  6.     public void setContent(String content) {
  7.         this.content = content;
  8.     }

页面

  1.     <html:form action="demo.do" method="post">
  2.         个人介绍:
  3.             <html:textarea property="content" rows="6" cols="30"/>
  4.             <br>
  5.             <html:submit value="提交"/> 
  6.             <html:reset value="重置"/>
  7.     </html:form
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值