Struts 1.x dispatch action 的处理

序:struts 1.x的dispatch action,是一种分发的处理,当一个画面上,你想要提交一个action,但是这个action要做不同的处理的时候,可以考虑使用。其中,个人认为最关键的有以下几点:

1.jsp中请求的action的url:"./dispatch.do?action=search"

2.struts-config.xml中的parameter参数,一定要写成你提交的method的名称

 

1. jsp:

 要注意dispatch和action,dispatch要写到struts-config.xml的action path 下,action要

写到action path 的parameter的属性下

 <%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<%@ taglib uri="/struts-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
</head>
<body>
    <html:link href="./dispatch.do?action=search">search</html:link><br>
    <html:link href="./dispatch.do?action=logout">logout</html:link>
</body>
</html>

2.struts-config.xml:

   <action name="InputForm" path="/dispatch"
          type="sources.dispatchAction"
          parameter="action"
          input="/pages/logonSucess.jsp"
          scope="session">
   <forward name="success" path="/searchSCR.do"></forward>
   <forward name="success1" path="/loginSCR.do"></forward>
  </action>

 

3.action:

  /**
 *
 */
package sources;

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;

/**
 * @author liusk
 *
 */
public class dispatchAction extends DispatchAction{
 public ActionForward search(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {
  // 自動生成されたメソッド・スタブ
  
  InputForm userForm = (InputForm) form;

  String userName = userForm.getUserName();

  //String password = userForm.getPassword();

  if ("".equals(userName) || userName == null) {
   return mapping.findForward("failure");
  } else {
   return mapping.findForward("success");
  }
 }
 
 public ActionForward logout(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {
  // 自動生成されたメソッド・スタブ
  
  InputForm userForm = (InputForm) form;

  String userName = userForm.getUserName();

  //String password = userForm.getPassword();

  if ("".equals(userName) || userName == null) {
   return mapping.findForward("failure");
  } else {
   return mapping.findForward("success1");
  }
 }
}
4,actionForm:

   /**
 *
 */
package sources;

import org.apache.struts.validator.ValidatorActionForm;

/**
 * @author liusk
 *
 */
public class InputForm extends ValidatorActionForm{
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -4309037220475269906L;
 private String userName;
 
 private String password;

 /**
  * @return password
  */
 public String getPassword() {
  return password;
 }

 /**
  * @param password 設定する password
  */
 public void setPassword(String password) {
  this.password = password;
 }

 /**
  * @return userName
  */
 public String getUserName() {
  return userName;
 }

 /**
  * @param userName 設定する userName
  */
 public void setUserName(String userName) {
  this.userName = userName;
 }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值