由于现在struts2的广泛使用,因此,先介绍struts2的相关标签
1.s:select
例子:
<s:form action="address!list.do" method="post" name="form1">
<TABLE border="0" width="100%" class="pager">
<TR>
<TD align="left">
<s:text name="pager.pageSize" />
<s:select name="pageSize" key="pager.pageSize"
list="pager.pageSizeIndexs" value="pager.pageSize"
onchange="document.all.pageNo.value='1';document.all.form1.submit();" />
</TD>
<TD align="center">
<s:text name="pager.rowCount" />
<s:property value="pager.rowCount" />
</TD>
<TD align="right">
<a
href="javascript:document.all.pageNo.value='<s:property value="pager.firstPageNo" />';document.all.form1.submit();"><s:text
name="pager.firstPageNo" /> </a>
<a
href="javascript:document.all.pageNo.value='<s:property value="pager.prePageNo" />';document.all.form1.submit();"><s:text
name="pager.prePageNo" /> </a>
<a
href="javascript:document.all.pageNo.value='<s:property value="pager.nextPageNo" />';document.all.form1.submit();"><s:text
name="pager.nextPageNo" /> </a>
<a
href="javascript:document.all.pageNo.value='<s:property value="pager.lastPageNo" />';document.all.form1.submit();"><s:text
name="pager.lastPageNo" /> </a>
<s:select name="pageNo" list="pager.pageNoIndexs"
value="pager.pageNo" onchange="document.all.form1.submit();" />
</TD>
<TD width="20"></TD>
</TR>
</TABLE>
</s:form>
其中:
<s:select name="pageSize" key="pager.pageSize"
list="pager.pageSizeIndexs" value="pager.pageSize"
onchange="document.all.pageNo.value='1';document.all.form1.submit();" />
其中上述代码的意思等同于如下语句:
<select name="pageSize">
<option value="pager.pageSize1">pager.pageSize1</option>
<option value="pager.pageSize2">pager.pageSize2</option>
<option value="pager.pageSize3">pager.pageSize3</option>
<option value="pager.pageSize4">pager.pageSize4</option>
</select>
其中:name对应的属性值要为跳转这个标签所在页面(当前jsp页面)的action中的一个属性,而且有setter/getter方法,key="pager.pageSize"表示跳转到这个标签(s:select)(如果点击某一页面上的一个连接,这个连接为address.do?method=init,点击之后就跳转到s:select所在的页面,则address对应的action就为这里所指的action)(即为struts.xml文件中address对应的action类)中有或父类中有pager属性,并且有getter和setter方法,而这个pager属性所属对象所对应的类中要有pageSize属性,而且要有getter和setter方法。list="pager.pageSizeIndexs"表示跳转到这个页面的action中有getPager方法,并且这个pager对象的类有getPageSizeIndexs方法,这个方法返回一个list/set/collection或其他容器
2.s:iterator
例子:
<s:iterator value="pager.resultList">
<TR>
<TD>
<s:property value="name" />
</TD>
<TD>
<s:property value="sex" />
</TD>
<TD>
<s:property value="mobile" />
</TD>
<TD>
<s:property value="email" />
</TD>
<TD>
<s:property value="qq" />
</TD>
<TD>
<s:property value="company" />
</TD>
<TD>
<s:property value="address" />
</TD>
<TD>
<s:property value="postcode" />
</TD>
<TD>
<a href='address!edit.do?id=<s:property value="id"/>'><s:text
name="button.edit" /> </a>
<a href="address!delete.do?id=<s:property value="id"/>"><s:text
name="button.delete" /> </a>
</TD>
</TR>
</s:iterator>
这里的value属性值的解释:跳转到这个jsp页面的那个action(1中已经提到)中要有getPager方法,返回的值为一个类的实例,这个类中要有getreslutList方法,且返回的必须是一个容器类(list、set等类)
3.s:property
例子:
<s:iterator value="pager.resultList">
<TR>
<TD>
<s:property value="name" />
</TD>
<TD>
<s:property value="sex" />
</TD>
<TD>
<s:property value="mobile" />
</TD>
<TD>
<s:property value="email" />
</TD>
<TD>
<s:property value="qq" />
</TD>
<TD>
<s:property value="company" />
</TD>
<TD>
<s:property value="address" />
</TD>
<TD>
<s:property value="postcode" />
</TD>
<TD>
<a href='address!edit.do?id=<s:property value="id"/>'><s:text
name="button.edit" /> </a>
<a href="address!delete.do?id=<s:property value="id"/>"><s:text
name="button.delete" /> </a>
</TD>
</TR>
</s:iterator>
这里使用了迭代器(iterator)来循环取出 value="pager.resultList"返回的一个容器类的实例中的结果,这个结果是一个对象,且这个对象有很多属性,假设返回的是一个Address对象,这个Address对象中有很多属性和方法
<s:property value="address" />
这就表示从这个Address对象中取出address方法,即调用这个对象的getAddress()方法,并将取到的值显示在这里
4.s:actionerror
例子:
<s:actionerror />
输入action中保存的全局错误,action中保存的全局错误代码为:
saveActionError("address.message.edit.notexist");
如果一个jsp文件中有s:actionerror标签,则跳转到这个jsp文件的action的相应函数中一定使用了saveActionError方法
5.s:actionmessage
例子:
<s:actionmessage />
对应的action中的相应方法的相应语句为:
saveActionMessage("address.message.delete.success");
解释同上
6.s:fielderror
显示错误信息
例子:
<s:textfield name="name" maxlength="50" />
<s:fielderror>
<s:param>name</s:param>
</s:fielderror>
对应的action中validate方法中对应代码为:
if (name == null || name.equals("")) {
addFieldError("name", getText("address.error.name"));
}
标签中的<s:param>name</s:param>的参数name对应于方法addFieldError方法的第一个参数name
struts1 标签解释
1、bean:message
例子:
<bean:message key="address.page.position" />
其中address.page.position为资源文件中的一个key所对应的属性值
2、html:errors
例子:
<html:errors property="org.apache.struts.action.GLOBAL_MESSAGE" />
对应的java代码如下:
ActionMessages messages = new ActionMessages();
AddressForm addressForm = (AddressForm) form;
String username = getUsername(request);
// check if exists
boolean b = addressDAO.isExist(username, addressForm.getName());
if (!b) {
// insert object
Address address = new Address();
address.setUsername(username);
address.setName(addressForm.getName());
address.setSex(addressForm.getSex());
address.setMobile(addressForm.getMobile());
address.setEmail(addressForm.getEmail());
address.setQq(addressForm.getQq());
address.setCompany(addressForm.getCompany());
address.setAddress(addressForm.getAddress());
address.setPostcode(addressForm.getPostcode());
addressDAO.insert(address);
// save messages
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"address.message.add.success"));
3、logic:present
用法:<logic:present name="user">相当于if(request.getAttribute("user"!=null))
例子:
<logic:present name="addressList">
<logic:iterate id="address" name="addressList" scope="request">
<TR>
<TD>
<bean:write name="address" property="name" scope="page" />
</TD>
<TD>
<bean:write name="address" property="sex" scope="page" />
</TD>
<TD>
<bean:write name="address" property="mobile" scope="page" />
</TD>
<TD>
<bean:write name="address" property="email" scope="page" />
</TD>
<TD>
<bean:write name="address" property="qq" scope="page" />
</TD>
<TD>
<bean:write name="address" property="company" scope="page" />
</TD>
<TD>
<bean:write name="address" property="address" scope="page" />
</TD>
<TD>
<bean:write name="address" property="postcode" scope="page" />
</TD>
<TD>
<a
href='address_edit.do?method=edit&id=<bean:write name="address" property="id" scope="page"/>'><bean:message
key="button.edit" /> </a>
<a
href="address.do?method=delete&id=<bean:write name="address" property="id" scope="page"/>"><bean:message
key="button.delete" /> </a>
</TD>
</TR>
</logic:iterate>
</logic:present>
4、logic:iterate
例子:
<logic:present name="addressList">
<logic:iterate id="address" name="addressList" scope="request">
<TR>
<TD>
<bean:write name="address" property="name" scope="page" />
</TD>
<TD>
<bean:write name="address" property="sex" scope="page" />
</TD>
<TD>
<bean:write name="address" property="mobile" scope="page" />
</TD>
<TD>
<bean:write name="address" property="email" scope="page" />
</TD>
<TD>
<bean:write name="address" property="qq" scope="page" />
</TD>
<TD>
<bean:write name="address" property="company" scope="page" />
</TD>
<TD>
<bean:write name="address" property="address" scope="page" />
</TD>
<TD>
<bean:write name="address" property="postcode" scope="page" />
</TD>
<TD>
<a
href='address_edit.do?method=edit&id=<bean:write name="address" property="id" scope="page"/>'><bean:message
key="button.edit" /> </a>
<a
href="address.do?method=delete&id=<bean:write name="address" property="id" scope="page"/>"><bean:message
key="button.delete" /> </a>
</TD>
</TR>
</logic:iterate>
</logic:present>