步骤:
一、创建JavaWeb工程;
二、创建FormBean代码如下
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.zkj.struts.form; import org.apache.struts.action.ActionForm; /** * MyEclipse Struts Creation date: 04-24-2015 * * XDoclet definition: * * @struts.form name="userForm" */ public class UserForm extends ActionForm { private String username; private String passwd; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } }
三、创建DispatchAction
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.zkj.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.zkj.struts.form.UserForm; /** * MyEclipse Struts Creation date: 04-24-2015 * * XDoclet definition: * * @struts.action path="/userDispatch" name="userForm" scope="request" */ public class UserDispatchAction extends DispatchAction { /* * Generated Methods */ /**** * 使用分派aciton, 往往需要自己重新命名函数,不改也可以 该函数用于响应登陆请求 */ public ActionForward userLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub if ("123".equals(userForm.getPasswd())) return mapping.findForward("ok"); else return mapping.findForward("relogin"); } /**** * 使用分派aciton, 往往需要自己重新命名函数,不改也可以 该函数用于响应注销请求 */ public ActionForward userLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub request.getSession().invalidate(); //所有Session一次清空 return mapping.findForward("relogin"); } }
三、struct-config.xml
视图结构:
代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans > <form-bean name="userForm" type="com.zkj.struts.form.UserForm" /> </form-beans> <global-exceptions /> <global-forwards /> <!--parameter参数用于区分用户的不同请求--> <action-mappings > <action attribute="userForm" input="/ok.jsp" name="userForm" parameter="direct" path="/userDispatch" scope="request" type="com.zkj.struts.action.UserDispatchAction" validate="false" cancellable="true"> <forward name="relogin" path="/login.jsp" /> <forward name="ok" path="/ok.jsp" /> </action> </action-mappings> <message-resources parameter="com.zkj.struts.ApplicationResources" /> </struts-config>
四.跳转写法,及参数与Action跳转函数名对应
时间: 2024-10-17 16:07:54