I'm following a small test application example listed in Profressional Jakarta Struts book (ch 3). It basically involves entering a stock symbol in one jsp and getting it's current price in another jsp. So basically it comprises of two JSPs, one Action, and one ActionForm. But the problem I'm having is that after I enter the value in the text field of input.jsp and click on submit button, the quote.jsp which is supposed to show the price (being set in the Action class) does not retrieves the price. It seems to me that the ActionForm bean is not being populated and carried over to Action processing after I click submit. Here are the code snippets and my config file settings:
-- Struts-config.xml --
quote:
--------------------------------------------------------------------------------
<struts-config>
<data-sources/>
<form-beans>
<form-bean name="lookupForm" type="myapp.LookupForm"/>
</form-beans>
<global-exceptions/>
<global-forwards>
<forward contextRelative="true" name="input" path="/input.jsp" redirect="true"/>
</global-forwards>
<action-mappings>
<action input="/input.jsp" name="lookupForm" path="/lookup"
scope="request" type="myapp.LookupAction" validate="no">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/input.jsp"/>
</action>
</action-mappings>
<controller/>
</struts-config>
--------------------------------------------------------------------------------
--- input.jsp --
quote:
--------------------------------------------------------------------------------
<html:form action="/lookup">
<html:text property="symbol" />
<html:submit value="Submit your quote"/>
</html:form>
--------------------------------------------------------------------------------
-- ActionForm class --
quote:
--------------------------------------------------------------------------------
package myapp;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LookupForm extends ActionForm implements Serializable {
private String symbol = null;
public void LookupForm(){}
public String getSymbol() {
return this.symbol;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.symbol = "";
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
--------------------------------------------------------------------------------
-- web.xml --
quote:
--------------------------------------------------------------------------------
<web-app>
<servlet>
<servlet-name>action</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>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
--------------------------------------------------------------------------------
-- Action class --
quote:
--------------------------------------------------------------------------------
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO: Write method body
String price = null;
String symbol = null;
String target = "success";
request.setAttribute("msg", ""
request.setAttribute("msg", "getting quote..."
LookupForm lookupform = new LookupForm();
lookupform = (LookupForm)form;
//lookupform.setSymbol("sun"
symbol = lookupform.getSymbol();
price = getQuote(symbol);
request.setAttribute("PRICE", price);
if(price != null) {
request.setAttribute("msg", new String("Here is the price:");
target = "success";
}
else {
request.setAttribute("msg", new String("Invalid stock!");
target = "failure";
}
return mapping.findForward(target);
}
}
--------------------------------------------------------------------------------
Any idea why my ActionForm (lookupForm) is being null in execute method of Action class? Any help would be greatly appreciated. Thanks.
-- Struts-config.xml --
quote:
--------------------------------------------------------------------------------
<struts-config>
<data-sources/>
<form-beans>
<form-bean name="lookupForm" type="myapp.LookupForm"/>
</form-beans>
<global-exceptions/>
<global-forwards>
<forward contextRelative="true" name="input" path="/input.jsp" redirect="true"/>
</global-forwards>
<action-mappings>
<action input="/input.jsp" name="lookupForm" path="/lookup"
scope="request" type="myapp.LookupAction" validate="no">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/input.jsp"/>
</action>
</action-mappings>
<controller/>
</struts-config>
--------------------------------------------------------------------------------
--- input.jsp --
quote:
--------------------------------------------------------------------------------
<html:form action="/lookup">
<html:text property="symbol" />
<html:submit value="Submit your quote"/>
</html:form>
--------------------------------------------------------------------------------
-- ActionForm class --
quote:
--------------------------------------------------------------------------------
package myapp;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LookupForm extends ActionForm implements Serializable {
private String symbol = null;
public void LookupForm(){}
public String getSymbol() {
return this.symbol;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.symbol = "";
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
--------------------------------------------------------------------------------
-- web.xml --
quote:
--------------------------------------------------------------------------------
<web-app>
<servlet>
<servlet-name>action</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>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
--------------------------------------------------------------------------------
-- Action class --
quote:
--------------------------------------------------------------------------------
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO: Write method body
String price = null;
String symbol = null;
String target = "success";
request.setAttribute("msg", ""
request.setAttribute("msg", "getting quote..."
LookupForm lookupform = new LookupForm();
lookupform = (LookupForm)form;
//lookupform.setSymbol("sun"
symbol = lookupform.getSymbol();
price = getQuote(symbol);
request.setAttribute("PRICE", price);
if(price != null) {
request.setAttribute("msg", new String("Here is the price:");
target = "success";
}
else {
request.setAttribute("msg", new String("Invalid stock!");
target = "failure";
}
return mapping.findForward(target);
}
}
--------------------------------------------------------------------------------
Any idea why my ActionForm (lookupForm) is being null in execute method of Action class? Any help would be greatly appreciated. Thanks.