Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态加载不同类型对象列表的标记_Java_Jsp_Arraylist_Collections_Jsp Tags - Fatal编程技术网

Java 动态加载不同类型对象列表的标记

Java 动态加载不同类型对象列表的标记,java,jsp,arraylist,collections,jsp-tags,Java,Jsp,Arraylist,Collections,Jsp Tags,我有一个非常不同的对象列表,但需要成为一个列表的一部分。e、 g.页面上的小部件列表(天气、时钟、facebook…)。它需要在一个列表中,因为这个列表需要被分成10块,随机和分页 当前解决方案的高级视图如下所示: 一个抽象基类: abstract class MyBaseClass { Object getMe() { return this; } } 公共父类 public class Common extends MyBaseClass { public String co

我有一个非常不同的对象列表,但需要成为一个列表的一部分。e、 g.页面上的小部件列表(天气、时钟、facebook…)。它需要在一个列表中,因为这个列表需要被分成10块,随机和分页

当前解决方案的高级视图如下所示:

一个抽象基类:

abstract class MyBaseClass
{
 Object getMe()
 {
  return this;
 }
}
公共父类

public class Common extends MyBaseClass
{
  public String commonMethod()
  {
    return "somestring";
  }
}
其他类别

public class A extends Common
{
   public String method_1_ForA();
   public String method_2_ForA();
}

public class B extends Common
{
   public String method_1_ForB();
   public String method_2_ForB();
}
这一点的实现如下所示:

public class MyQuickTest
{

 public void test()
 {
   List<A> listA = new ArrayList<>();
   List<B> listB = new ArrayList<>();

   List<Common> listCommon = new ArrayList<>();
   listCommon.addAll(listA);
   listCommon.addAll(listB);

   foreach(Common common:listCommon)
   {
      common.getMe(); //will return an object of type A or B
   }

 }
公共类MyQuickTest
{
公开无效测试()
{
List listA=new ArrayList();
List listB=新的ArrayList();
List listCommon=新的ArrayList();
listCommon.addAll(listA);
listCommon.addAll(listB);
foreach(公共:listCommon)
{
common.getMe();//将返回类型为A或B的对象
}
}
}

为了显示上面的内容,我必须返回上面带有自定义标记库的列表,并在jsp中迭代对象列表。然后,对于每个对象,我必须检查类型,然后调用适当的jsp/.tag来呈现该项

我的问题是:

  • 有没有比从基类返回“object”更好的方法来支持这一点
  • 如何避免在JSP中编写if?是否有一种方法可以完全基于类型加载标记?-谷歌告诉我没有。。或者,如果不是按类型加载,是否还有其他技术、方法或设计模式可以提供更优雅的解决方案 i、 e

    当前解决方案:

    /// loop
     <c:if test="${type==A}"> call render A JSP </c:if>
     <c:if test="${type==B}"> call render B JSP </c:if>     
    // end loop
    
    //loop
      <dispatcher type="${type}"/> //load the appropriate render tag for the object
    //end loop
    
    ///循环
    调用呈现JSP
    调用renderbjsp
    //端环
    
    有希望的解决办法:

    /// loop
     <c:if test="${type==A}"> call render A JSP </c:if>
     <c:if test="${type==B}"> call render B JSP </c:if>     
    // end loop
    
    //loop
      <dispatcher type="${type}"/> //load the appropriate render tag for the object
    //end loop
    
    //循环
    //为对象加载适当的渲染标记
    //端环
    

    任何帮助或建议都将不胜感激

    我相信我已经找到了上述两个问题(第一季度、第二季度)的解决方案。我的调查结果如下:

    问题1。我不需要有一个返回object的基类;只要所有子类都从“Common”继承,我就可以返回一个Common列表,并使用instanceof获取子类型信息。因此,考虑到原始示例,我可以省略抽象类“MyBaseClass”,而是执行以下操作

    public class MyQuickTest
    {
    
     public void test()
     {
      List<A> listA = new ArrayList<>();
      List<B> listB = new ArrayList<>();
    
      List<Common> listCommon = new ArrayList<>();
      listCommon.addAll(listA);
      listCommon.addAll(listB);
    
      foreach(Common common:listCommon)
      {
       if(common instanceof B)
       {
        B b = (B) common;
        String s = b.method_1_ForB();
        //do some other logicfind solutions for the above problem.
       }
      }
    
    }
    
    公共类MyQuickTest
    {
    公开无效测试()
    {
    列出使用嵌套范围变量的位置

    *我选择使用选项“ii”,因为有一个明确的关注点分离,即对象不需要知道它是如何呈现的,而是全部用于JSP。JSP方法对其他团队成员来说也更直观,并与我们现有项目的常用方法保持一致*