<html:form action="demo.do" method="post">
性别:
<html:radio property="sex" value="男"/>男
<html:radio property="sex" value="女"/>女
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
要想使单选有默认值则需在frome中给set赋予默认值
中文乱码要用Filter解决 建一class继承Fliter
mport java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("GBK") ;
arg2.doFilter(arg0, arg1) ;
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
然后在在web.xml中写入相应的映射
html:select
<html:form action="demo.do" method="post">
选择所喜欢的城市:
<html:select property="city">
<html:option value="BJ">北京</html:option>
<html:option value="TJ">天津</html:option>
<html:option value="NJ">南京</html:option>
<html:option value="GD">广东</html:option>
<html:option value="DB">东北</html:option>
</html:select>
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
html:optionsCollection
<%
Collection col = new ArrayList() ;
col.add(new org.apache.struts.util.LabelValueBean("北京","BJ")) ;
col.add(new org.apache.struts.util.LabelValueBean("天津","TJ")) ;
col.add(new org.apache.struts.util.LabelValueBean("南京","NJ")) ;
col.add(new org.apache.struts.util.LabelValueBean("东北","DB")) ;
col.add(new org.apache.struts.util.LabelValueBean("广东","GD")) ;
// 将全部的集合放在一个属性范围之中
pageContext.setAttribute("ht",col) ;
%>
<html:form action="demo.do" method="post">
选择所喜欢的城市:
<html:select property="city">
<html:optionsCollection name="ht"/>
</html:select>
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
复选框:
<html:form action="demo.do" method="post">
选择所爱吃的水果:
<html:checkbox property="fruit" value="orange"/>橘子
<html:checkbox property="fruit" value="banana"/>香蕉
<html:checkbox property="fruit" value="apple"/>苹果
<html:checkbox property="fruit" value="watermelon"/>西瓜
<html:checkbox property="fruit" value="cherry"/>樱桃
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
复选框默认值问题:
<html:form action="demo.do" method="post">
选择所爱吃的水果:
<logic:iterate id="fru" name="demoForm" property="fruit">
<html:multibox property="select">${fru}</html:multibox>${fru}
</logic:iterate>
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
from应为:
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: 06-05-2007
*
* XDoclet definition:
* @struts.form name="demoForm"
*/
public class DemoForm extends ActionForm {
/*
* Generated fields
*/
/** sex property */
private String sex ;
private String city ;
private String fruit[] = {"orange","apple","cherry","banana","watermelon"} ;
private String select[] = {"orange","apple","cherry"} ;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
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
}
/**
* Returns the sex.
* @return String
*/
public String getSex() {
return sex;
}
/**
* Set the sex.
* @param sex The sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
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:form action="demo.do" method="post">
个人介绍:
<html:textarea property="sex" rows="6" cols="30"/>
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>