• 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

Scriptlets in Struts

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
AnilPrakash Raju
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Shankar

That's what i wanted



Anil
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic