首先要知道这些标签的property属性都要有相应的formbean里的属性值与之相对应,不然就无法通过验证,因为Struts在初始化html标签时是要检查formbean内是否存在此属性值的!
这些标签学习起来并不轻松,有好多种变化,要掌握全面,一定要有耐心!
下面把用到jsp页面的代码和formbean代码和properties文件代码贴到下面!慢慢看看,别着急
Jsp页面源代码:
<%@ page language="java" pageEncoding="UTF-8" import="com.html.struts.form.*,java.util.*"%>
<!--引入form包中的javabean和接下来可能要建立集合用到的java.util.*中的所有类-->
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<!--导入Struts的常用标签Struts1.2以后可以不在web.xml中声明,只要把uri路径写对就ok!-->
<html:html>
<head>
<title>html标签研究</title>
</head>
<body>
<html:form action="/login.do">
<table border="1" width="100%">
<tr>
<th align="left" width="20%">
<html:checkbox>
</th>
<th align="left" width="80%">
Struts code for example
</th>
</tr>
<tr>
<td align="left" width="20%">
Checkbox 1:
<html:checkbox property="checkbox1" value="true"></html:checkbox>
</td>
<td align="left" width="80%">
<html:checkbox property="checkbox1">
-Normal checkbox
</td>
</tr>
<tr>
<td align="left" width="20%">
Checkbox 2:
<html:checkbox property="checkbox2"></html:checkbox>
<!--checkbox用来最单选有点绕,不建议这么用-->
</td>
<td align="left" width="80%">
<html:checkbox property="checkbox2">
-Normal checkbox
</td>
</tr>
</table>
<table border="1" width="100%">
<tr>
<th align="left" width="20%">
<html:multibox>
</th>
<th align="left" width="80%">
Struts code fro example
</th>
</tr>
<tr>
<td align="left" width="20%">Multibox1:
<html:multibox property="strArray" value="Value1"></html:multibox>
</td>
<td align="left" width="80%">Multibox1:<html:multibox property="strArray" value="Value1"/>
</tr>
<tr>
<td align="left" width="20%">Multibox1:
<html:multibox property="strArray" value="Value2"></html:multibox>
<!--multibox为复选框注意上面两个的property后面都是strArray对应着frombean中的strArray这个数组变量提交以后将都被放入此数组中-->
</td>
<td align="left" width="80%">Multibox1:<html:multibox property="strArray" value="Value2"/>
</tr>
</table>
<table>
<tr>
<th align="left" width="20%">
<html:Radiobox/>
</th>
<th align="left" width="80%">
Struts code for example
</th>
</tr>
<tr>
<td align="left" width="20%">
<html:radio property="radioVal" value="RadioValue1">Radio1</html:radio>
</td>
<td align="left" width="80%">
<html:radio property="radioVal" value="RadioValue1"/>
</td>
</tr>
<tr>
<td align="left" width="20%">
<html:radio property="radioVal" value="RadioValue1">Radio1</html:radio>
<!--这个radio按钮,单选互斥按钮,当然property后边的名字一样才可以互斥,formbean中为这样的互斥按钮建立一个string变量即可-->
</td>
<td align="left" width="80%">
<html:radio property="radioVal" value="RadioValue1"/>
</td>
</tr>
</table>
<table>
<tr>
<%
Vector colorCollection = new Vector();
colorCollection.add(
new org.apache.struts.util.LabelValueBean("Pink","htmlselect.pink"));
colorCollection.add(
new org.apache.struts.util.LabelValueBean("Pink","htmlselect.pink"));
pageContext.setAttribute("colorCollection",colorCollection);
%>
<!--这个在页面上建立的集合colorCollection 将对应下面的select的options标签中的collection属性,options将调能够调用pageContext范围内的集合变量而一次性输出集合中的值-->
</tr>
<th align="left" width="20%">Select a customer:</th>
<th align="left" width="20%">Select some colors:</th>
<th align="left" width="50%">You last submitted:</th>
<tr>
<td align="left" width="20%">
<html:select property="custId">
<html:optionsCollection property="customers" label="name" value="custId" /></html:select>
<!--第一行property指定对应的formbean中的属性;第二行输出用到了optionsCollection来输出一组option元素,select内可以包含多个optionscollection,optionsCollection元素应该有一个name元素来对应特定的javabean的名字,此例中没有指定特定的name属性,那么就将使用关联的formbean,在下面的formbean代码中为其property属性指定了可选集合customers[],并在formbean的构造函数中为customers[]集合初始化,这里要多注意这种用法!另外Customerbean这个bean中只是定义了Customerbean这个类的一半形式,并不是和这个property不要弄混。-->
</td>
<td align="left" width="80%">
<html:select property="colors" size="6" multiple="true">
<!--select标签中的size控制多选列表的数量multiple为true为多选,默认为下拉列表。由于是多选列表property属性对应的formbean中的colors应为一个数组,这在下面formbean中的代码中将看到-->
<html:option value="htmlselect.orange">Orange</html:option>
<html:option value="htmlselect.purple">Purple</html:option>
<!--以上是普通option项加入多选列表中value对应实际值!中间为显示值!-->
<html:option value="htmlselect.red" bundle="htmlselect.Colors" key="htmlselect.red"/>
<html:option value="htmlselect.blue" bundle="htmlselect.Colors" key="htmlselect.blue"/>
<!--以上为调用properties中的key属性对应值,在struts-config.xml定义为“<message-resources parameter="HtmlSelectColors" key="htmlselect.Colors">”message-resources元素配置的Resourse Bundle的资源文件为 HtmlSelectColors.properties。文件内容“htmlselect.red=Red htmlselect.blue=Blue”-->
<html:options collection="colorCollection" property="value" labelProperty="label" />
<!--options元素输出一组option元素,collection属性指定存放列表项的集合对象,这个集合应该存放在page范围内,在本文开头可以找到我们定义的集合collorCollection。-->
</html:select></td>
</tr>
</table>
<html:submit>确定</html:submit>
<html:reset>重设</html:reset>
</html:form>
</body>
</html:html>
对应formbean文件代码:
(由各个属性名字即可判断出对应的jsp文件中的property,不再敖述!~^-^)
package com.html.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 11-22-2006
*
* XDoclet definition:
* @struts.form
*/
public class LoginForm extends ActionForm {
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public LoginForm() {
customers = new CustomerBean[3];
for (int i=0; i<3 ; i++ ) {
customers[i] = new CustomerBean();
customers[i].setCustId(i);
}
customers[0].setName("Tom");
customers[1].setName("Linda");
customers[2].setName("Jane");
}
private boolean checkbox1;
public boolean getCheckbox1(){
return this.checkbox1;
}
public void setCheckbox1(boolean checkbox1){
this.checkbox1=checkbox1;
}
private boolean checkbox2;
public boolean getCheckbox2(){
return this.checkbox2;
}
public void setCheckbox2(boolean checkbox2){
this.checkbox2=checkbox2;
}
private String strArray[] = new String[0];
public String[] getStrArray(){return this.strArray;}
public void setStrArray(String strArray[]){this.strArray = strArray;}
private String radioVal="";
public String getRadioVal() {
return radioVal;
}
public void setRadioVal(String radioVal) {
this.radioVal = radioVal;
}
private CustomerBean customers[];
public CustomerBean[] getCustomers() {
return customers;
}
public void setCustomers(CustomerBean[] customers) {
this.customers = customers;
}
private int custId;
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
private CustomerBean cust = new CustomerBean();
public CustomerBean getCust() {
return cust;
}
public void setCust(CustomerBean cust) {
this.cust = cust;
}
private String colors[];
public String[] getColors() {
return colors;
}
public void setColors(String[] colors) {
this.colors = colors;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
this.setCheckbox1(false);
this.setCheckbox2(false);
}
}
CustomerBean 代码如下:
package com.html.struts.form;
public class CustomerBean {
private int custId;
private String name;
private String[] favColors=new String[0];
public CustomerBean() {
}
public int getCustId(){
return this.custId;
}
public String getName(){
return this.name;
}
public String[] getFavColors(){
return this.favColors;
}
public void setCustId(int custId){
this.custId=custId;
}
public void setName(String name){
this.name=name;
}
public void setFavColors(String[] favColors){
this.favColors=favColors;
}
}