• struts学习笔记系列(三)


    今天介绍如何在项目使用action向action之间的传递。

          先介绍没有参数传递的action之间的传值。

          1、新建一个项目,添加了struts之后新建一个jsp页面,用于展示信息。命名为MyJsp.jsp。

          2、新建两个action,第一个action用于向第二个action传值,第二个action跳转到jsp页面展示信息。

          3、配置struts.xml文件。

          下面通过将配置信息以及相应的代码贴出进行讲解。

          stuts.xml代码

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
     3 <struts>
     4     <package name="struts2" extends="struts-default" namespace="/">
     5         <action name="login" class="com.Login">
     6             <result name="list" type="redirectAction">
     7                 <param name="actionName">List</param>
     8             </result>
     9         </action>
    10         <action name="List" class="com.List">
    11             <result name="bookListJsp">/WEB-INF/content/MyJsp.jsp</result>
    12         </action>
    13     </package>
    14 </struts>    

          在上述代码中action是主要讲解的内容:第一个action定义了action之间的跳转,而第二个action定义了action想jsp页面的跳转。
          在name属性为login的action中,type属性为redirectAction,意思是指向一个action(其他的属性在上一篇博文已经介绍)。在struts.xml中action的type属性的分类如下:

          :dispatcher 是type属性的默认值,通常用于转向一个JSP;

          :redirect 重定向

          :redirect-action   重定向到一个Action

          :chain 用来处理Action链,被跳转的action中仍能获取上个页面的值

          :stream   向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据

          :xslt   处理XML/XLST模板

          :redirectAction 重定向到一个Action 

          注:redirect与redirect-action区别

          1、使用redirect需要后缀名 使用redirect-action不需要后缀名;

          2、type="redirect" 的值可以转到其它命名空间下的action,而redirect-action需要配置actoinName 和nameSpace两个属性,就可以访问配置的 nameSpace下的action

         Login.java代码 

    1 package com;
    2 
    3 public class Login {
    4     public String execute() {
    5         return "list";
    6     }
    7 }

          上述代码中只是指定了返回什么,以便在struts.xml文件中对action进行定义。

        List.java代码

     1 package com;
     2 
     3 import java.util.ArrayList;
     4 
     5 import com.sun.org.apache.bcel.internal.generic.NEW;
     6 
     7 public class List {
     8     private ArrayList bookList = new ArrayList();
     9 
    10     public ArrayList getBookList() {
    11         return bookList;
    12     }
    13 
    14     public void setBookList(ArrayList bookList) {
    15         this.bookList = bookList;
    16     }
    17 
    18     public String execute() {
    19         bookList.add("book1");
    20         bookList.add("book2");
    21         bookList.add("book3");
    22         bookList.add("book4");
    23         bookList.add("book5");
    24         return "bookListJsp";
    25     }
    26 }

          在上述代码中定义在前台显示的集合以及返回值以便在struts.xml文件中定义action向哪个jsp跳转。

          MyJsp.jsp代码: 

    1 <body>
    2     <c:forEach var="bookName" items="${bookList}">
    3         <c:out value="${bookName}">
    4         </c:out>
    5         <br>
    6     </c:forEach>
    7 </body>

          部署项目之后在浏览器中输入http://localhost:8080/strutsTest2/login.action,跳转之后浏览器的地址会变为http://localhost:8080/strutsTest1/list.action,并在页面展示了集合中的信息。

        

       

  • 相关阅读:
    .NET 2.0泛型集合类与.NET 1.1集合类的区别(二)
    关于插件的好文章
    MemberInfo.GetCustomAttributes和MemberDescriptor.Attributes获取特性的不同
    新一代编程语言
    .NET 2.0泛型集合类与.NET 1.1集合类的区别(一)
    发现一篇关于.NET 2.0中关于事务命名空间的好文章
    C# WinForm控件美化扩展系列之给TextBox加水印
    log4.net
    C# GDI+ 双缓冲
    C# WinForm控件美化扩展系列之ListBox
  • 原文地址:https://www.cnblogs.com/shangwuyuyi/p/2797087.html
Copyright © 2020-2023  润新知