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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ struts Example pgm

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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>
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Trying putting this in your validate method and see what prints to your console window when you submit the form:

System.out.println(":"+color+":");
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
From this other post, it looks like your select is not well formed :

It should be


Please check your book again.
 
kathir vel
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closed. Continuing here
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic