• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom tag containing generated JSTL : not interpreted

 
Nils Quairiat
Greenhorn
Posts: 6
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm struggling with the following issue :

I have a custom tag that has no body at all. I'm trying to replace the empty body with, for simplicity's sake, "[<c:out value="SUCCESS!"/>]". The goal is to see "[SUCCESS!]" displayed by the JSP which uses the tag, but all I see is "[]" and if I look at the generated source code, I can see that the <c:out statement is written on the page between the brackets, but not interpreted.

Is there a common way to achieve this ? The final goal will be to use other custom tags instead of the "<c:out" tag. The content will come from a database.
I tried different techniques with SimpleTagSupport and BodyTagSupport but none of those were successfull. I'm not sure if it is technically possible to do it, but then how should this be done ?

Thanks for any info.

Best regards,

Nils
 
sundeep kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.tutorialspoint;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

StringWriter sw = new StringWriter();
public void doTag()
throws JspException, IOException
{
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}

}
this is the class.
use these in the tag class and
in jsp
this one you all get the output;

html>
<head>
<title>A sample custom tag</title>
</head>
<body>
<ex:Hello>
This is message body
</ex:Hello>
</body>
</html>
and finally
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD with Body</short-name>
<tag>
<name>Hello</name>
<tag-class>com.tutorialspoint.HelloTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
in the .tld file.
bye
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to use JSTL inside your tag you can use a tag file.
 
sundeep kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If you need to use JSTL inside your tag you can use a tag file.



niice
 
Nils Quairiat
Greenhorn
Posts: 6
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that if the body of a tag is empty at first (and compiled like this, AKA translated into a servlet like this), and if, once the request is sent, the java code that corresponds to the tag finds out, after accessing the database, that the body of the tag should be another custom tag, that other custom tag cannot be rendered correctly since the JSP has already been compiled before.. Is there any common solution for this issue ? Or a less common solution ? That other custom tag could very well have to access the database as well and find out that its own body should be another custom tag, and so on.
 
Nils Quairiat
Greenhorn
Posts: 6
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to clarify the issue, here is a simple example :



Once the page is hit, the Java code that corresponds to the "myTag" tag goes into the data base and find out that the result becomes :



where anotherTag also fetches content in database, could find out that its body should be yet another tag etc..

==> the "test:anotherTag" cannot be rendered correctly because when the JSP was compiled, the tag was not present on it. On some page hits, it would not be "anotherTag" that would be the body of the "myTag", but for example "yetAnotherDifferentTag"'.

:)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer has already been given: you need to use a tag file.
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the question another way: No, you can't generate jsp/tag code from your jsp/tag code and expect to have it evaluated.
By the time you are running your page/tag file it's already converted to a servlet/SimpleTag and compiled into bytecode.

Jsp/tag code results in html template text. So you can't as part of that code load jsp/tag code from the database and translate/compile it on the fly.

Is the 'code' in the database expected to change at runtime at all?
I would convert the 'code' in the database into tag files. One tag file for each row in the database.
Then a simple database lookup and a switch statement would decide which tag to execute.





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!