Setting a Bean Property in the JSP
Hello,
Can someone please point out my error here.
<jsp:useBean class="org.trollope.DbAccess" id="dba"> </jsp:useBean>
<jsp:setProperty name="dba" param="user" property="jdbcUser" ></jsp:setProperty>
Bean property is: <jsp:getProperty name="dba" property="jdbcUser"></jsp:getProperty>
The value displayed is always null.
I have verified that the property String value is available in the "user" parameter in the request.
If I set thejdbcUser value in the Bean, the value is returned properly in the JSP.Evidently, I am misunderstanding how something should work here.
The eventual goal is to be able to use a database connection in the JSP without using scriptlets.
Thanks.
mp
# 2
> Does your DbAccess bean have a "setJdbcUser" method?
> ie public void setJdbcUser(String user)
Yes. Wouldn't the 'setProperty" tag cause compilation to fail if there was not a corresponding bean property?
> I'll add the standard disclaimer how db connections
> in JSPs is a bad idea. Such code should be in a servlet/bean.
I have created context parameters in web.xml with the jdbc configuration values to create the connection. I created a context listener to open the connection on application initialization. My original conception was to create the connection in my controller servlet and then pass it as a request attribute and fetch it in the JSP. But, on further consideration, I could not find a way to do that without using a scriptlet. For that reason, I backtracked and created the DbAccess bean, whose purpose actually is to create the connection.So, the business with the jdbcUser property is really just me "feeling my way" with how these tags work in the JSP. I'm having difficulty in visualizing how to access that connection (or the data feed) in the JSP. I can create the DbAccess object with the JDBC connection in the listener.
I am certainly open to suggestions as to the best way to make this work. I haven't found much guidance in the documentation I've read.
Thanks for the reply.
mp