Struts标签<html:radio/>校验失败返回后默认值不能保持,纠结半天是乱码问题!!
1、标签页面
<strong>用户注册</strong> <html:form action="/register"> 用户名 : <html:text property="username"/><html:errors property="regun"/><br/> 密码 : <html:password property="password"/><html:errors property="regpw"/><br/> 确认密码 : <html:password property="password2"/><html:errors property="regpw2"/><br/> 性别 : <html:radio property="sex" value="男">男</html:radio> <html:radio property="sex" value="女">女</html:radio><br/> 生日 : <html:text property="birthday" onclick="JTC.setday(this)" readonly="true"/><html:errors property="regbday"/><br/> 邮箱 : <html:text property="email"/><html:errors property="regemail"/><br/> <html:submit value="提交"/><html:reset value="重置"/> </html:form>
2、actionForm中添加校验和默认值
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if(username == null || username.trim().equals("")) { errors.add("regun",new ActionMessage("reg.error.username")); } if(password == null || password.trim().equals("")) { errors.add("regpw",new ActionMessage("reg.error.password")); } if(password2 == null || password2.trim().equals("") || !password2.equals(password)) { errors.add("regpw2",new ActionMessage("reg.error.password2")); } if(birthday == null || !birthday.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) { errors.add("regbday",new ActionMessage("reg.error.birthday")); } if(email == null || !email.matches(".+@.+\\..+")) { errors.add("regemail",new ActionMessage("reg.error.email")); } return errors; } public void reset(ActionMapping mapping, HttpServletRequest request) { sex = "男"; }
3、页面提交后,提示校验信息,但是<html:radio/>标签不显示默认值!
这种情况下不选择值,直接提交保存,可以保存,但是radio是乱码!
--------没有设置编码,添加编码的filter:
public static final String ENCODING = "UTF-8"; public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ((HttpServletRequest) request).setCharacterEncoding(ENCODING); ((HttpServletResponse) response).setCharacterEncoding(ENCODING); chain.doFilter(request, response); } public void init(FilterConfig arg0) throws ServletException { }
便可以正常显示了,原来是中文传过程中编码出现问题。望借鉴!最好用英文字符!