File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes ActionForm not populated/is null after mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ActionForm not populated/is null after mapping" Watch "ActionForm not populated/is null after mapping" New topic

ActionForm not populated/is null after mapping

A Wolfe

Joined: Sep 08, 2003
Posts: 23
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 --

<form-bean name="lookupForm" type="myapp.LookupForm"/>
<forward contextRelative="true" name="input" path="/input.jsp" redirect="true"/>
<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"/>

--- input.jsp --

<html:form action="/lookup">
<html:text property="symbol" />
<html:submit value="Submit your quote"/>

-- ActionForm class --
package myapp;
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 --


-- Action class --

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;
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.
Santosh Maskar
Ranch Hand

Joined: Jul 02, 2003
Posts: 226
u can pass this value to action class which is called
here is the example for the same"+frmDate1+"&toDate="+toDate1;
here to date field is passed to the another jsp page and it work
also u can define the same form bean in the strut config .xml
so u can get the value for the same in both jsp pages ,
use same form bean in multipale pages to get the values
A Wolfe

Joined: Sep 08, 2003
Posts: 23
Santosh, I didn't understand. Please elaborate. Thanks.
A Wolfe

Joined: Sep 08, 2003
Posts: 23
Ok, I finally got that working. For the benefit of those who may come accross samiliar problem, the issue was in Action class. It's kinda weird since I've used the same sort of code beofore and it worked fine. Here is the change I made:
I changed my getQuote method in Action class from

public String getQuote(String symbol) {
if(symbol == "sun" ) {
String s = new String("25");
return s;
return null;

public String getQuote(String symbol) {
if(symbol.equals("sun")) {
String s = new String("25");
return s;
return null;

So it was basically using symbol.equals("sun") instead of symbol == "sun".
I still don't get why it doesn't work either way.
[ November 17, 2003: Message edited by: A Wolfe ]
parthiban subramaniam
Ranch Hand

Joined: May 15, 2002
Posts: 116
== compares the reference
.equals compares the actual value held in the reference

Even crazy and silly looking problems are sometimes real.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

As previously mentioned, == should almost never be used to evaluate string equality; use equals() instead. Also, new String("25") is unnecessarily redundant because it causes two Strings to be created: one on the heap and another one in the String pool. Use String s = "25" instead.

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: ActionForm not populated/is null after mapping
It's not a secret anymore!