• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bean population

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am new to Javaranch and this is my first message, It may be very simple for many of you. I am developing web application using 3tier architectutre, I have Beans corresponding to each JSP page, like sendmail.jsp have SendMailBean.java, the problem is that my bean is not getting populated, i have used <jsp:useBean id="bean" class="PBMailBean" scope="session"/>
<jsp:setProperty name="bean" property="*"/>
on my sendmail.jsp page and wants my bean to be populated by the data user enter in form, my bean attributes matches exactly as fields on form, but the bean is not getting populated(setters methods are not being callled), I need this bean to be passed to my BusinessObjects, which is getting null in all beans attributes.
What should I do to get my bean populated.
regards
Nelson
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you are using session scope make sure that you're bean implements Serializable (or switch to request scope). This might be the problem since anything in a scope beyond request must be serialbizable to retain state.
Sean
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one catches a lot of people. In your useBean tag you MUST specify a complete package for the bean class. If your bean is not now in a package, go back and put it in one. Generally speaking, using default package for any code used in servlets and JSP is going to be a source of problems.

Thats because the JSP engine uses reflection to generate the bean instance and it needs the complete package.class name. You may also have to import com.mypkg depending on how you use the bean.
Bill
 
Nelson David
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sean and Brogden for reply, my problem is different one, in my case I have a single servlet(ServletController) which is being hit for by all the pages such as "login" or "sendmail" requests. These requests are declared as a hidden variable on form, when a requests comes to ServletController it gets the hidden variable from request and then loads a class corresponding to a servlet and calls its execute method. (for example if request is "sendmail" it loads ACSendMail class and invoke its execute method)this execute method then calls appropriate business class method (BOSendMail.sendMail),
NOW I want the bean to be passed into the business method as a parameter.
Which is empty in my case. I know that bean is populated from form request variables, but I dont want to make a seperate JSP page for populating bean from request. What I want is that when my Servlet Controller is hit , it populates the corresponding bean automatically.
regards
Nelson
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand you correctly, there is no method in the servlet API to do the equivalent of JSP's automatic population of bean variables.
Obviously you can do this manually by using getParameter and your bean methods. I suppose you could look at the JSP source code and copy that.
Bill
 
Author
Posts: 350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jakarta has a lib called bean utils, that does stuff like this.
Check it out at....

BeanUtils


Most Java developers are used to creating Java classes that conform to the JavaBeans naming patterns for property getters and setters. It is natural to then access these methods directly, using calls to the corresponding getXxx and setXxx methods. However, there are some occasions where dynamic access to Java object properties (without compiled-in knowledge of the property getter and setter methods to be called) is needed. Example use cases include:
Building scripting languages that interact with the Java object model (such as the Bean Scripting Framework).
Building template language processors for web presentation and similar uses (such as JSP or Velocity).
Building custom tag libraries for JSP and XSP environments (such as Jakarta Taglibs, Struts, Cocoon).
Consuming XML-based configuration resources (such as Ant build scripts, web application deployment descriptors, Tomcat's server.xml file).
The Java language provides Reflection and Introspection APIs (see the java.lang.reflect and java.beans packages in the JDK Javadocs). However, these APIs can be quite complex to understand and utilize. The BeanUtils component provides easy-to-use wrappers around these capabilities.


I know we used this to do similar things.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was facing the same problem. You can use the following code for same
/**
* This method populates bean object properties from the request
* parameters having same name. Hence it is equivalent of
* <jsp:setProperty name="beanId" property="*"/>
* @param HttpRequest The servlet request containing parameters
* @param Bean object implementing Serializable
*/

public void populateBean(HttpServletRequest request, Serializable beanToBePopulated)
{
Enumeration enum = request.getParameterNames();
String name;
String[] values;
int i = 0;
while (enum.hasMoreElements())
{
name = (String)enum.nextElement();
values = request.getParameterValues(name);
for (i = 0; i < values.length; i++)
{
try
{
Class beanClass = beanToBePopulated.getClass();
Class fieldClass = beanClass.getDeclaredField(name).getType();
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = beanClass.getMethod(methodName, new Class[]{fieldClass});
method.invoke(beanToBePopulated, new Object[]{values[i]});
}
catch (NoSuchMethodException e){}
catch (java.lang.reflect.InvocationTargetException e){}
catch (IllegalAccessException e){}
catch (NoSuchFieldException e){}
}
}
}
Comments welcome.
Regards,
Bhushan
 
reply
    Bookmark Topic Watch Topic
  • New Topic