File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Forwarding from form or action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Forwarding from form or action" Watch "Forwarding from form or action" New topic

Forwarding from form or action

Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
I have a jsp which accepts input and the action class calls another service class which searches the database and returns a resultset. But, if the resultset is empty, I need to redisplay the input jsp page with an error message. I do not want to call the service method in the form but once I get to action class, how do I go back to input jsp with a message ? I am sorry if this is a really basic question. I am just not sure how to design this. Thanks.

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here is the process:
  • In your Struts-config.xml, Create a local forward for this action to the input jsp.
  • In your file, create an entry for the message you want to display
  • In your input jsp, place the tag <html:errors/> where you want the message to appear
  • In your action class, Instantiate an object of type ActionErrors
  • If the resultSet is empty, instantiate an object of type ActionMessage, using the constructor that requires a message key. (Use the key of the message you set up in the 2nd step above)
  • Add this object to the ActionErrors using the add() method.
  • Call the saveErrors() method provided by the superclasss
  • Forward to the input JSP.

  • [ March 23, 2005: Message edited by: Merrill Higginson ]

    Consultant, Sima Solutions
    Ritu varada
    Ranch Hand

    Joined: Sep 08, 2004
    Posts: 117
    Thank you, Merrill. That works!
    I agree. Here's the link:
    subject: Forwarding from form or action
    It's not a secret anymore!