struts的form标签提供了和原始html几乎一样的属性
属性
Action | 请求地址。直接写动作名称,不用写contextPath |
method | 请求方式,默认的是post请求,可以不用写 |
enctype | 静音编码的MIME类型 |
struts的form表单标签,会自动美化表单的一些样式,比原始的html的form标签更加优良
下面是同样的内容使用struts的form标签和原始的form标签的对比
原始的:
<form action="*">
用户名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
生日:<input type="text" name="birthday"><br>
爱好:
<input type="checkbox" name="hobby" value="写代码">写代码
<input type="checkbox" name="hobby" value="泡妞">泡妞
<input type="checkbox" name="hobby" value="买房">买房<br>
</form>
使用struts的form标签
<s:form>
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:textfield name="birthday" label="生日"></s:textfield>
<s:checkbox name="hobby" label="写代码" value="写代码"></s:checkbox>
<s:checkbox name="hobby" label="打游戏" value="打游戏"></s:checkbox>
<s:checkbox name="hobby" label="旅游" value="旅游"></s:checkbox>
</s:form>
二者对比
从结果显示来看,这个struts会自动优化样式,使表格对齐
这里面的checkbox可以合并成checkboxlist
<s:form>
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:textfield name="birthday" label="生日"></s:textfield>
<%--list使用的OGNL表达式--%>
<s:checkboxlist list="#{'写代码':'写代码','打游戏':'打游戏','旅游':'旅游'}" label="爱好" name="hobby"></s:checkboxlist>
<s:radio list="#{'true':'男','false':'女'}" label="是否已婚" name="married"></s:radio>
</s:form>
<s:form action="/user/register.action">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:textfield name="birthday" label="生日"></s:textfield>
<%--list使用的OGNL表达式--%>
<s:checkboxlist list="#{'写代码':'写代码','打游戏':'打游戏','旅游':'旅游'}" label="爱好" name="hobby"></s:checkboxlist>
<s:radio list="#{'true':'男','false':'女'}" label="是否已婚" name="married"></s:radio>
<s:submit value="注册"></s:submit>
</s:form>
表单特点:
1. 不需要写项目名,会自动加
2. 会将表单里面的内容放到table中,并且加上样式
3. struts的表单项必须要有name属性