JSP: Beans from factories?

K

Kai Grossjohann

I have an object which has a factory method which returns objects that
I would like to use as beans in JSPs.

<jsp:useBean id="foo" class="blarfl"/> will call the constructor of
the blarfl class, though, so that's not what I want.

I can use a scriptlet along the lines of

<% Factory fact = new Factory();
Blarfl b = fact.findBlarfl(); %>

But then I don't know how to make b into a bean such that I can use
<jsp:setProperty/> and friends on it.

Maybe it works to use <c:set/>? How to do that?

Kai
 
C

Chris Riesbeck

Kai Grossjohann said:
I have an object which has a factory method which returns objects that
I would like to use as beans in JSPs.

<jsp:useBean id="foo" class="blarfl"/> will call the constructor of
the blarfl class, though, so that's not what I want.

I can use a scriptlet along the lines of

<% Factory fact = new Factory();
Blarfl b = fact.findBlarfl(); %>

But then I don't know how to make b into a bean such that I can use
<jsp:setProperty/> and friends on it.

See item #7 of http://java.oreilly.com/news/jsptips_1100.html
 
K

Kai Grossjohann


It seems that they are telling me to do this:

<jsp:useBean id="foo" class="Foo" scope="request"/>
<%
foo = factory.makeFoo(parm1, parm2);
pageContext.setAttribute("foo", foo, PageContext.REQUEST_SCOPE);
%>
<jsp:setProperty name="foo" property="bar" value="42"/>

What if I can't say <jsp:useBean /> in the first line? My class Foo
does not have a no-arg constructor.

I'm not 100% sure but I seem to recall that just putting stuff into
the request scope was not sufficient for a subsequent <jsp:setProperty
..../> to succeed.

Kai
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top