Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: useBean defined by URL Parameter?


Message #1 by Adam Winter <awinter@a...> on Wed, 07 Feb 2001 14:07:46 -0600
useBean tags are not really meant to be dynamic.  That is, you can't 
dynamically change the class of the bean.  However, one approach I have 
used is to have the bean be a _holder_ of some other object which is 
dynamic.  

I used this approach when I wrote a JSP include that would take a vector 
of strings and generate the <option> tags for a selection list.  Instead 
of writing the same loop over and over again, I include the jsp module 
that generates the options and replace the contents of the holder bean.

For example:  (some detail omitted for clarity)

<jsp:useBean id="options" class="jsp.components.SelectContextHolder" 
scope="session"/>
<% 	SelectContext colors = new SelectContext(), states = new 
SelectContext();
%>

<!-- Swap the first SelectContext into the SelectContextHolder.  -->
<%	options.setContext(colors); %>
<select name="selectedColor">
<jsp:include page="options.jsp" flush="true"/>
</select>

<!-- Swap the second SelectContext into the SelectContextHolder.  -->
<%	options.setContext(states); %>
<select name="selectedState">
<jsp:include page="options.jsp" flush="true"/>
</select>

You can take the same approach, programmatically altering the contents of 
the holder bean based on some application logic.

> Hey all,
> 
> I'm trying to write a page that uses the useBean tag to load a class, 
whose 
> value is defined as a parameter in the url. Here's my code that doesn't 
work:
> 
> <jsp:useBean id="bvdSelector" scope="request" 
> class=request.getParameter("model") />
> 
> where my URL is like: thefile.jsp?model=com.atoutcome.JavaBean
> 
> Any ideas on how to deal with this?
> 
> -adamw
> 

  Return to Index