File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

ActionForm not populated/is null after mapping

A Wolfe
Greenhorn

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 --

<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 --

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

-- ActionForm class --
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 --

<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 --

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.
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
fdVoucherReport.do?rmDate="+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
Greenhorn

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

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;
}

to
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
cheers,
parthi.


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

Joined: Feb 26, 2001
Posts: 4456
    
    6

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: http://aspose.com/file-tools
 
subject: ActionForm not populated/is null after mapping