Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

duplicate bean setters in JSP Page  RSS feed

 
Harshad Patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Does any one know if its possible to specify
a JSP which uses 2 beans of the same class
but both containing different data

code >>
<%@page import="java.util.Date" session="true" %>
<jsp:useBean id="verifyloggingBean" scope="session" class="com.nexor.LoggingBean"/>
<jsp:setProperty name="verifyloggingBean" property="*" />
<jsp:useBean id="secureloggingBean" scope="session" class="com.nexor.LoggingBean"/>
<jsp:setProperty name="secureloggingBean" property="*" />


How can I distinguish that
one INPUT="logLocation" has to go to verifyLoggingBean
and one INPUT="logLocation" has to go to secureLoggingBean

Thanks
harshad
 
Sumitro Palit
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshad,

What you did will work.
Look at the id attribute of jsp:useBean as the variable name of the bean instance.
So you have created 2 new instances of com.nexor.LoggingBean,
have one reference to each - verifyloggingBean & secureloggingBean
- and you have added these as attributes to the session.
verifyloggingBean & secureloggingBean refer to 2 different instances of com.nexor.LoggingBean and thats what you were trying to achieve.

- ortimuS
 
Harshad Patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ortimus

It works to a point, the default information from each beans
using getProperty works - i.e. each beans distinct information is displayed

however the automatic setting of the property doesnt - the second bean gets set with the values of the first ??
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's possible... as your code example shows - they are the same class, but each instance has a different name or id (as appropriate to the tag).

It sounds like your real problem is that you have request parameters that have the same name, and you want to ensure that different values go to different beans... the easiest way to fix this is to use different names for the request parameters and have another setProperty tag using the specific parameter name for the specfic bean...

i.e. instead of having two parameters named 'logLocation' in the same request, have one named 'secureLogLocation' and another named 'verifyLogLocation'. Then you can have JSP code like this to process the request -



All the other attributes of each of the LoggingBeans get set by using '*', but setLogLocation() never gets called, because a request parameter of that name doesn't exist in the request. If a request pararameter doesn't have a name that fits in with the getters and setters of the bean, then it just gets ignored. So you have to specify on the next line which request parameter holds the value for which bean.
 
Harshad Patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh, so the line
<jsp:setProperty name="secureloggingBean" property="logLocation" param="securelogLocation"/>

indicates that the property is logLocation, but the paramater in the request is actually going to be secureLogLocation
so how does this result in a call to setLogLocation of the appropriate bean ? where is the mapping stored ?
 
Sumitro Palit
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx Nate,

I didnt read Harshad's original question too carefully.
Thx for the idea to use property="*" to map common properties and add params to the request for different values that need to get mapped to the same property on 2 different instances.

- ortimuS
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshad,

The mapping isn't stored anywhere... finding the property is just a mix of the jsp:setproperty tag and the Reflection API...



Lets dissect this tag -

<jsp:setProperty - says this is going to be a setSomething method call.

name="secureloggingBean" - provides the instance name of the specific bean.

property="logLocation" - finishes the method name to call. Since this is a "set", we're going to call "setLogLocation" on the bean.

param="securelogLocation" - pass the value of the request param named "securelogLocation" into the method specified on the bean specified. If you use '*', the tag assumes the property names will match the request param names.
 
Harshad Patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent - thanks for that explanation
It works now & I understand it
Cheers
Harshad
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!