Strtus 实现 基本登录
Struts 笔记 一,基本的登录表单
1.引入struts库(使用myeclipse一键实现)
2.创建form action 和forward 并做关联(图形界面实现)
3.实现FormBean的validate方法
?
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { //覆盖FormBean的Validate方法 //创建一个ActionError对象 ActionErrors ae = new ActionErrors(); //做校验,设置判断条件,下面是一个demo if(this.username.equals("system")){ //创建一个错误信息对象,usernameerror需要在资源文件中做设置 ActionMessage am = new ActionMessage("usernameerror"); //添加ActionError对象 ae.add("username",am); } if(this.password.equals("admin")){ ActionMessage am2 = new ActionMessage("passworderror"); ae.add("password",am2); } //将错误返回,如果ae不是null,则action不会做跳转 return ae; }
?4.在资源文件中添加usernameerror 和 passworderror 两个字段
5.测试 如果用户名是system或者密码是admin 表单不会提交给action
这种简单应用可以做用户名和密码的后台非空验证,不适合做很复杂的验证