sakrua`s java 世界
struts hibernate spring web2.0 ajax
posts - 0,  comments - 0,  trackbacks - 0
在以前的例子中,我们每一个action都只有一个动作,例如execute函数,那么有没有其它的方法让一个Action有多个动作昵,

当然那些设计者也不是SB,那么我们看看API文档,在包org.apache.struts.actions中我们会发现有很多的Action

org.apache.struts.actions

Interfaces
DownloadAction.StreamInfo

Classes
ActionDispatcher
BaseAction
DispatchAction
DownloadAction
DownloadAction.FileStreamInfo
DownloadAction.ResourceStreamInfo
EventActionDispatcher
EventDispatchAction
ForwardAction
IncludeAction
LocaleAction
LookupDispatchAction
MappingDispatchAction
SwitchAction

我们从字面的意思看上去,好像第三个有的像,那么我们来看看 org.apache.struts.actions.DispatchAction

来看看下面的英文注解:

An abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter property of the corresponding ActionMapping. This Action is useful for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design.

To configure the use of this action in your struts-config.xml file, create an entry like this:

<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>

which will use the value of the request parameter named "method" to pick the appropriate "execute" method, which must have the same signature (other than method name) of the standard Action.execute method. For example, you might have the following three methods in the same action:

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

and call one of the methods with a URL like this:

http://localhost:8080/myapp/saveSubscription.do?method=update

NOTE - All of the other mapping characteristics of this action must be shared by the various handlers. This places some constraints over what types of handlers may reasonably be packaged into the same DispatchAction subclass.

NOTE - If the value of the request parameter is empty, a method named unspecified is called. The default action is to throw an exception. If the request was cancelled (a html:cancel button was pressed), the custom handler cancelled will be used instead. You can also override the getMethodName method to override the action's default handler selection.

从第一段可以知道,这就是我们所要找的那个它了,

<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>

还有这里有一个配置的例子 ,它的意思就是说,这个Action提供了一个方法让我们跟椐parameter="method" (这里是method)让我们调用Action中的不同的方法,当然这些方法跟我们的execute是一样的只是名字不一样。例如下面:

public ActionForward execute(ActionMapping arg0, ActionForm arg1,
        HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
    return super.execute(arg0, arg1, arg2, arg3);
}
//跟上面对比一下,是不是一样呢
public ActionForward sayHello(ActionMapping arg0, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.getWriter().write("Hello ! DidpatchAction");
    response.flushBuffer();
    return null;
}

public ActionForward closeWindow(ActionMapping arg0, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    String str ="IE Will Close<script language=\"javascript\">window.close();</script>";
    response.getWriter().write(str);
    response.flushBuffer();
    return null;

好下面我们在以前的例子的基础上完成这次的例子

新建一个struts-config-action.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>

    <action-mappings>
        <action path="/testAction" validate="false" parameter="method"
         type="org.action.struts.DidpatchActionTest">
        </action>
    </action-mappings>
</struts-config>

是不是看不习惯呢,没有Action的也没有forward。不过现在我告诉你这是完全可以的

我们把这个配置文件加到web.xml文件中

<init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-config-file.xml,/WEB-INF/struts-config-action.xml</param-value>
    </init-param>

image

以这种目录结构建一个index.jsp文件

下面为jsp的内容:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Action Test</title>
</head>
<body>
<a href="../testAction.do?method=sayHello">sayHello</a>
<a href="../testAction.do?method=closeWindow">closeWindow</a>
</body>
</html>

没有什么好说的。

下面我们建一个Action,当然基类是org.apache.struts.actions.DispatchAction

下面为Action的内容:

package org.action.struts;

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;

public class DidpatchActionTest extends DispatchAction {

    public ActionForward execute(ActionMapping arg0, ActionForm arg1,
            HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        return super.execute(arg0, arg1, arg2, arg3);
    }
    //跟上面对比一下,是不是一样呢
    public ActionForward sayHello(ActionMapping arg0, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.getWriter().write("<script language=\"javascript\">alert('Hello ! DidpatchAction');</script>");
        response.flushBuffer();
        return null;
    }

    public ActionForward closeWindow(ActionMapping arg0, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String str ="IE Will Close<script language=\"javascript\">window.close();</script>";
        response.getWriter().write(str);
        response.flushBuffer();
        return null;
    }

}

我们用response对象来输出页面的内容(这个与jsp的response对象是一样的);

下面为运行的结果

image

image

image

大家看到了没有,http://localhost:8080/Struts_HelloWorld/testAction.do?method=closeWindow

它会跟椐method中的参数来调用不同的函数

跟菜鸟学struts (3) - actionFrom

通常我们在Struts中是使用ActionForm来传送jsp页面的form中提交的数据的,在前面的例子中我们也用到了一个

ActionForm它们都是从org.apache.struts.action.ActionForm中继承来的,那么Struts就行帮我们把提交的数

据自动入到这个Form 中,我们在Action中就可以用form.getXXX()和form.setXXX()来处理数据了,好!下面我

们来做一下这个例子

新建一个文件struts-config-formtest.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="testForm" type="org.form.struts.form.NormalForm"  ></form-bean>
        <form-bean name="dynaTestForm" type="org.apache.struts.action.DynaActionForm">
            <form-property name="userName" type="java.lang.String"></form-property>
            <form-property name="passWorld" type="java.lang.String"></form-property>
            <form-property name="sex" type="byte" initial="0"></form-property>
            <form-property name="email" type="java.lang.String"></form-property>
            <form-property name="phoneNum" type="java.lang.String"></form-property>
            <form-property name="age" type="int" initial="12"></form-property>
        </form-bean>
    </form-beans>

    <action-mappings>
        <action path="/formTest"  name="testForm" type="org.form.struts.action.NormalFormAction">
            <forward name="success" path="/normalsuccess.jsp"></forward>
        </action>
        <action path="/dynaformTest"  name ="dynaTestForm" type="org.form.struts.action.dynaFormAction">
            <forward name="success" path="/dynasuccess.jsp"></forward>
        </action>
        <action path="/gotoaction" type="org.form.struts.action.GotoAction">
            <forward name="success" path="/index.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

现在我们只要看到这一句就足够了,动态Form我们会在下面讲解

      <form-bean name="testForm" type="org.form.struts.form.NormalForm"  ></form-bean>

相信朋友们都清楚这个用来做什么的吧,不知道就看看我先前的文章。

下面为NormalForm.java的内容

package org.form.struts.form;

import org.apache.struts.action.ActionForm;

public class NormalForm extends ActionForm {
    private static final long serialVersionUID = -4017195510815090304L;

    private String userName;
    private String passWorld;
    private byte sex;
    private String email;
    private String phoneNum;
    private int age;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWorld() {
        return passWorld;
    }
    public void setPassWorld(String passWorld) {
        this.passWorld = passWorld;
    }
    public byte getSex() {
        return sex;
    }
    public void setSex(byte sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

我们可以看到这个只是个从ActionForm派生来的javabean,下面把jsp页面也帖出来

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>请选择你要测试的form</title>
</head>
<body>
    <html:form action="formTest" styleId="testform" >
        userName:<html:text property="userName"></html:text><br />
        passWorld:<html:password property="passWorld"></html:password><br />
        sex<html:radio property="sex" value="0" title="男">男</html:radio>
        <html:radio property="sex" value="1" title="女">女</html:radio><br />
        email<html:text property="email"></html:text><br />
        phoneNum<html:text property="phoneNum"></html:text><br />
        age<html:text property="age"></html:text><br />
        normalForm submit<html:submit onclick="submit(0);return false;"></html:submit><br />
        dynaForm submit<html:submit ></html:submit>
    </html:form>
</body>
</html>

这次不会讲它的目录结构,在最后我会放出下载地址

好,我们拉着反struts动态from也讲完再来看运行效果:

动态Form是什么意思呢,好就是让我们不用写上面的javabean面直接在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="testForm" type="org.form.struts.form.NormalForm"  ></form-bean>
        <form-bean name="dynaTestForm" type="org.apache.struts.action.DynaActionForm">
            <form-property name="userName" type="java.lang.String"></form-property>
            <form-property name="passWorld" type="java.lang.String"></form-property>
            <form-property name="sex" type="byte" initial="0"></form-property>
            <form-property name="email" type="java.lang.String"></form-property>
            <form-property name="phoneNum" type="java.lang.String"></form-property>
            <form-property name="age" type="int" initial="12"></form-property>
        </form-bean>

    </form-beans>

    <action-mappings>
        <action path="/formTest"  name="testForm" type="org.form.struts.action.NormalFormAction">
            <forward name="success" path="/normalsuccess.jsp"></forward>
        </action>
        <action path="/dynaformTest"  name ="dynaTestForm" type="org.form.struts.action.dynaFormAction">
            <forward name="success" path="/dynasuccess.jsp"></forward>
        </action>
        <action path="/gotoaction" type="org.form.struts.action.GotoAction">
            <forward name="success" path="/index.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

我们可以看到form中的所有的内容都在xml文件中写出来了,

            <form-property name="sex" type="byte" initial="0"></form-property>
            <form-property name="email" type="java.lang.String"></form-property>
看看上面的两行有什么不一样,它们都是用<form-property>来定义的都有name (相当于javabean中的一个property)

type 不用说了吧就是它的类型,不同的是在第一行中多了一个initial="0" 为什么呢,请记住在基本数据成员是要初始化的

就像局部变量一样。

其实这个东东没什么难度吧,下面讲一个我们初学者都出现的问题,

我们可以先找找这个关键字Cannot retrieve mapping for action 了解一下

下面我们进入正题吧:

web.xml的内容

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xm</param-value>
    </init-param>
    <init-param>
        <param-name>config/formtest</param-name>
        <param-value>/WEB-INF/struts-config-formtest.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

我们可以看到这里有两个模块,问题就在这里了,(如果不清楚struts多个配置文件的使用可以看我另一个文章)

在配置文件中你们可以看到我们多了模块,在直接访问jsp文件时,struts只会在默认的模块中找Action所以我们只能

通过一个Action来做连接(好不爽啊)。

下面帖运行效果出来:

image

image

下面这个是找不到Action的效果

image

   <!-- 动态Form用这个 -->

<html:form action="formTest" styleId="testform" >
<!--<html:form action="dynaformTest" styleId="testform" >-->

下载地址

Click to view this file

posted on 2007-11-18 13:32 风の使者 阅读(418) 评论(0)  编辑  收藏 所属分类: struts

<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章分类

文章档案

搜索

  •  

最新评论