File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Display database values in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Display database values in JSP" Watch "Display database values in JSP" New topic
Author

Display database values in JSP

kashwini Kulkarni
Ranch Hand

Joined: Aug 07, 2009
Posts: 63
Hello,

I am developing an application with struts,hibernate and JSP.
I have an action class where I am using criteria to retrieve value from database.
I need to show that particular value in my JSP page.
Currently I am using the following : -

ActionClass

Criteria criteria=session.createCriteria(Product.class);
ProductForm prdForm=criteria.list(); (which returns only one record)
request.setAttribute("myObj", productForm);

My JSP page

<bean:write name="ProductForm" property="Id" />

Is this a correct way to displaye values in JSP using struts tags?


Thanks in Advance
Kashiwni
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51

Hi Kashwini,

Firstly, when you are writing criteria.list(), collect it in a list instead of form object.

a. List<ProductForm> productFormList = criteria.list();

Going ahead if you are sure of receiving only one record, you can do a get(0) on the list as

b. ProductForm productForm = productFormList.get(0);

Next, you do not have to save it in session. On your JSP, you can directly iterate over productFormList (if you retrieve multiple rows), and display the values using <bean:write>, else directly use the form from line b. (if you are getting only one row), to display the values.

Hope this helps.

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
kashwini Kulkarni
Ranch Hand

Joined: Aug 07, 2009
Posts: 63
hi,


Thanks a lot for you solution.I will surely try this.
I was able to iterate over a list(when having multiple records) but I was not able to understand how will I proceed with a single record.


Thanks once again.




Regards,
Kashwini



kashwini Kulkarni
Ranch Hand

Joined: Aug 07, 2009
Posts: 63
Hello Komal,


I tried your solution, but I get a ClassCastException.

My code is as follows (Action class): -

Criteria criteria=session.createCriteria(Product.class);
List<ProductForm>productList=criteria.list();
and then,

ProductForm prodFormObj=productList.get(0);

gives me a ClassCastException as my criteria returns me list of objects of type Product(Which is my POJO)
how can I cast it to my form class object (ProductForm) ?

Further my Product class has instance variables which are objects of some other class.

Class Product{
private int productId;
private String productName;
private Part part;
}

how can I retrieve object of type Part from the list (or single record) returned by Criteria?




Thanks in Advance
Kashiwni

Mohan rvscas
Ranch Hand

Joined: Jul 03, 2010
Posts: 39
gives me a ClassCastException as my criteria returns me list of objects of type Product(Which is my POJO)
how can I cast it to my form class object (ProductForm) ?




how can I retrieve object of type Part from the list (or single record) returned by Criteria?


your bean should have getter and setter method / the variable should be public.
like

kashwini Kulkarni
Ranch Hand

Joined: Aug 07, 2009
Posts: 63
Hello,

Can anyone please suggest me (or explain me) a link having proper example of <bean-write>?
I searched for the net and got many different examples but none up to the mark.

<bean-write name="myForm" property="myProperty">

What should be myForm?
Name of my Form bean or object of my Form bean created in my Action Class


I am really not getting this.
It will be very helpful if someone helps me out of this.




Thanks in Advance
Kashwini
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51
myForm would be the 'id' of the <jsp:useBean>. This is one of the ways, however in general 'name' specifies the attribute name of the bean whose property is accessed to retrieve the value specified by property (if specified). If property is not specified, the value of this bean itself will be rendered.

This link is the tag library for the same:
http://struts.apache.org/1.x/struts-taglib/tlddoc/bean/write.html

Also, if you want details about struts-bean, please refer : http://struts.apache.org/1.2.x/userGuide/struts-bean.html

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display database values in JSP
 
Similar Threads
form resubmission
Retrieving Cookies After actionMapping.findForward("")
Retrieving Cookies After actionMapping.findForward("")
logic:iterate Tag
Passing Atrribute Object From Jsp To Action