Accessing method of different class in jsp

Code Break

I am new to java and JSP, i have doubt about accessing the method of tag class. i have created 3 custom tags namely Primary, Secondary, Axis. so i can access

<pre:Primary>
  <pre:Axis/>
</pre:Primary>

<pre:Secondary>
<Pre:Axis/>
</pre:Secondary> 

In Axis Tag class i have to access the parent methods, in this case primary, secondary is parent for Axis tag. In both parent class, i have method addItem(), i am calling the method as below in axis class.

 Object parent=null;
         try{
             parent = (PrimaryWrapper)getParent();
         }catch(Exception e){
            parent = (SecondaryWrapper)getParent();
         }
     parent.addItem(axisItem);

but this is shows error, "The method addItem(StringBuilder) is undefined for the type Object", i know in try, catch only the scope of parent is changing to primary/secondary, hence the error occurs. but i have to access the appropriate addItem method based on Primary, Secondary. is it Possible? Or i am doing wrong.

Thanks in advance

Jozef Chocholacek

The problem is the parent variable is of class Object, so anything you assign to it, you can only use the Object's methods. I see basically two possible solutions.

Quick and dirty

Object parent = getParent();
if (parent instanceof PrimaryWrapper) {
  ((PrimaryWrapper) parent).addItem(axisItem);
} else if (parent instanceof SecondaryWrapper) {
  ((SecondaryWrapper) parent).addItem(axisItem);
} else {
  // log error?
}

Better - using Interface

// define an interface
public interface ItemContainer {
  void addItem(StringBuilder item);
}

// change your Wrappers to implement the interface, i.e.
public class PrimaryWrapper extends ... implements ItemContainer {
  // ... your code, just add @Override annotation to the addItem(...) method
}
// and
public class SecondaryWrapper extends ... implements ItemContainer {
  // ... your code, just add @Override annotation to the addItem(...) method
}

// and finally, fix the AxisTag code
ItemContainer parent = (ItemContainer) getParent();
parent.addItem(axisItem);

Or, as mentioned in comment from Hacketo, instead of creating and interface, you could create an common ancestor class (e.g. Wrapper) and implement the common methods there. The code of AxisTag would be similar to the above solution with interface.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing JTextField in a different class and adding it to a String

From Dev

Single Jsp with different Action Class

From Dev

Accessing class variables from ActionListener method

From Dev

different signatures for accessing class methods

From Dev

Difficulty accessing class method during keystroke event

From Dev

Accessing a HashMap from a different class

From Dev

Accessing a file in a different Java class?

From Dev

Accessing volley response in another class/method

From Dev

Implement method for class in a different class

From Dev

Accessing an internal class from a different Dll file

From Dev

accessing method of the class inside of async module or

From Dev

Accessing method in a class from MainWindow

From Dev

Loop to beginning with JFrame, accessing method in different class

From Dev

accessing instance methods from class method proc

From Dev

Accessing Arraylist contents from a different class?

From Dev

Single Jsp with different Action Class

From Dev

Accessing class variables from ActionListener method

From Dev

Difficulty accessing class method during keystroke event

From Dev

Accessing public method of another class

From Dev

Accessing a public method from a class inside a SurfaceListBox

From Dev

Accessing a file in a different Java class?

From Dev

Accessing instance method from class method

From Dev

accessing variable in a method to another method in same class

From Dev

Accessing an internal class from a different Dll file

From Dev

Accessing class method vairable in instance method

From Dev

Accessing method of different class in jsp

From Dev

accessing method of the class inside of async module or

From Dev

Java - Accessing a subclass constructor in a different class

From Dev

Accessing field of a different instance of the same class in Kotlin

Related Related

  1. 1

    Accessing JTextField in a different class and adding it to a String

  2. 2

    Single Jsp with different Action Class

  3. 3

    Accessing class variables from ActionListener method

  4. 4

    different signatures for accessing class methods

  5. 5

    Difficulty accessing class method during keystroke event

  6. 6

    Accessing a HashMap from a different class

  7. 7

    Accessing a file in a different Java class?

  8. 8

    Accessing volley response in another class/method

  9. 9

    Implement method for class in a different class

  10. 10

    Accessing an internal class from a different Dll file

  11. 11

    accessing method of the class inside of async module or

  12. 12

    Accessing method in a class from MainWindow

  13. 13

    Loop to beginning with JFrame, accessing method in different class

  14. 14

    accessing instance methods from class method proc

  15. 15

    Accessing Arraylist contents from a different class?

  16. 16

    Single Jsp with different Action Class

  17. 17

    Accessing class variables from ActionListener method

  18. 18

    Difficulty accessing class method during keystroke event

  19. 19

    Accessing public method of another class

  20. 20

    Accessing a public method from a class inside a SurfaceListBox

  21. 21

    Accessing a file in a different Java class?

  22. 22

    Accessing instance method from class method

  23. 23

    accessing variable in a method to another method in same class

  24. 24

    Accessing an internal class from a different Dll file

  25. 25

    Accessing class method vairable in instance method

  26. 26

    Accessing method of different class in jsp

  27. 27

    accessing method of the class inside of async module or

  28. 28

    Java - Accessing a subclass constructor in a different class

  29. 29

    Accessing field of a different instance of the same class in Kotlin

HotTag

Archive