struts-config.xml 写道
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="usersForm" type="com.accp.struts.form.UsersForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="usersForm"
name="usersForm"
parameter="method"
path="/users"
scope="request"
validate="true"
input="/msg.jsp"
type="com.accp.struts.action.UsersAction">
<forward name="index" path="/index.jsp"></forward>
<forward name="msg" path="/msg.jsp"></forward>
<forward name="agree" path="/userInfo.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.accp.struts.ApplicationResources" />
</struts-config>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="usersForm" type="com.accp.struts.form.UsersForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="usersForm"
name="usersForm"
parameter="method"
path="/users"
scope="request"
validate="true"
input="/msg.jsp"
type="com.accp.struts.action.UsersAction">
<forward name="index" path="/index.jsp"></forward>
<forward name="msg" path="/msg.jsp"></forward>
<forward name="agree" path="/userInfo.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.accp.struts.ApplicationResources" />
</struts-config>
form写道
public class UsersForm extends ActionForm
{
private Users users=new Users();
private String password2;//确认密码
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors=new ActionErrors();
if(null==this.users.getUserName() || "".equals(this.users.getUserName()))
{
System.out.println("kfsksk");
System.out.println(errors);//这值能取到
errors.add("errors.uname", new ActionMessage("errors.uname"));
System.out.println(errors.size());
}
else if(null==this.users.getPassword() || "".equals(this.users.getPassword()))
{
errors.add("errors.upass", new ActionMessage("errors.upass"));
}
else if(!(this.users.getPassword().equals(password2)) || this.users.getPassword()!=password2)
{
errors.add("errors.upass2", new ActionMessage("errors.upass2"));
}
else if(null==this.users.getUIdCode() || "".equals(this.users.getUIdCode()))
{
errors.add("errors.IDCard", new ActionMessage("errors.IDCard"));
}
else if(null==this.users.getUTel() || "".equals(this.users.getUTel()))
{
errors.add("errors.uTel", new ActionMessage("errors.uTel"));
}
else if(null==this.users.getUAddress() || "".equals(this.users.getUAddress()))
{
errors.add("erros.uAddress", new ActionMessage("erros.uAddress"));
}
else if(null==this.users.getUZip() || "".equals(this.users.getUZip()))
{
errors.add("errors.uZip", new ActionMessage("errors.uZip"));
}
if(!errors.isEmpty())
{
return errors;
}
return null;
}
/*
* Generated Methods
*/
public Users getUsers()
{
return users;
}
public void setUsers(Users users)
{
this.users = users;
}
public String getPassword2()
{
return password2;
}
public void setPassword2(String password2)
{
this.password2 = password2;
}
}
{
private Users users=new Users();
private String password2;//确认密码
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors=new ActionErrors();
if(null==this.users.getUserName() || "".equals(this.users.getUserName()))
{
System.out.println("kfsksk");
System.out.println(errors);//这值能取到
errors.add("errors.uname", new ActionMessage("errors.uname"));
System.out.println(errors.size());
}
else if(null==this.users.getPassword() || "".equals(this.users.getPassword()))
{
errors.add("errors.upass", new ActionMessage("errors.upass"));
}
else if(!(this.users.getPassword().equals(password2)) || this.users.getPassword()!=password2)
{
errors.add("errors.upass2", new ActionMessage("errors.upass2"));
}
else if(null==this.users.getUIdCode() || "".equals(this.users.getUIdCode()))
{
errors.add("errors.IDCard", new ActionMessage("errors.IDCard"));
}
else if(null==this.users.getUTel() || "".equals(this.users.getUTel()))
{
errors.add("errors.uTel", new ActionMessage("errors.uTel"));
}
else if(null==this.users.getUAddress() || "".equals(this.users.getUAddress()))
{
errors.add("erros.uAddress", new ActionMessage("erros.uAddress"));
}
else if(null==this.users.getUZip() || "".equals(this.users.getUZip()))
{
errors.add("errors.uZip", new ActionMessage("errors.uZip"));
}
if(!errors.isEmpty())
{
return errors;
}
return null;
}
/*
* Generated Methods
*/
public Users getUsers()
{
return users;
}
public void setUsers(Users users)
{
this.users = users;
}
public String getPassword2()
{
return password2;
}
public void setPassword2(String password2)
{
this.password2 = password2;
}
}
action代码
public ActionForward doAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); System.out.println("fsfsa");//这里打印不出来,没进入action String str=""; UsersForm usersForm = (UsersForm) form; Users u=usersForm.getUsers(); System.out.println(u); boolean b=userBiz.isExists(u.getUserName()); if(b) { request.getSession().setAttribute("msg", "该用户已存在,请重新注册!"); str="msg"; } else { int i=userBiz.addUser(u); if(i>0) { request.getSession().setAttribute("msg", "添加用户成功!请登录!"); str="msg"; } else { request.getSession().setAttribute("msg", "添加用户失败!请重新注册!"); str="msg"; } } return mapping.findForward(str); }
在页面都是通过<html:errors property=""/>的方式来取值,但是就是没进入action,直接跳转到在struts-config.xml文件中的action配置里面input的页面
麻烦大家帮我看看,谢谢啦!