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>