Results 1 to 6 of 6

Thread: Getting objects from model

  1. #1
    Join Date
    Aug 2004
    Location
    Whitehorse, YT, Canada
    Posts
    26

    Default Getting objects from model

    I have a JSP view which is passed a model consisting of several business objects. I have logic defined within my business objects and can access them through "is" methods, ie: isClosed() : boolean.

    How do I obtain these objects from the model so that I can then perform a test of the object's state? For example, in my controller I create a Map consisting of an object Car and this is passed to my JSTL view via the ModelAndView object. I need to display a link in my view only if Car.isRunning() is true.

    I've tried <c:if test="${model.car.isRunning()}">...</c:if> but that didn't work. I'm new to JSP and JSTL programming, so I don't know if this is a Spring or a JSTL question.

    Thanks!
    smv

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    I think the problem may be in the "model" prefix in your variable path. Where did that come from? Remember the model map is a set of name=value pairs. Before a jsp page is rendered, the name=value pairs are injected as attributes in request scope. So in your jsp page, you simply access each request attribute by name; for example:

    Code:
       &#91;in controller code, prepping the model content for the view&#93;
         modelMap.put&#40;"myCar", new Car&#40;"Grand Marquis"&#41;&#41;;
         ...
       &#91;in jsp page&#93;
        <c&#58;if test="$&#123;myCar.isRunning&#40;&#41;&#125;>...</c&#58;if>
    HTH!

    Yes, I really do drive a Grand Marquis ;-)
    Keith Donald
    Core Spring Development Team

  3. #3
    Join Date
    Aug 2004
    Location
    Whitehorse, YT, Canada
    Posts
    26

    Default

    Hi Kevin!

    The "model" came from the name of the Map when I was constructing the ModelAndView object: return new ModelAndView("viewPage", "model", model);

    I tried both <c:if test="${myCar.isRunning()}"> and <c:if test="${model.myCar.isRunning()}"> and I receive a similar ServletException each time: The function isRunning must be used with a prefix when a default namespace is not specified.

    I've tried to Google the error message, but I've come up with dead ends. Just to be sure, this is how I am creating my model and returning it:
    Code:
    Map model = new HashMap&#40;&#41;;
    model.put&#40;"myCar", new Car&#40;"Grand Marquis"&#41;&#41;;
    return new ModelAndView&#40;"myView", "model", model&#41;;
    Thanks again!
    smv

  4. #4
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    JSTL doesn't support calling methods explicitly. However, you should be able to access getters and boolean getters (i.e. isProperty()). Just remove the get/is:

    <c:if test="${myCar.running}">

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Thanks Matt I didn't even seen that ;-) smv that definitely sounds like the other half of your problem!
    Keith Donald
    Core Spring Development Team

  6. #6
    Join Date
    Aug 2004
    Location
    Whitehorse, YT, Canada
    Posts
    26

    Default

    Yes! That worked perfectly! Thank you both for helping me on this issue. I need to read more about JSTL as I thought it only worked on "get" accessors.

    smv

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Replies: 3
    Last Post: Apr 21st, 2005, 03:19 PM
  3. Content Provider vs View Model
    By Martin Kersten in forum Swing
    Replies: 21
    Last Post: Mar 10th, 2005, 02:25 PM
  4. Domain model objects in session
    By jaypee in forum Architecture
    Replies: 3
    Last Post: Feb 10th, 2005, 02:16 PM
  5. How to make domain model objects observables ?
    By baptiste in forum Architecture
    Replies: 1
    Last Post: Nov 15th, 2004, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •