• Post Reply 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

ActionForm not populated/is null after mapping

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh, I didn't understand. Please elaborate. Thanks.
 
A Wolfe
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== compares the reference
.equals compares the actual value held in the reference
cheers,
parthi.
 
Marshal
Posts: 14508
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!