struts的DispatchAction的应用

一 为什么需要分派action


 
 
二 快速入门
1、创建一个web工程
2、引入struts并配置 struts-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.yourcompany.struts.form.UserForm" />
  </ form-beans >
  < global-exceptions />
  < global-forwards />
  <!-- parameter用于区分用户的不同的请求 -->
  < action-mappings >
    < action
      attribute = "userForm"
      input = "/WEB-INF/login.jsp"
      name = "userForm"
      parameter = "flag"
      path = "/loginandLogout"
      scope = "request"
      type = "com.yourcompany.struts.action.LoginandLogoutAction"
      cancellable = "true" >
      < forward name = "loginok" path = "/WEB-INF/ok.jsp" />
      < forward name = "gologin" path = "/WEB-INF/login.jsp" />
    </ action >
  </ action-mappings >
 
  < message-resources parameter = "com.yourcompany.struts.ApplicationResources" />
</ struts-config >
3、JSP页面
3.1 login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "ISO-8859-1" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page </ title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
         <!--
        <link rel =" stylesheet " type="text/ css " href ="styles.css">
        -->
 
  </ head >
 
  < body >
    < form action = "/dispatch/loginandLogout.do?flag=userlogin" method = "post" >
        u: < input type = "text" name = "username" >< br />
        p: < input type = "password" name = "password" >< br />
         < input type = "submit" value = "login" >
    </ form >
  </ body >
</ html >
3.2 ok.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page </ title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
         <!--
        <link rel =" stylesheet " type="text/ css " href ="styles.css">
        -->
 
  </ head >
 
  < body >
         < h1 > 登录成功 </ h1 >
         < a href = "/dispatch/loginandLogout.do?flag=userloginout" > 退出系统 </ a >
  </ body >
</ html >
LoginandLogoutAction
LoginandLogoutAction
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.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.yourcompany.struts.form.UserForm;
/**
* MyEclipse Struts
* Creation date: 10-21-2017
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class LoginandLogoutAction extends DispatchAction {
    /*
     * Generated Methods
     */
    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    //使用分派action往往需要自己重新命名函数,这个函数用于响应登录请求
    public ActionForward userlogin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        UserForm userForm=(UserForm) form;
        if("123".equals(userForm.getPassword())){
            return mapping.findForward("loginok");
        }
        else
        {
            return mapping.findForward("gologin");
        }
        
    }
    //这个函数用于响应注销请求
    public ActionForward userloginout(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        //把用户的session清空
        request.getSession().invalidate();
        return mapping.findForward("gologin");
    }
}
 
UserForm
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;
import org.apache.struts.action.ActionForm;
/**
* MyEclipse Struts
* Creation date: 10-21-2017
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
    private String username;
    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;
    }
    private String password;
}

猜你喜欢

转载自cakin24.iteye.com/blog/2398234
今日推荐