File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want to write dynamically to HTML attribute within JSP. How to do this?

 
Edward Winchester
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The following fragment is from a JSP file of mine:


I'm wanting to internationalise the submit button text. The above method works (in my environment).

Is this valid though? I'm new to JSP and tag processing so wasn't sure if you could have a tag within a string literal.

Thanks,

Ed
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two "levels" of tags in a JSP file.

There's the simple HTML--these are simply sent from the server to the client. This includes <input> tags, <b> tags, etc.

Then there are JSP tags, like <c:if>, <fmt:...>, etc. These are interpreted on the server side before being sent to the client.

There are no issues with embedding JSP tags in HTML tags, since they're on different planes of existence. You cannot, however, use a JSP tag as an attribute value for another JSP tag.

A simple web search would have validated your approach.
 
Edward Winchester
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:There are two "levels" of tags in a JSP file.

There's the simple HTML--these are simply sent from the server to the client. This includes <input> tags, <b> tags, etc.

Then there are JSP tags, like <c:if>, <fmt:...>, etc. These are interpreted on the server side before being sent to the client.

There are no issues with embedding JSP tags in HTML tags, since they're on different planes of existence. You cannot, however, use a JSP tag as an attribute value for another JSP tag.

Thank you.

A simple web search would have validated your approach.

I did a lot of searching on the web before posting this query. I didn't find any page that validated my approach. There was examples using expression language in attributes. I only post on forums after searching on the web.

Ed
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL is cool. You may want to work through the 4-part introduction linked under "Resources" in the http://faq.javaranch.com/java/JspFaq to learn about all the things it can do.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to look at it is to recognize that to a JSP, anything that's not a JSP element (JSP action, scriptlet, EL expression, etc) is simply template text to the JSP engine. The fact that HTML looks like tags is completely moot because the JSP just treats it as text without even looking at it.
 
Edward Winchester
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Another way to look at it is to recognize that to a JSP, anything that's not a JSP element (JSP action, scriptlet, EL expression, etc) is simply template text to the JSP engine. The fact that HTML looks like tags is completely moot because the JSP just treats it as text without even looking at it.

Ahh, ok. I thought there might be some complex rules about some elements only being available in some contexts. I was looking at my problem from an XML/XHTML viewpoint where it just didn't look right. I guess JSP doesn't even have to be used with HTML hence the term "template text" to generically refer to the non-JSP stuff.

So there is no real context - just template text whose structure is unimportant (and there need be no structure at all) and JSP elements.

Feel like a penny has just dropped!
 
Edward Winchester
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:JSTL is cool. You may want to work through the 4-part introduction linked under "Resources" in the http://faq.javaranch.com/java/JspFaq to learn about all the things it can do.

I'm going to take a look now, thanks.

Ed
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Winchester wrote:So there is no real context - just template text whose structure is unimportant (and there need be no structure at all) and JSP elements.


Exactly. Whatever is not a JSP element could be random characters entered by letting an iguana walk over the keyboard as far as the JSP engine is concerned.

The HTML-ness of the generated page is important only to the browser when it receives the response.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Whatever is not a JSP element could be random characters entered by letting an iguana walk over the keyboard as far as the JSP engine is concerned.

That's actually quite close to how most HTML seems to be developed.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Bear Bibeault wrote:Whatever is not a JSP element could be random characters entered by letting an iguana walk over the keyboard as far as the JSP engine is concerned.

That's actually quite close to how most HTML seems to be developed.

Based upon what I've seen, it is certainly how most JavaScript is written.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Moving rapidly off-topic ;)

I've never really understood why JavaScript is so abused and mis-understood. Except for a few glaring warts (I'm looking at you, this) I really, really like it, and it's quite capable.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that the OP's question has been adequately addressed (please let us know)...

In my opinion, while JavaScript is one of the world's most-used languages (due to its ubiquitousness on the web), it is the also the one least understood by most who use it.

It's actually quite an elegant and interesting language. I truly believe that most people who moan "I hate JavaScript", really don't understand the language, or are reacting to the browser DOM models (which are a complete mess), rather than to the language itself.
 
Edward Winchester
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Assuming that the OP's question has been adequately addressed (please let us know)...

What you said about JSP elements and template text was very helpful. My worry/uncertainty has been lifted. Thanks.
 
Lukasz Ochmanski
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible in JSP:

<jsp:attribute>
The jsp:attribute element allows you to define the value of a tag attribute in the body of an XML (HTML) element instead of in the value of an XML (HTML) attribute.

JSP Syntax

XML (HTML) Syntax
Same as JSP syntax.

Examples
The following template page uses jsp:attribute, which uses the output of fmt:message to set the value of the value attribute of tt:parameter:




source: http://java.sun.com/products/jsp/syntax/2.0/syntaxref2014.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic