Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActionForm bean not being populated from JSP

Status
Not open for further replies.

roman882

IS-IT--Management
May 14, 2003
4
US
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=&quot;lookupForm&quot; type=&quot;myapp.LookupForm&quot;/>

</form-beans>

<global-exceptions/>

<global-forwards>

<forward contextRelative=&quot;true&quot; name=&quot;input&quot; path=&quot;/input.jsp&quot; redirect=&quot;true&quot;/>

</global-forwards>

<action-mappings>

<action input=&quot;/input.jsp&quot; name=&quot;lookupForm&quot; path=&quot;/lookup&quot;

scope=&quot;request&quot; type=&quot;myapp.LookupAction&quot; validate=&quot;no&quot;>

<forward name=&quot;success&quot; path=&quot;/quote.jsp&quot;/>

<forward name=&quot;failure&quot; path=&quot;/input.jsp&quot;/>

</action>

</action-mappings>

<controller/>

</struts-config>



--------------------------------------------------------------------------------

--- input.jsp --


quote:
--------------------------------------------------------------------------------

<html:form action=&quot;/lookup&quot;>
<html:text property=&quot;symbol&quot; />
<html:submit value=&quot;Submit your quote&quot;/>
</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 = &quot;&quot;;
}

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 = &quot;success&quot;;
request.setAttribute(&quot;msg&quot;, &quot;&quot;);
request.setAttribute(&quot;msg&quot;, &quot;getting quote...&quot;);
LookupForm lookupform = new LookupForm();
lookupform = (LookupForm)form;
//lookupform.setSymbol(&quot;sun&quot;);
symbol = lookupform.getSymbol();
price = getQuote(symbol);
request.setAttribute(&quot;PRICE&quot;, price);
if(price != null) {
request.setAttribute(&quot;msg&quot;, new String(&quot;Here is the price:&quot;));
target = &quot;success&quot;;
}
else {
request.setAttribute(&quot;msg&quot;, new String(&quot;Invalid stock!&quot;));
target = &quot;failure&quot;;
}

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.

 
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


quote:
--------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------


to

quote:
--------------------------------------------------------------------------------
public String getQuote(String symbol) {
if(symbol.equals(&quot;sun&quot;)) {
String s = new String(&quot;25&quot;);
return s;
}
return null;
}

--------------------------------------------------------------------------------

So it was basically using symbol.equals(&quot;sun&quot;) instead of symbol == &quot;sun&quot;.

I still don't get why it doesn't work either way.

 
you cannot use == to compare String object. The == operator checks to see if two objects are exactly the same object. hence, yo have to use equals. similarly, use compareTo to check for inequality.

~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top