常用struts标签使用举例--html篇,转载

1. html:base

同html的base元素。

2. html:cancel

该标签生成一个取消按钮。当点击该按钮后action servlet会绕过相应的form bean的validate()方法,同时将控制权交给相应的action。在action中可使用Action.isCancelled(HttpServletRequest)方法判断是否被取消了。如果返回true表示这个action被取消了,否则表示这个action没有被取消。

eg. <html:cancel>取消</html:cancel>

3.html:form

1) 标签中必须包含一个action属性,它是这个标签中唯一必需的属性。如果不具备该属性则JSP页面会抛出一个异常。之后你必须给这个action属性指定一个有效值。一个有效值是指应用程序的Struts配置文件中元素里的任何一个子元素的访问路径。而且相应的元素中必须有一个name属性,它的值是form bean的名称

<html:form action=\"/login\" >

如果你有上述一个标签 ,那么你的Struts配置文件的元素中必须有一个如下显示为粗体的元素:

<action-mappings>

<action path=\"/login\"

type=\"com.javapro.struts.LoginAction\"

name=\"loginForm\"

scope=\"request\"

input=\"/login.jsp\">

<forward name=\"success\" path=\"/mainMenu.jsp\"/>

</action>

</action-mappings> // 这就是说一个form标签是和form bean相关联的

2) 任何包含在<form>中用来接收用户输入的标签(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)必须在相关的form bean中有一个指定的属性值。比如,如果你有一个属性值被指定为“username”的<text>标签,那么相关的form bean中也必须有一个名为“username”的属性。输入<text>标签中的值会被用于生成form bean的userName属性。

4.html:select标签

该标签生成一个select元素。multiple属性决定是否为多选。如果指定了multiple="true"则为多选,此时对应的属性应该是一个数组。否则,此时对应的属性应该是标量。

注意:为了正确的处理未作选择的情况,在ActionForm中的reset()方法中必须将标量属性设置为默认值而将数组的长度置为0。

另外的一个重要问题就是struts如何生成option元素了,这个任务struts交给了html:option、html:options和html:optionsCollection三个标签。

1)html:option

该标签生成一个HTML的option元素。该标签必须嵌在html:select标签中。它的显示文本来自其标签体,也可以来自于资源文件。

 eg. <html:option value="red">red</html:option><html:option value="blue">blue</html:option>

2)html:options

该标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。

指定collection属性的方式举例如下:

<html:select name="selectForm" property="orgId" size="1">    


<html:options collection="orgCollection" property="orgId" labelProperty="orgName"/>


</html:select>

未指定collection属性方式的举例如下:

<html:select name="selectForm" property="orgId" size="1">     


<html:options property="orgIds" labelProperty="orgNames"/>


</html:select>

3)html:optionsCollection标签

该标签生成多个HTML的option元素。其功能和html:options标签的相同。

  <html:select name="selectForm" property="orgIds" size="1">      
<html:optionsCollection name="selectForm" property="orgs" label="orgName" value="orgId"/>
</html:select>

5. html:img标签
最重要的属性page:图象文件的路径,前面必须带有一个斜线。
其它属性:heignt、width、alt。
<html:img page=\"/logo.gif\" height=\"50\" width=\"200\" alt=\"Web Logo\"/>
6.html:link标签
<html:link page=\"/index.html\">Click demo</html:link>