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;

 

}
  • 大小: 105.2 KB
  • 大小: 14.4 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值