Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

call bean's funtion in jsp

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys.

Say, i have a javabean class named userBean,it has a lot of functions.
It is very easy to call the functions in servlet.But,I really do not know how to do it in jsp.
can i do it like this:

<jsp:useBean id="userBean" class="...">
</jsp:useBean>

${userBean.removerLastName()}


I know it is easy to call the function to include the java script in jsp,but i do not want do it in this way.
Thanks very much.
 
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Joshua Cloch:

I know it is easy to call the function to include the java script in jsp



It is impossible to call any Java function with JavaScript in a JSP. So I'm sure that you meant to say java scriptlet.

But to your main question: you can't call instance methods of beans using the JSTL and EL in scriptless JSP pages. All you can do is to get and set properties of the beans.

This is intentional. The whole purpose of the JSTL and EL is to help herd developers to move processing out of the pages and into servlet controllers where it belongs. Any bean processing that is more involved than accessing the properties should be done before the request is forwarded from the controller to the page.

If you absolutely need to do something on the page than can't be done with JSTL/EL, you can write custom actions or EL function to extend the ability of the pages.
 
Joshua Cloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much.Maybe I should use a custom tag...
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, it looks like you're trying to erase the last name, so instead of doing a custom tag, you could call <bean instance>.setLastName("") or
<bean instance>.setLastName(null)

either of those would clear out the lastName property of your bean, if that is your intent.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lanny Gilbert:
you could call <bean instance>.setLastName("") or
<bean instance>.setLastName(null)



His point is that he wants to avoid scriplets.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, the syntax to set a property in a scriptless JSP would be:

<c:set target="${beanName}" property="propertyName" value="${whatever}"/>

or you could use the old-fashioned <jsp:setProperty> action.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic