Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

call bean's funtion in jsp  RSS feed

 
Joshua Cloch
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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...
 
Lanny Gilbert
Ranch Hand
Posts: 132
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!