码迷,mamicode.com
首页 > 其他好文 > 详细

Struts学习DispatchAction之入门篇

时间:2015-04-24 22:37:31      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

步骤:

一、创建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>
View Code

四.跳转写法,及参数与Action跳转函数名对应

技术分享

Struts学习DispatchAction之入门篇

标签:

原文地址:http://www.cnblogs.com/jiqiwoniu/p/4454768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!