LookupDispatchAction的详细使用步骤

1在struts中要学会使用xxx.properties配置文件,配置文件在struts-config.xml中的

<message-resources parameter=”applicationresource”/>中定义。配置文件必须在src根目录下并且文件的扩展名是.properties。在action的子类中经常需要修改配置文件中的配置信息。

2LookupDispatchAction主要用来处理多个同名提交按钮的表单之中,类中可以定义多种业务方法,每种方法都是由配置文件中指定的一个特殊请求参数的值来决定的,提交按钮不一定要使用struts的html标签实现,可以使用标准的html的表单元素创建提交按钮

使用该类时必须重写getKeyMethodMap()方法完成按钮与action中方法的关联

在配置文件中必须再次确认按钮与action中方法的再次关联

                   submit.add=add

submit.delete=delete

action中的方法定义:

    package com.inspur.controller;

 

import java.util.HashMap;

import java.util.Map;

 

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.LookupDispatchAction;

 

public class UserAction extends LookupDispatchAction {

 

    @Override

    protected Map getKeyMethodMap() {

       // TODO Auto-generated method stub

       Map<String,String> map=new HashMap<String,String>();

       map.put("submit.add", "add");

       map.put("submit.delete", "delete");

       return map;

    }

 

   

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

       // TODO Auto-generated method stub

       System.out.println("==add==");

       return mapping.findForward("success");

    }

   public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

       // TODO Auto-generated method stub

       System.out.println("==delete==");

       return mapping.findForward("deleteSuccess");

    }

   

 

}

Struts-config.xml文件的写法:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

 

<struts-config>

  <data-sources />

  <form-beans >

    <form-bean name="userForm" type="com.inspur.view.UserForm"></form-bean>

  </form-beans>

  <global-exceptions />

  <global-forwards />

  <action-mappings >

    <action path="/test" name="userForm" type="com.inspur.controller.UserAction" parameter="method" scope="request">

        <forward name="success" path="/success.jsp"></forward>

        <forward name="deleteSuccess" path="/deleteSuccess.jsp"></forward>

    </action>

  </action-mappings>

   <message-resources parameter="com.yourcompany.struts.ApplicationResources" />

</struts-config>

 

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 'userTest.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 name="myform" action="test.do" method="post">

        <input type="text" name="userName"/><br>

        <input type="text" name="password"/><br>

        <input type="submit" name="method" value="add"/>

        <input type="submit" name="method" value="delete"/>

    </form>

  

  </body>

</html>

配置文件的写法:

# Resources for parameter 'com.yourcompany.struts.ApplicationResources'

# Project demo3

submit.add=add

submit.delete=delete

userform的写法:

package com.inspur.view;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

public class UserForm extends ActionForm{

    private String userName;

    private String password;

    public String getPassword() {

       return password;

    }

    public void setPassword(String password) {

       this.password = password;

    }

    public String getUserName() {

       return userName;

    }

    public void setUserName(String userName) {

       this.userName = userName;

    }

    public UserForm(String userName, String password) {

       super();

       this.userName = userName;

       this.password = password;

    }

    public UserForm(){

      

    }

    @Override

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

       // TODO Auto-generated method stub

       return super.validate(mapping,request);

    }

   

}

以上为LookupDispatchAction类的全部使用步骤。

原文地址:https://www.cnblogs.com/moonfans/p/2716316.html