• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Pass Parameter from .java to struts-config.xml to .jsp

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello people,
i like to knoe how do you pass paramter from .java to struts-config.xml and then forward it .jsp(view).and how to fetch the parameter in the View.

.java file -->

package app;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.*;

public class addaction extends Action
{
public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{

addform rf=(addform)form;
String noa=rf.getNoa();
String nob=rf.getNob();
String noc=String.valueOf(Integer.parseInt(noa)+Integer.parseInt(nob));
req.setAttribute("&noc",noc);// ???IS IT OK
return mapping.findForward( "success" );

}
}


struts-config.xml-->

<struts-config>
<form-beans>
<form-bean name="addjorm" type="app.addform"/>
</form-beans>
<action-mappings>
<action path="/add" type="app.addaction" name="addjorm">
<forward name="success" path="/success.jsp"/>

</action>
</action-mappings>
</struts-config>

.jsp(view)--->

<%@page language="java"%>
<html>
<title>
Success
</title>
<body>
<%!String s;%>
<% s=(String)request.getAttribute("&noc");// i did like this but it always print null
%>
<form>
The value =<input type=text name="t" value=<%=s%>/>!!!<p><a href="add.jsp"> try someother???</a></p>
</form>
</body>
</html>



Any ideas ???


tx
TUHIN
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, try including the 'redirect=false' in the <forward> tag. Seems to be bean is not carried from controller to the view.

Thanks.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

seems like you are mixing up parameters and attributes. have a look at this article to understand the difference...

bye,
jan
 
Tuhin Ghosh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ November 08, 2006: Message edited by: Tuhin Ghosh ]
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure about your existing code...it seems like it might work. What I would do is to add the property noc to my form, update that property in my action and not mess with setting individual values on the request. This approach would definitely simplify your JSP code.

- Brent
 
Tuhin Ghosh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi again ,

tx people, for replying i got it.

tx Nathan ..<forward name="success" path="/success.jsp" redirect="false"/>

your ans helped.

Could anyone please explain how the value didnt pass in earlier case?

Tx

tuhin
[ November 08, 2006: Message edited by: Tuhin Ghosh ]
 
gopinathang nathan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure about the default values.
- if redirect=true, index page to controller first request and controller to view is second request. second request is from the browser, so you might need scope=session.
- if redirect=false, the same request will be forwarded from controlle to view. so scope=request will work here.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea how that change would have fixed your problem. The default for redirect is false if it is not specified so I don't see where your change would make a difference. Were there other changes that you made?

- Brent
 
Tuhin Ghosh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the code:

.java--->

package app;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.*;

public class addaction extends Action
{
public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{
addform rf=(addform)form;
String noa=rf.getNoa();
String nob=rf.getNob();
String noc=String.valueOf(Integer.parseInt(noa)+Integer.parseInt(nob));
req.setAttribute("noc",noc);//same
return mapping.findForward("success");
}
}

struts-config---->

<struts-config>
<form-beans>
<form-bean name="addjorm" type="app.addform"/>
</form-beans>
<action-mappings>
<action path="/add" type="app.addaction" name="addjorm">
<forward name="success" path="/success.jsp" redirect="false"/>

</action>
</action-mappings>
</struts-config>

.jsp(view)--->

<%@page language="java"%>
<html>
<title>
Success
</title>
<body>
<%!String s;%>
<% s=(String)request.getAttribute("noc");
%>
<form>
The value =<input type=text name="t" value=<%=s%>>!!!<p><a href="add.jsp"> try someother???</a></p>
</form>
</body>
</html>

and its working fine now?? But m clueless....... any help regarding the flow(links are welcome)...!!!
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparing your two code postings I see three changes
1) added redirect="false" - it does not seem like this should matter
2) added a closing / to your input tag - this should not matter
3) changed attribute from "&noc" to "noc" - hmmm...would this make a difference?

oh well...I am glad you got it working.

- Brent
 
Tuhin Ghosh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also feel the same Brent. But i like to knoe the flow... any help!!

tx
tuhin
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
looks like the issue was with the &noc. I guess you are asking about the code flow.

1. when the user input the value , the values are get assigned to your bean (here addform).
2. now when the user submit the data for futher process, the control comes to your action(via Actionservlet, which is internal to struts, the user don't have to wrry abt this class & struts-config.xml) where you will get the input values from the your form (addform)



3. Now you are assigning the final value, noc to the request attribute named "noc". You can give any name in the first parameter.



4. and then you are telling the strut-config.xml that take me to "success" page, by mentioning return mapping.findForward("success");

5. now in struts config, you have mapping for a "success" to success.jsp. & you get the result page, where you are retriving the noc value from request attribute.


Hope this helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic