File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How do I Call a Bean Function Using JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How do I Call a Bean Function Using JSP?" Watch "How do I Call a Bean Function Using JSP?" New topic
Author

How do I Call a Bean Function Using JSP?

Lindsay Fester
Greenhorn

Joined: Nov 05, 2010
Posts: 9
I need to call my Bean's method, but I don't know how to do that without using EL. I don't want to use EL because it's bad coding practice.



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Lindz Zdnil wrote:I need to call my Bean's method, but I don't know how to do that without using EL. I don't want to use EL because it's bad coding practice.

You've got two things wrong:
  • You cannot use the EL to call general bean methods. Only bean properties can be accessed.
  • Using the EL is most certainly not a bad coding procatce -- quite the opposite. The EL is the preferred way to add dynamic data to JSPs. Who told you that?


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Lindsay Fester
    Greenhorn

    Joined: Nov 05, 2010
    Posts: 9
    I meant using <% %> with straight Java code, not EL. So how would I call a method? I'm new to JSP and having a lot of trouble with my assignment.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61010
        
      65

    If you need to call a bean method, it's a sign of a design problem. Either you are trying to do something in the JSP that should be handled by the page controller, or your bean has a design problem. Beans should have getters and setters -- general methods violate the bean pattern.
    Lindsay Fester
    Greenhorn

    Joined: Nov 05, 2010
    Posts: 9
    Basically I have an assignment where I need to create a dynamic table that represents a time sheet. Employees can login, makes changes to time sheets, and save them to the remote database. Employees can also click "Add a Row" to add a new row to the time sheet. So I've created a bean to represent the table with an 2D array list of TSCells. Each TSCell object has coordinates (as an ID) and data. In the JSP page, I create and populate an HTML table filled with text inputs.

    At the moment I'm having 2 issues:

    1. Saving to the column value because I can't figure out how access the TSCell's setData() method in JSP because it's in an ArrayList, there's no "bean" I can use for it. Is there a way to reference an existing bean? Can I use EL to access it?

    2. Every time I add a row by clicking the "Add Row" button, the form action sends the input to the "ts_process.jsp" page, which forwards back to the original. The row is added, but I can't add additional rows. I seem to be stuck on the ts_process.jsp page or something.

    3. I still can't find a way to call the bean method "Add Row" without using scriptlets (<% tags with pure Java). Is there a way? Or is it just poor design as you've said?

    I'm not looking for someone to do my assignment for me, I want to learn. I just can't seem to find specific answers to my questions online. So how would I go about solving these issues? Is it possible? Should I change my design from a table class to multiple objects (e.g. have time sheet entry objects with data members for each column)?
    Wendy Gibbons
    Bartender

    Joined: Oct 21, 2008
    Posts: 1107

    what are you learning in your course? have you been taught about servlets?
    as if i was doing it i would look into a form/servlet call for add row, but i am pretty new to this myself
    Lindsay Fester
    Greenhorn

    Joined: Nov 05, 2010
    Posts: 9
    I'm calling this one solved. The only way to call a bean method is to use EL. As for the rest, I made some minor mistakes with my code that have been corrected (i.e. accidentally setting the scope to request instead of session).

    Thanks to all who replied.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: How do I Call a Bean Function Using JSP?