Struts之DispatchAction使用(录像教程)

Posted on 2005-12-04 10:46 oksonic 阅读(8333) 评论(10)  编辑  收藏 所属分类: java

速动画教程第十三集

 

下载地址:http://sonic.peakle.net/download/sonic013.rar

 

Struts 之 DispatchAction

 

介绍

    DispatchAction就是在struts-config中用parameter参数配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法。

    例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的请求将根据parameter被分发到"save"或者"edit"或者什么。但是有一点,save()或者edit()等方法的声明和execute必须一模一样。

 

新建工程:test

添加Struts框架

 

创建index.jsp

 

按下Ctrl + N ,创建add.jspUsersAction.java

ActionForm采用动态的ActionForm,所以继承于DynaActionForm

UserAction的内容将包含add、delall等方法,并且继承于DispatchAction

 

* 记得修改AddAction.java 为 UsersAction

 

<action

      attribute="addForm"

      input="/add.jsp"

      name="addForm"

      parameter="method"

      path="/add"

      scope="request"

       validate="false"

      type="com.test.struts.action.UsersAction" />

 

* 绿色字全部份为参数

 

新建一个forward,名称为indexGo,并指向index.jsp,Relative设置为true

 

修改add.jsp文件

              <html:form action="/add">

           username : <html:text property="username"/><html:errors property="username"/><br/>

           <html:submit onclick="document.forms[0].action='add.do?method=add';document.forms[0].submit();"/><html:cancel/>

       </html:form>

 

* 绿色字为修改部份

修改后的提交方式是带参数提交的,不过必须点提交按钮,如果是使用回车键的话就不会带有参数

 

修改UsersAction.java文件

增加以下代码:

    public ActionForward add(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response) {

        DynaActionForm addForm = (DynaActionForm) form;

        String username = addForm.getString("username");

        // 验证用户输入

        if (username == null || username.length() < 1)

            mapping.getInputForward();

        HttpSession session = request.getSession();

        // 从session中获得数据

        Vector users = (Vector) session.getAttribute("Users");

        if (users == null)

            users = new Vector();

        users.addElement(username);

        session.setAttribute("Users", users);

        return mapping.findForward("indexGo");

    }

 

 

修改index.jsp文件,使页面中可以显示session中的数据,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

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

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

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

<html>

  <head>

    <title>INDEX</title>

  </head>

 

  <body>

    <a href="add.jsp">ADD USER</a><br>

    <a href="delete.jsp">DELETE ALL</a><p>

    <logic:present name="Users">

    <logic:iterate id="element" name="Users">

        <bean:write name="element"/><br>

    </logic:iterate>

    </logic:present>

  </body>

</html>

 

 

按下Ctrl + N ,创建DellallAction.java,继承于DispatchAction

选中:Use existing Action class,浏览UsersAction

选中:Parameter选项卡,填入method,然后完成

 

现在修改index.jsp文件

<a href="delete.jsp">DELETE ALL</a><p>

改为

<a href="delall.do?method=delall">DELETE ALL</a><p>

 

修改UsersAction.java文件

增加以下代码:

    public ActionForward delall(

            ActionMapping mapping,

            ActionForm form,

            HttpServletRequest request,

            HttpServletResponse response) {

            HttpSession session=request.getSession();

            session.setAttribute("Users",null);

            return mapping.findForward("indexGo");

        }

 

这一步很重要,execute 方法必须删除!!!

 

好了,可以进行测试了!!!

Feedback

# re: 速动画教程第十三集Struts之DispatchAction  回复  更多评论   

2005-12-05 10:53 by 铁手
So cool so good. pls go on. Supporting!

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-01-08 18:58 by ooad
感谢oksonic:
你的例子我作了,很好,在此表示感谢!
 但是怎末解决回车不带参数呢?
还有就是我看到大部分都是继承于LookUpDispatchAction 他们有什么区别呢??
盼望回复.

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-03-15 15:51 by xs
不知道为什么我做的例子每次加进去两条 ,请帮我解答一下。谢谢

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-03-22 10:41 by achun
感谢一下!

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-03-31 18:38 by 雨来
好伟大啊

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-05-08 16:22 by lingdian
楼主,为什么你的gmail里没有这个的sonic013的文件啊!!!

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-07-13 10:27 by abc
* 绿色字为修改部份

修改后的提交方式是带参数提交的,不过必须点提交按钮,如果是使用回车键的话就不会带有参数

-----------------请问如何解决回车提交 ?

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-09-14 21:11 by hawks
下面这段代码
<table align=center border="15" cellspacing="2" width="89%"> <br>
<tr>
<th>用户名 </th><th>角色</th><th>删除</th>
</tr>
<logic:iterate id="rs" name="queryResult">
<tr>
<form action="/searchAction.do?expression=<bean:write name='rs' property='username' />">
<td><bean:write name="rs" property="username" /></td>
<td><bean:write name="rs" property="role" /></td>
<td>
<html:submit value="remove" onclick="document.forms[0].action='searchAction.do?method=remove';document.form[0].submit();"/>
</td>
</form >
</tr>
</logic:iterate>
</table>
用form[0]不合适,需要一个变量,类似form[i],应该怎样;或者是有其他的方法,例如bean:message。不知道怎么实现,请教楼主。

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2006-12-18 14:54 by LeVaN
http://www.anna-sen-soida-elastinen.beibi.info ^^^ http://www.anssi-22-vuosi-joensuu.beibi.info ^^^ http://www.eldre-masturbering-mpeg.biseksuell.info ^^^ http://www.film-prostituert-hetest.biseksuell.info ^^^ http://www.eldre-masturbering-mpeg.erotiska.info ^^^ http://www.film-prostituert-hetest.erotiska.info ^^^ http://www.film-snall-homosexuell.fitta69.info ^^^ http://www.videor-transsexuell-rakning.fitta69.info ^^^ http://www.sympatisk-foto-mpg.fotsex.info ^^^ http://www.naturlig-penis-klipp.fotsex.info ^^^ http://www.naida-gratissex.isomuna.info ^^^ http://www.uhkarohkea-poliisi-striptease.isomuna.info ^^^ http://www.sopo-tytot-vittu.laukeaminen.info ^^^ http://www.pelottava-lesbo-pano.laukeaminen.info ^^^ http://www.porno-kukk-jenter.rype.info ^^^ http://www.klipp-munnsex-strippe.rype.info ^^^ http://www.knulle-pornostjerne-porno.sadsprut.info ^^^ http://www.eksotisk-bitch-bilde.sadsprut.info ^^^ http://www.onnelliset-orgasmi.tytsy.info ^^^ http://www.latino-pillut-imeskella.tytsy.info ^^^ http://www.masturbation-creampie-ass.18analsex.com ^^^ http://www.anl-fucking-xxx.18analsex.com ^^^

# re: Struts之DispatchAction使用(录像教程)  回复  更多评论   

2008-01-18 13:32 by 插两条记录的解决方法
<html:button value="submit" property="submit" onclick="document.forms[0].action='record.do?method=add';document.forms[0].submit();"/><html:cancel/>

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 103, comments - 1104, trackbacks - 0, articles - 0

Copyright © oksonic