aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Trouble with Struts 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Trouble with Struts" Watch "Trouble with Struts" New topic
Author

Trouble with Struts

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm working with the HF book and I've been having some difficulty getting the struts example to work. I'm using Struts 1.1, if that makes any difference. The web.xml and struts-config.xml files are mostly generated via my IDE (WSAD), but the source of those files seems to match what is in the book. Here are the web.xml and struts-config.xml files that I'm currently using:

web.xml


struts-config.xml


Unfortunately, I can get to my index page, index.html, but when I try to submit the form there, which has an action of SelectBeer.do, I get a blank screen. The URL changes to http://localhost:9080/Struts/SelectBeer.do, just as I'd expect, but I don't seem to be executing any of my code.

I even put some simple println statements in my form bean and action objects. None of them print. It's as if the ActionServlet isn't passing on execution to the other classes, at all.

Anyone have any ideas of what I can check?

Thanks,
Corey


SCJP Tipline, etc.
Bryan Basham
author
Ranch Hand

Joined: Apr 30, 2001
Posts: 199
Hi Corey,

I don't trust IDEs, so I would recommend that you do all HFS exercises by hand. It is possible that your IDE has corrupted something.

The only thing that I noticed that looked odd was in the web.xml file:

<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>

This should be:

<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

Notice the opening / on the file path. Struts might ignore it but it is something that you can fix easily and test. (unless your IDE prevents you from fixing it

Good luck,
Bryan
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Yeah, I'm not a big fan of IDE's, either, but they do make life easier sometimes. I had already tried adding the slash, but that didn't seem to make any difference.
Dan Gun
Greenhorn

Joined: Aug 06, 2003
Posts: 26
If you haven't done it, I'll put some debugging statements in the validate method of the form object as you form will be validated with each submission.


SCJP 1.4
Anurag Saksena
Greenhorn

Joined: Jul 20, 2004
Posts: 28
Corey,

What is the Exception you are getting on the console ?

-a.
==========
SCJP 1.4
SCWCD 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Anurag Saksena:
Corey,

What is the Exception you are getting on the console ?

-a.
==========
SCJP 1.4
SCWCD 1.4


There is no exception being printed to the console - that's part of my problem. There's no error that I can see, but the ActionServlet never seems to forward the request to any other objects, neither my Form Bean nor my Action class. I've put debugging statements into those classes and even put breakpoints in them, but I've hit none of them.

This is what I see...

1. I go to index.html - renders properly
2. I submit the form, which has an action of SelectBeer.do
3. I get a blank screen in my browser (with the URL changed to /SelectBeer.do). There are no errors that I can see, but nothing seems to be happening, either. I do, however, see the following tracing statements in the console:


action: Processing a POST for /SelectBeer
action: Setting locale 'en_US'
action: Looking for ActionForm bean under attribute 'selectBeerForm'
action: Creating new ActionForm instance of class 'edu.mayo.strutstraining.BeerSelectForm'
action: Storing instance under attribute 'selectBeerForm' in scope 'request'
action: Populating bean properties from this request
action: Validating input form properties
action: No errors detected, accepting input
action: Looking for Action instance for class edu.mayo.strutstraining.BeerSelectAction
action: Double checking for Action instance already there
action: Creating new Action instance


From that, it would appear that the ActionServlet is getting the request, but it never seems to make it to my Form Bean or Action Class.
Nitish Bahadur
Ranch Hand

Joined: Aug 25, 2003
Posts: 118
1. In your struts-config.xml under <action-mappings>, write a new action called: <action path="/HelloWorld" forward="/HelloWorld.jsp"/>

2. Create HelloWorld.jsp at the web-root of your Struts Web App.
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Hello World!</h2>
</body>
</html>

3. Stop and start your App Server. (Test Environment in case of WSAD)

4. What do you see when you access http://localhost:9080/Struts/HelloWorld.do thru a browser. (Tells us if Struts Controller is forwarding the request or not)

5. If you still see a blank page, then try accessing the page directly.
http://localhost:9080/Struts/HelloWorld.jsp
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Interesting, Nitish.

I did just what you said and I do see the HelloWorld.jsp file going through the ActionServlet. So, I know that, in this case, at least, the ActionServlet is performing the forward. So what does that tell me about the other case, when I'm going to my own Form Bean and Action Class? Is there error in there? If that's the case, how come I'm not seeing any of the debugging statements and/or hitting and of the breakpoints I have set in those classes?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Here's a new wrinkle I'll add...

In the console, I'm currently seeing these lines when I try to execute my app:



So, what does that tell us? First of all, I can see that the struts-config file is being read properly and it has located the correct class to do form validation (my Form Bean, 'edu.mayo.strutstraining.BeerSelectForm'). Secondly, it appears that no errors are being found and that it then goes on to try to create an Action instance.

I found this odd simply because I put a breakpoint in my Form Bean so that execution would stop at that point, but I never hit that breakpoint. So, I modifier the code in my Form Bean to look like this:



You can see that I commented out some code in my validate() method so that I should always have an error with my form parameters. However, when I execute my app again, I get the same output in the console, showing no errors found in the form parameters. This would seem to go along with the fact that I'm not seeing my debugging info and I'm not hitting my breakpoint - this method isn't being invoked.

So, if that's the case, what is getting invoked? Anything?
Nitish Bahadur
Ranch Hand

Joined: Aug 25, 2003
Posts: 118
1. BeerSelectForm.java
(a.) change import org.apache.struts.action.ActionError to import org.apache.struts.action.ActionMessage;
(b.) Add the following method:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
return validation(mapping, request);
}

(c.) validation should look like this:
public ActionErrors validation(ActionMapping mapping, HttpServletRequest req) {
System.out.println(0);
ActionErrors ae = new ActionErrors();
if (VALID_CRITERIA.indexOf(criteria) == -1)
{
ae.add("color", new ActionMessage("error.criteriaField.notValid")); // changed
}
return ae;
}

2. beerSelection.jsp
I removed the JSTL declaration at the top. (not being used in the version you sent me)

3. BeerSelectAction.java
(a.) Add public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
return actionForward(mapping, form, request, response);
}

These might be related to the differences between Struts 1.0, 1.1 and 1.2. It works in my Tomcat.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Woohoo! It works! Thanks, Nitish.

I think my problem was that the methods invoked by ActionServlet have changed from version to version in Struts. I believe I was using signatures from Struts 1.2 and/or 1.1 when my ActionServer is from 1.0. I didn't make the exact changes you specified (the method in the Action class is actually called "perform"), but your suggestions certainly got me going in the right direction.

Thanks for the help, Nitish.

Corey
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
Originally posted by Nitish Bahadur:
1. In your struts-config.xml under <action-mappings>, write a new action called: <action path="/HelloWorld" forward="/HelloWorld.jsp"/>

2. Create HelloWorld.jsp at the web-root of your Struts Web App.
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Hello World!</h2>
</body>
</html>

3. Stop and start your App Server. (Test Environment in case of WSAD)

4. What do you see when you access http://localhost:9080/Struts/HelloWorld.do thru a browser. (Tells us if Struts Controller is forwarding the request or not)

5. If you still see a blank page, then try accessing the page directly.
http://localhost:9080/Struts/HelloWorld.jsp


I am having problems with the very same beer-app struts example. My problem is that I get HTTP Status 404 - Servlet FrontController is not available
So I tried the advice above and got the same error with http://localhost/~juhan/HelloWorld.do while .../HelloWorld.jsp is displayed fine.

I think the problem may be in my xml conf files so I add them here allso.
web.xml

struts-config.xml
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with Struts