How do I do this?

Discussion in 'Java' started by Bob Rivers, Jul 27, 2005.

  1. Bob Rivers

    Bob Rivers Guest

    Hi,

    I have an old jsp like this:

    <%
    MyObject myobject = new MyObject();

    String id = request.getParameter("id");
    if(id != null) {
    myobject.load(id);
    }
    %>
    .....
    <%= myobject.getName() %>

    The load method uses hibernate to do database access. The method looks
    like this:

    public void load(String id) {
    HibernateUtil.getSession().load(this, new Integer(id));
    }

    How do I convert it to jsp 2.0?

    I'm trying to do something like this, but it isn't working:

    <jsp:useBean id="myobject" class="mypackage.MyObject" />

    <c:if test="${not empty param.id}">
    <c:set var="objName" value="${myobject.load(param.id).name}"
    scope="page" />
    </c:if>

    My problem is how to use the load method. I don't know how to call it
    passing the id parameter.

    I was thinking about to change the method load to something like:

    public void setId(String id) {
    this.id = id;
    }

    public MyObject getMyObject() {
    MyObject loadedObject = new MyObject();
    HibernateUtil.getSession().load(loadedObject, new Integer(this.id));
    return loadedObject;
    }

    So, with this method I think that I could write a jsp like this:

    <jsp:useBean id="myobject" class="mypackage.MyObject" />

    <c:if test="${not empty param.id}">
    <jsp:setProperty name="myoject" property="id" value="${param.code}"
    />
    <c:set var="loadedobject" value="${myobject.load}" scope="page" />
    </c:if>

    I did not tested it (I did it while I was writing this), and I'm not so
    sure about it. And also, is possible, I don't want to change my
    classes, only my jsps...

    TIA,

    Bob
    Bob Rivers, Jul 27, 2005
    #1
    1. Advertising

  2. Bob Rivers

    jeak110 Guest

    hi Bob Rivers,
    Sorry, but I dont really see you reason for change you code. If you
    can do it like that
    <%
    MyObject myobject = new MyObject();

    String id = request.getParameter("id");
    if(id != null) {
    myobject.load(id);
    }
    %>
    why not? If you are really needed jstl ... use it.
    There is a good book that help me. It's 'Pro JSP, Third Edition' >>
    Simon Brown 1-59059-225-5
    try to find it here (ebuki.powernews.ru)
    jeak110, Jul 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page