首页 > 代码库 > ActionForm

ActionForm

1、ActionForm就相当于javabean,用来封装用户表单信息提交给控制器
2、控制器(config.xml): 在控制层,struts提供了一个控制器组件Action,他继承了HttpServlet,并重载了HttpServlet的doGet(),doPost()方法,
可以接受Http的响应,并进行转发, 同时还提供了使用XML进行转发的Mapping(映射)功能。
3
、在项目中建立自己的ActionForm 在项目中建立自己的ActionForm,继承struts框架中已经写好的ActionForm,在ActionForm中设置用到的数据,并且要和我们界面上设置的
名称一致。以为我们在提交表单的时候队友东西都会放在ActionForm中
ActionForm代码:
package com.bjpowernode.struts; import org.apache.struts.action.ActionForm; /* 登陆ActionForm,负责表单收集工具 表单的属性必须和ActionForm中的get和set属性一致 */ public class LoginActionForm extends ActionForm{ //用户名 private String username; //密码 private String password; //自动生成get和set方法 } 4、建立自己的Action 建立自己的Action。同时继承Struts框架中org.apache.struts.action.Action,重载父类的execute方法,在这里完成取出表单数据,
通过声明form对象,

通过get方法取得表单中的值,经过判断之后进行相应的操作,跳转到相应的页面。action的功能就是取得表单数据和调用业务逻辑进行页面跳转。 业务逻辑类UserManager和自定异常类:
package com.bjpowernode.struts; public class UserManager{ public void Login(String username,String password){ if(!"admin".equals(username)){ throw newUserNotFoundException(); } if("admin".equals(password)){ throw newPasswordErrorException(); } } } LoginAction代码: package.bjpowernode.struts; importjavax.servlet.httpServletRequest; public class LoginAction extends Action{ @override public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{ LoginActionForm aa = (LoginActionForm)form; String username = aa.getUsername; String password = aa.getPassword; //传递用户名和密码 UserManager userManager = new UserManager(); try { userManager.login(username,password); request.setAttribute("username",username); return mapping.findForward("success"); }catch(UserNotFoundException e){ e.printStackTrace(); request.setAttribute("msg","用户不能找到") }catch(PasswordErrorException e){ e.printStackTrace(); request.setAttribute("msg","没密码错误"); } return mapping.findForward("error"); } 自定义异常类UserNotFoundException和PasswordErrorException代码: package com.bjpowernode.struts; public class UserNotFoundException extends RuntimeException{ public UserNotFoundException(){} public UserNotFoundException(String message){ super(message); } public UserNotFoundException(Throwable cause){ super(cause); } public UserNotFoundException(String message,Throwable cause){ super(message,cause); } } package com.bjpowernode.struts; public class PasswordErrorException extends RuntimeException{ public PasswordErrorException (){} public PasswordErrorException (String message){ super(message); } public PasswordErrorException (Throwable cause){ super(cause); } public PasswordErrorException (String message,Throwable cause){ super(message,cause); } } JSP页面: <%@page language="java" contentType="text/html;charser=GB18030 " pageEncoding="GB18030"%> <!DOCTYPEhtml PUBLIC> <html> <head> <meta http-equiv = "content - Type" content="text/html; charset=GB18030"> <title>Inserttitle</title> </head> <body> <form action="login.do" method="post"> 用户:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="http://www.mamicode.com/登陆"> </form> </body> </html> Login_success.jsp: <%@page language="java"contentType="text/html;charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> ${username},登录成功! </body> </html> Login_error.jsp界面: <%@page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <%-- <%=request.getAttribute("msg") %> --%> ${msg } </body> </html>

 

ActionForm