jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes Passing value to JSP from a method in Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing value to JSP from a method in Action class" Watch "Passing value to JSP from a method in Action class" New topic
Author

Passing value to JSP from a method in Action class

navi kumar
Ranch Hand

Joined: May 10, 2006
Posts: 47
Hi all,

I have a situation where, I am supposed to pass a value from method in action class to JSP.

For ex,

in a method called
public ActionForward deleteRecord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
{
String region = daoObj.deleteRecord(id, Name, phaseNum, delRecds);
// the daoObj.deleteRecord returns a string.
}
Now, I want to get this String (region) into my external javascript file(being included in JSP) for validation purposes.

Is it possible to do so? If yes, please let me know.

Regards
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
You can do the first part by simply putting the region String in request scope. On the jsp side, get it from request scope.

But I don't think you can call javascript function on the client side after response is being committed. Javascript is basically called on the client side before making any request to the server. But your case is just reverse of that.


Naseem
[ August 28, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
navi kumar
Ranch Hand

Joined: May 10, 2006
Posts: 47
Thanks for the reply Naseem,

But, for confirmation, Is that possible or completely NO!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can do this. I wouldn't recommend it, but you can.

Suppose the name of your JavaScript include file is validation.js. You could change the name of this file to validation.jsp. You could then include a <cut> tag to include the value you put in the request and then write:

<script src="validation.jsp" />

I don't like this method, though because it makes it so your include file is always dependent on something being in the request, and it's no longer pure JavaScript but a mixture of JSP tags and JavaScript.

I'd suggest you refactor your validation function so that the region can be passed as a parameter. Then use JSP tags in the calling JSP to provide the region when calling the validation method.

Here's an example:
<script>
// global variable for region
g_region = '<cut value="${theRegion}" />';

// call validation function
validate(foo, bar, g_region);
</script>
[ August 28, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
navi kumar
Ranch Hand

Joined: May 10, 2006
Posts: 47
Hi, I am doing it this way.

in the jsp:
html:button property="submitButton" value="Delete" onKlick="javascript : deleteRecord();chkRegion();"

in the javascript:
function deleteRecord()
{
with(document.SearchRecordForm)
{
var msg = "Are you sure you want to delete the selected MDU Records ?";
if (confirm(msg)){
method.value = "deleteRecord";
submit();}}}

function chkRegion()
{
with(document.SearchRecordForm){
var region1 = rcdRegion.value;
if(region1 == "MID-ATLANTIC")
{
alert("Please delete MID_ATLANTIC records in Mid Atl Manage.");
}
return;
}
}

in the action :
public ActionForward deleteRecord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
{
String region = daoObj.deleteRecord(id, streetName,streetNum,delRecds);
formObj.setRcdRegion(region);
// here i am trying to set the RcdRegion which I call in javascript fucntion checkRegion()
return mapping.findForward("showDetail");
}

But, when I do this..
ERROR: rcdRegion is undefined.

I understand this error is because, the rcdRegion is empty when the function checkRegion() is called.

Please let me know, if there is any way to call the function2 after the function1.

For ex: in my case:
html:button property="submitButton" value="Delete" on
Klick="javascript: deleteRecord(); chkRegion();"
// when the DELETE button is clicked, the function deleteRecord() should be called... go to the method and perform all the action as defined. The page is loaded again(as defined in the method). Now, call the function chkRegion(). By doing so, I can check if the rcdRegion is null or not!

I tried doing something like onreturn(). But it didnt work. I was not sure whether to use it or not!

Please Help!
Regards
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It looks like I didn't understand what you were doing. I was assuming that the region was determined by the action prior to the page being rendered. The suggestion I made in my last post was based on this assumption.

What you are trying to do is to have a JavaScript function communicate directly with the server. This type of JavaScript/Server communication is referred to as AJAX (Asynchronous JavaScript and XML) programming. This has become popular recently, and there is a lot of material on how to do this. However, learning to do it means a significant learning curve, particularly if you're only marginally familiar with JavaScript.

The code you have currently is trying to mix client-side JavaScript code with server-side ActionForm values. Because one lives on the client (browser) and the other lives on the server, mixing them like your code does will never work. You must use a JavaScript component called the XMLHttpRequest object in order to make this communication between the client and server work.

As I see it, you have three options:

1- Find a conventional, non-AJAX solution to this problem. You might create a JavaScript data structure of some sort that would allow you to determine which records belong to the MID_ATLANTIC region by looking up values in an array or associative array, and not make a call to the server at all.

2- Adopt an AJAX framework such as DWR which will allow you to use AJAX without learning all the low-level details of how the calls are made. After downloading DWR and trying some of the examples, you could be ready to use it for your current application.

3- Spend some time and and actually learn how to make AJAX calls to the server. There is no shortcut. If you want to go this route, you will have to spend quite a few hours learning the process and scratching your head over the mistakes you make.

In any case, teaching you how to program AJAX is beyond the scope of this forum. It's something you'll have to do on your own.
[ August 29, 2006: Message edited by: Merrill Higginson ]
mnadeem chishti
Greenhorn

Joined: Aug 07, 2006
Posts: 11
it is quite simple
HttpServletRequest object is already in action class or use session
for example

public class MyAction extends Action{

public ActionForword execute (.............){


String myvar;
myvar=..;

request.setAttribute("validate",myvar);
or

HttpSession session=request.getSession(false); // if session already creaqted

session.setAttribute("validate",myvar);

}
}


// *************************now in jsp file and javascript


<script language="javascript">
var validate=<%=(String) session.getAttribute("validate")%>
or
var validate=<%= (String) request.getAttribute("validate") %>

// session and request variable are available in jsp file by default
</script>
 
Consider Paul's rocket mass heater.
 
subject: Passing value to JSP from a method in Action class
 
Similar Threads
Struts2: REST URLs with Convention plug in - passing params through the URL
passing values from jsp to struts action without showing in address bar
How to store value in hidden field with struts 2.0?
dropdown menu default value from table
Execute a JSP page with client side Java.