wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ struts Example pgm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply locked New topic
Author

HFSJ struts Example pgm

kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
Hi All,
I tried the struts_beer sample program,exactly the same way in the book page740.But it is not forwarding the request from form.jsp to servlet.In the address bar it is changing as SelectBeer.do,but it is showing the same form.jsp.Can anyone help how to debug about forwarding in this program?



package com.example.web;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import javax.servlet.http.*;

public class BeerSelectionForm extends ActionForm
{
private String color;
public void setColor(String color)
{
this.color=color;
}
public String getColor()
{
return color;
}
private static final String VALID_COLORS="amber,dark,brown,light";
public ActionErrors validate( ActionMapping mapping,HttpServletRequest request)
{
ActionErrors errors=new ActionErrors();
if(VALID_COLORS.indexOf(color)== -1)
{
errors.add("color", new ActionMessage("error.colorField.notValid"));
}
return errors;
}
}


package com.example.web;

import java.util.*;
import com.example.model.*;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.*;
import javax.servlet.http.*;

public class BeerSelectionAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
{
BeerSelectionForm myForm=(BeerSelectionForm) form;
BeerExpert be=new BeerExpert();
List result=be.getBrands(myForm.getColor());
request.setAttribute("styles",result);
return mapping.findForward("show_results");

}

}



<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>FrontController</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>



<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">

<struts-config>

<!-- ========== Form Bean Definitions ============ -->
<form-beans>
<form-bean name="selectBeerForm" type="com.example.web.BeerSelectionForm" />
</form-beans>

<action-mappings>
<action
path="/SelectBeer"
type="com.example.web.BeerSelectionAction"
name="selectBeerForm" scope="request"
validate="true" input="/form.jsp">
<forward name="show_results" path="/result.jsp" />

</action>

</action-mappings>



<message-resources parameter="ApplicationResources" null="false" />

</struts-config>
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Trying putting this in your validate method and see what prints to your console window when you submit the form:

System.out.println(":"+color+":");


A good workman is known by his tools.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

From this other post, it looks like your select is not well formed :

It should be


Please check your book again.


[My Blog]
All roads lead to JavaRanch
kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
I tried it.Still it is not working.In address box it is showing the SelectBeer.do after submit.But it is not showing result.jsp.It is same as form.jsp.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

As it's taking you so long to solve, I've tried it myself. And it worked after correcting the above select.
Did you restart the container after saving form.jsp ?

In address box it is showing the SelectBeer.do after submit.

This is not a problem.

But it is not showing result.jsp.It is same as form.jsp.

Please show your result.jsp.
kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
I tried your code in form.It was working first.But second time just I opened that page.Again it is showing the same problem.

my result.jsp

%@ page import="java.util.*" %>

<html>
<head>
<title>My Beer Choice</title>
</head>
<body>
<h1>Beer Recommendations JSP</h1><br>
<%
List styles=(List)request.getAttribute("styles");
Iterator it=styles.iterator();
while(it.hasNext())
{
out.println("<br>try :"+it.next());
}
%>
</body>
</html>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It was working first.But second time just I opened that page.Again it is showing the same problem.

Strange. Try to clear the cache.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by kathir vel:
I tried your code in form.It was working first.But second time just I opened that page.Again it is showing the same problem.

The reason why it was so hard to troubleshoot was that your validate method was sending back an error, which causes the app to navigate back to the form page (as defined by the input attribut of your ActionMapping in the struts-config.xml file). Unfortunately, you apparently don't have an html:errors or html:messages tag in your form page to display the error and tell you what went wrong.

Your forwarding fails because validation is not passing. That's part of how Struts works. You need to get an html:errors tag set up properly on the form page or do something (like my previous recommendation) to understand when and why validation is failing.
[ October 31, 2007: Message edited by: Marc Peabody ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You need to get an html:errors tag set up properly on the form page or do something (like my previous recommendation) to understand when and why validation is failing.

That's right. I'm lamely assuming that a correct select would not generate any error, as all colors are in VALID_COLORS. But the System.out or the html:errors will help.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Closed. Continuing here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFSJ struts Example pgm
 
Similar Threads
strange error with respect to web.xml file
Getting error while running BeerStruts example
Trouble with Struts
problem in struts example
404 - Servlet FrontController is not available