aspose file tools*
The moose likes JSP and the fly likes Bean population Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Bean population" Watch "Bean population" New topic
Author

Bean population

Nelson David
Greenhorn

Joined: Oct 01, 2002
Posts: 13
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
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
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

Joined: Oct 01, 2002
Posts: 13
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
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
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
Rick Hightower
Author
Ranch Hand

Joined: Feb 20, 2002
Posts: 350
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.


Rick Hightower is CTO of Mammatus which focuses on Cloud Computing, EC2, etc. Rick is invovled in Java CDI and Java EE as well. linkedin,twitter,blog
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean population