动态Action的实现:

在下面用的是只用一个Action 来处理多个Form 的业务处理:

它的原理是:1利用一个隐藏的文本域传一个方法名:然后调用Action中相映的方法:而这个Action一定是一个DispatchAction 代码如下:

package com.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.action.DynaActionForm;

import org.apache.struts.actions.DispatchAction;

 

 

public class LoginAction extends DispatchAction {

 

    public ActionForward love(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       DynaActionForm da=(DynaActionForm)form;

       System.out.println(da.get("name")+"   I love you ");

       return null;

    }

    public ActionForward dislike(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       DynaActionForm da=(DynaActionForm)form;

       System.out.println(da.get("name")+"   I dislike you ");

       return null;

    }

    public ActionForward miss(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       DynaActionForm da=(DynaActionForm)form;

       System.out.println(da.get("name")+"   I miss you ");

       return null;

    }

}

 

而在jsp中的页面中的代码如下:

<%@ page language="java" pageEncoding="utf-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

 

<html>

    <head>

       <title>JSP for LoginForm form</title>

       <script type="text/javascript">

       <!--

           function _action(_value){

              document.forms[0].method.value=_value;

              document.forms[0].submit();

           }

       //-->

       </script>

    </head>

    <body>

       <html:form action="/login">

       <html:text property="name"/>

       <html: hidden property="method"/>

           姓名<input type="button" name="" value="love" onclick="_action(this.value)"/>

           <input type="button" name="" value="dislike" onclick="_action(this.value)"/>

           <input type="button" name="" value="miss" onclick="_action(this.value)"/>

       </html:form>

    </body>

</html>

如上是一个的一个hidden

 

然后在struts-config.xml中的配置中如下代码:标明hidden:

<action-mappings >

    <action

      attribute="loginForm"

      input="/form/login.jsp"

      name="loginForm"

      path="/login"

      parameter="method"

      scope="request"

      type="com.struts.action.LoginAction" />

  </action-mappings>

在上面的parameter指定hidden

 

超链接访问如:<a herf="index.do?mehto=login"></a>

 

你可能感兴趣的:(apache,html,bean,jsp,struts)