3. 用 struts 开发简单的登录示例程序
3.1 建立 ActionForm
开发 FORM
l 建一个类,继承 ActionForm
l 注册,修改配置文件 struts-config.xml
1 、当建立一个 Action 时,必须通知 struts ,即这种行为叫做注册。
|
创建一个 ActionForm ;
package com.ibm;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm { private String username = null ; private String password = null ; public String getUsername() { return username ; } public void setUsername(String username) { this . username = username; } public String getPassword() { return password ; } public void setPassword(String password) { this . password = password; }
}
|
l 写入 struts-config.xml
< form-beans > < form-bean name = "loginForm" type = "com.ibm.LoginForm" ></ form-bean > </ form-beans > |
3.2 建立 Action
l 建立一个类,继承 Action 覆盖 execute 方法
|
Com.ibm.LoginAction.java
public class LoginAction extends Action {
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LoginForm loginForm = (LoginForm)form; if (loginForm.getUsername().equals( "ibm" )){ return mapping.findForward( "loginSuccess" ); } else { return mapping.findForward( "loginFailure" ); } }
} |
l 注册,修改配置文件 struts-config.xml
< action path = "/login" type = "com.ibm.LoginAction" name = "loginForm" > < forward name = "loginSuccess" path = "/LoginSuccess.jsp" ></ forward > < forward name = "loginFailure" path = "/LoginFailure.jsp" ></ forward > </ action > |