aspose file tools*
The moose likes Struts and the fly likes I NEED TO DISPLAY A RESULTSET IN A JSP CORE TAG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "I NEED TO DISPLAY A RESULTSET IN A JSP CORE TAG " Watch "I NEED TO DISPLAY A RESULTSET IN A JSP CORE TAG " New topic
Author

I NEED TO DISPLAY A RESULTSET IN A JSP CORE TAG

Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
HI,
i have a project that i am working on which is based on struts and hibernate and i have created a bean class which i am using to retrieve some data and i want to display the value in the JSP but it is rfeturning a null pointer exception i guessed the error is from the bean...but cant figure it out any hel will be appreciated.
Here is my code



Laa
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You haven't included the JSP and you haven't said where the NPE occurs--it's going to be tough to help!

If the NPE is occurring in the code above then it's not really a Struts issue, but JDBC, for which there is a forum.
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
the jsp page contains only the get and set property for the bean which and the null pointer exception occurs at the getproperty tag

<<jsp:useBean id="c" scope="session" class="org.accounts.savings.struts.action.Idan" />
<h2><jsp:getProperty name = "c" property = "amount"/></h2>
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
in case what i am trying to achieve is not too clear what i want is to retrieve some data from database with resultset and display the content on JSP and i want to follow the structure of struts where jsp is only used for displaying
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Where is the code that creates org.accounts.savings.struts.action.Idan and puts it in the HttpSession?
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The code tags are primarily for... code.

Normally the action class would be responsible for putting objects into scope for the JSP page.

Which version of Struts are you using? The canonical ways for doing what you're doing are different for Struts 1 and Struts 2.
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Could you answer the questions I asked? (And really, save the 'code' tags for code.)
I wrote:Normally the action class would be responsible for putting objects into scope for the JSP page.

Which version of Struts are you using? The canonical ways for doing what you're doing are different for Struts 1 and Struts 2.
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
i am using struts 2...thanks for the concern i need to get this working asap
thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So... you're not actually trying to display a resultset in the JSP, rather the results of some calculations done on a resultset.

In Struts 2 data is usually exposed by creating an action property and displaying it via the <s:property> tag or JSP EL. Personally I'd just create an "amount" property and set it to the results of the calculations done in the bean.
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
hi David,
i would really appreicate it if you would demonstrate it using a simple code to display the results of some calculations done on a resultset
thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Action:

JSP:

Config:

That's about it.
Ahmed Oladele
Greenhorn

Joined: Mar 06, 2009
Posts: 8
Thanks David,
the Config is it the struts config and FooAction class where do i place it is it under struts action.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not really sure what you just said, but the configuration goes in the struts.xml configuration file inside a package definition. The class should be a fully-qualified Java class, wherever you implemented it.
rk palleru
Greenhorn

Joined: Feb 26, 2009
Posts: 22
the problem you have declared "int amount" as a local variable same variable you are returning .so that it returns like that.
instead of that declare "amount " variable starting of method ,assign to the variable and return the same variable amount
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What?
rk palleru
Greenhorn

Joined: Feb 26, 2009
Posts: 22
1.
2.
3. public int getAmount()
4. { int temp=0;
5. int test=this.getAccountId();
6. try
7. {
8. Class.forName("com.mysql.jdbc.Driver");
9. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/allovermfb","root", "");
10. PreparedStatement stmt= con.prepareStatement("SELECT AMOUNT FROM SAVINGS_ACTIVITY_DETAILS WHERE ACCOUNT_ID =? AND AMOUNT >200000 AND Flag =1 AND ACCOUNT_ACTION_ID =6 AND Treated =0 ORDER BY created_date DESC");
11. stmt.setInt(1, test);
12. ResultSet results = stmt.executeQuery();
13. while(results.next())
14. {
15. temp= results.getInt("AMOUNT");
16.
17. }
stmt.close();
18. }catch(Exception e)
19. {
20. System.out.println("listing erore" + e);
21. }
22. return temp;
23. }
24.
25. public void setAmount(int amount){
26. this.amount=amount;
27. }
28. }
rk palleru
Greenhorn

Joined: Feb 26, 2009
Posts: 22
but one thing here you are overwriting database values to "temp " variable.so final value which is in the result object only returns .otherwise use some "if" condition in while loop
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@rk: Please use code tags when posting code.

The OP asked about getting data back to the JSP, which is what I addressed. Without knowing anything more about the contents of the database the logic in getAmount() doesn't affect that part of the question--it's up to the OP to determine if the logic being used to generate the data is correct, which isn't really related to Struts itself.
 
Consider Paul's rocket mass heater.
 
subject: I NEED TO DISPLAY A RESULTSET IN A JSP CORE TAG