wood burning stoves 2.0*
The moose likes Struts and the fly likes Scriptlets in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Scriptlets in Struts" Watch "Scriptlets in Struts" New topic
Author

Scriptlets in Struts

AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Hi,
Is it possible to use scriptlets in JSP pages to get results from the Action.do.

eg. In the Action.do
// If I set a value in a bean

beanSearch.setValue(new Long(1));
httpServletRequest.setAttribute("beanSearch", beanSearch);

in the jsp
I tried to get the value in jsp by
<%=beanSearch.getValue %>

but this does not seem to work.

Any ideas?

Thanks

Anil
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
If you use struts tags, the bean will be made available to the jsp page, else you have to define it or get it by .
or try using the <jsp:useBean>


"Failure is not when you fall down; its only when you fail to get up again."
AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Thanks Shankar.

i am now getting the results by using <jsp:usebean>

But when i trying to displaying the value in a button it does not get the result.


eg in a HTML:button tag i got an onclick to go to an action with a id
Search.do?test=<%=beanSearch.getValue()%>

(sorry i can't get to paste the code)

thanks

Anil
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
what I understand is that you are trying to set the value of "test" when the onclick event is fired. When you first get to the jsp page all the scriptlets are processed at the server side and replaced by the constants, since the javascript method is not called on load of the form, the scriptlet is not processed



If this function is called when you click the button the value of test will not be replaced by the scriptlet, it wud be a javascript error I suppose. So in order to overcome this what you can do is rewrite your javascript code in this way


So now the testVal variable is initialised when the page is loaded and when you click the button it gets the value there.

Hope this is what you were trying to do.
AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Thank you Shankar

That's what i wanted



Anil
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

You could also have just used the bean:write tag:

<bean:write name="beanSearch" property="value" />


The problem with the scriptlet is that, unlike the bean tag, it does not automatically go through the context collections to find the referenced bean. That's why you need to use the jsp:usebean tag first in order for the scriptlet to work.

Shankar: When you first get to the jsp page all the scriptlets are processed at the server side and replaced...

Correct

Shankar: ...by the constants, since the javascript method is not called on load of the form, the scriptlet is not processed

No, the scriplets have already been processed by the time the response gets back to the browser. You can "view source" in the browser to verify this.


...
var url = "search.do?test=<%=beanSearch.getValue()%>"
location.href = url;
...
If this function is called when you click the button the value of test will not be replaced by the scriptlet, it wud be a javascript error I suppose


I don't think so. Assuming the scriptlet evaluates properly on the server side, the javascript that the browser receives in the response should be good.

This should work:



Junilu - [How to Ask Questions] [How to Answer Questions]
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
Thanks Junilu, You are right all the scriptlets are being processed before the response is being sent back. If this was the case then Anil's code should have been worked properly isn't it?.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

I think we've already established why Anil's scriptlet didn't work. I was just commenting on some of the things you wrote in your post.
 
wood burning stoves
 
subject: Scriptlets in Struts
 
Similar Threads
encoding URL in (href element of) JSP without scriptlets
What does this do? request.getParameter("string" + int)
Custom tags
How to get and set request.getParameter?
Displaying bean value in JSP