How do I do this?

B

Bob Rivers

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
 
J

jeak110

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top