• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

PRoblem in displaying ActionForm

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all... I am using struts to design an online Library system for my organization. I have an action class called ManageBooks.java which extends DispatchAction. It has 2 mthods view() and addNew() which are actually configured with parameter name "action" in my struts-config.xml file. There are 2 links in the books home page "View" and "Add". My idea is when I'll click Add link, it should display a blank form (name is BookForm) and let me add a new book. But its not working. Can anyone tell what could be the problem ??...

My struts-config file contains these entries...

<action-mapping>
<action path="manageBooks"
type="lib.books.ManageBooks"
name="bookForm"
validate="false"
scope="request"
parameter="action"
input="/index.jsp">
<forward name="success" path="/books/index.jsp" />
</action>
<action path="saveBooks"
type="lib.books.SaveBooks"
name="bookForm"
validate="false"
scope="request"
input="/index.jsp">
<forward name="success" path="/books/index.jsp" />
</action>
</action-mapping>

My problem is the BookForm is not displayed. I have checked that the addNew() method is called properly, but the Form is not displayed. Kindly help me.

Thanks in advance.

N. Nanda
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the form is not displayed, then what is? Is there any exception thrown (check server console)? Some more information about the problem would be useful.

Check your action class code to verify the ActionForward being returned. You have only one local forward ("success") for both adding and viewing?

Sheldon Fernandes
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But its not working.



Ah, the tech support question: "What does 'not working' look like?"

By your struts-config, you should get either a blank screen because the forward you wanted was not defined or you are getting the index page because that is what "success" is pointing to.
 
N Nanda
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies. I get a blank screen. Kindly tell me in detail what could be the problem. Thank you in advance.
 
N Nanda
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for their responses. Here I am putting part of my coding. Kindly somebody check it and help me to solve my problem.


struts-config.xml
===========

<form-beans>
<form name="bookForm" type="lib.book.BookForm" />
</form-beans>

<global-forwards>
<forward name="home" path="/index.jsp"/>
<forward name="bookHome" path="/books/index.jsp"/>
</global-forwards>

<action-mappings>
<actionpath="/manageBook"
type="lib.book.ManageBook"
name="bookForm"
validate="false"
scope="request"
parameter="action"
input="/books/index.jsp">
<forwardname="success"path="/books/index.jsp"/>
</action>

<actionpath="/saveBook"
type="lib.book.SaveBook"
name="bookForm"
validate="false"
scope="request"
input="/books/index.jsp">
<forwardname="success"path="/books/index.jsp"/>
</action>
</action-mappings>

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

ManageBook.java
================

package lib.book;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import java.util.ArrayList;

public class ManageBook extends DispatchAction
{
public ActionForward view( /* The usual arguments */...) throws Exception
{
String action = request.getParameter("action");

if(action == null) {
action="view";
}

if("view".equalsIgnoreCase(action)) {
/*
*Here I have retrieved all existing books from database and stored in an arraylist
*ArrayList allBooks=new ArrayList();
*/

request.setAttribute("action", action);
request.setAttribute("books", allBooks);
}

/* Here I have removed any FormBean if it is created. */

return mapping.findForward("success");
}

public ActionForward addNew( /* The usual arguments */...) throws Exception
{
String action = request.getParameter("action");

if(action == null) {
action="addNew";
}


if("addNew".equalsIgnoreCase(action)) {
request.setAttribute("action", action);
}

return mapping.findForward("success");
}
}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

SaveBook.java
================

package lib.book;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class SaveBook extends Action
{
public ActionForward execute( /* The usual arguments */...) throws Exception
{
Book book= new Book();
BookForm bookForm = (BookForm)form;

String action=bookForm.getAction(); /* This I send from the form as a hidden field. */
String target="error";

book.setBookId(bookForm.getBookId());
...
...
...

if("addNew".equalsIgnoreCase(action)) {
target=doSave(request, book);
}
else if("edit".equalsIgnoreCase(action)) {
target=doUpdate(request, book);
}

return mapping.findForward(target);
}

private String doSave(HttpServletRequest request, Book book) throws Exception
{
String target = BookDAO.addBook(getDataSource(request), book);
return target;
}

private String doUpdate(HttpServletRequest request, Book book) throws Exception
{
String target = BookDAO.updateBook(getDataSource(request), book);
return target;
}
}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

/books/index.jsp
===============
...
...
...

<logic:equal name="action" value="view" scope="request">
<template ut name="content" content="/books/viewBooks.jsp" />
</logic:equal>

<logic:equal name="action" value="addNew" scope="request">
<template ut name="content" content="/books/addBooks.jsp" />
</logic:equal>
...
...

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

/books/addBooks.jsp
===============
...
...
...

<html:form action="saveBook" focus="bookId">
<html:hidden property="action ... ... />
...
...
...
</html:form>

==========================

Thanx in advance.

Niranjan Nanda
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After looking over what you've got I'm surprised you are getting a blank screen. I would have expected a 404 error.

Could you verify for me if the screen gives a 404 or if it is COMPLETELY blank? Also, when your page looks this way, what does it read in the Address bar of the browser?
 
N Nanda
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Marc for your response. In my address bar I am getting
http://localhost:8080/manageBook?action=addNew

But a blank screen in place of content area. I am not sure about your skepticism regarding 404-error, but I am getting a complete blank screen. And if I am disabling the <html:form ..> part in my /books/addBooks.jsp page then I am getting that page easily, I mean no blank screen, the page is coming ok. I have few other things in that page except that Book adding form like I have displayed a heading "Add A Book" and this header is coming properly if I am commenting out that <html:form ... > part. But If that form part is uncommented, I am getting a blank screen.

Please help me. Its really urgent and please if possible add some comments where I could be going wrong.

Thank you again.

Niranjan Nanda.
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's an exception thrown!!!??? It would have helped to know that. It surprises me that the top line of the trace did not print out on the jsp.

Please let us see the trace.
 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nanda

This means there is some problem with attributes which are coming as null....pls check the logs of the application u will surely have got some stack trcae there and doe let me know about that

i can give a clue on t hat
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nanda

<html:hidden property="action ... ... />

Here lies the problem i feel ....does ur formbean have the set and get methods for this paramter like getAction and setAction() becouase if u use a hidden variable inside a form and use property attribute then it will try to look up in the formbean associated with the <html:form action...........

so i hope u havent missed this and the reaason why it works with out form is then it will look up for that attribute in request scope
 
N Nanda
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx srinath.. but i have this filed in my BookForm with getters and setters properly set for it. Can you point out any other problem ?? Mr. Marc. answered that I should get 404-error, but I didn't understand his point clearly. Kindly tell me if you can find any other problems. I'll give you the stack trace that could have appeared in the log file.

Thank you again.

Niranjan Nanda
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi nanda

better u could send the stack trace which appeared after performing this operation as this is not live and its tough to track
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There

At a glance it looks like you ar emissing your .do...

Instead of http://localhost:8080/manageBook?action=addNew

Shouldn't your address look like this?
http://localhost:8080/manageBook.do?action=addNew

Hope that helps
Kim
 
N Nanda
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc and Srinath. I checked my web server log and the trace was showing that it was failing to get the definition of BookForm. Then I checked my struts-config.xml and there I had a wrong classpath. However, even after I changed the classpath it wasn't working. Then I created that BookForm class again and after that it worked as I require. But thank you both for giving me the idea of tracing the webserver log. Sorry if I have disturbed you for this small problem. I'll get back to you if I'll face any problems.

Thank you again.

Niranjan Nanda.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic