Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

methods generating html output in jsp  RSS feed

 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For servlet or simple classes, we can easily modularized our class by using methods.

However, the jsp section I want to cutout (called many times in the main jsp method), outputs html (e.g. tags). It wont compile.

One solution I hate but did was to use jsp fragments.

Jsp Include only allows strings for parameters. I need to pass an object.

Will a method work, if we do servlet-style stuff on the method? Like printing the html output using jsp 'out' implicit object? Or maybe getting the printwriter?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you investigated tag files?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could add a method to a JSP, but you really can't reuse a method in a JSP; at least not in other JSPs.

What about developing a custom tag? You create a custom tag, such as

<mytage:doSomething/>

And when the JSP hits that tag, something gets printed out. Essentially, this tag links back to a Java method that can spit out the HTML tags that you desire.

Also, in the code for the custom tag, you have access to things like the HttpSession and ServletContext. If you have an object, you can stuff those objects into the HttpSession, and retrieve it in the custom tag.

And of course, you can write one tag, but use it in all of your JSP files.

Does this sound like a feasable solution? Custom tags were added to the API to address these very types of issues.

Cheers!

-Cameron
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kameron McKenzie:
Custom tags were added to the API to address these very types of issues.


Yes, exactly why I mentioned them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!