my dog learned polymorphism*
The moose likes Struts and the fly likes How to Pass Parameter from .java to struts-config.xml to .jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to Pass Parameter from .java to struts-config.xml to .jsp" Watch "How to Pass Parameter from .java to struts-config.xml to .jsp" New topic
Author

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

Tuhin Ghosh
Ranch Hand

Joined: Oct 11, 2006
Posts: 33
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
gopinathang nathan
Ranch Hand

Joined: Oct 28, 2006
Posts: 41
Hi, try including the 'redirect=false' in the <forward> tag. Seems to be bean is not carried from controller to the view.

Thanks.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
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

Joined: Oct 11, 2006
Posts: 33

[ November 08, 2006: Message edited by: Tuhin Ghosh ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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

Joined: Oct 11, 2006
Posts: 33
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

Joined: Oct 28, 2006
Posts: 41
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

Joined: Feb 08, 2006
Posts: 948
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

Joined: Oct 11, 2006
Posts: 33
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

Joined: Feb 08, 2006
Posts: 948
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

Joined: Oct 11, 2006
Posts: 33
I also feel the same Brent. But i like to knoe the flow... any help!!

tx
tuhin
Prasanth Pillai
Ranch Hand

Joined: Oct 16, 2006
Posts: 52
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.
 
wood burning stoves
 
subject: How to Pass Parameter from .java to struts-config.xml to .jsp
 
Similar Threads
could not load the select box during pageload.
problem with the name attribute of form tag
Hi i am trying to connect to postgresql database using struts 1.2..i have used datasource
getParameter gives null value
DynaActionForm - Exception creating bean