wood burning stoves 2.0*
The moose likes JSP and the fly likes Want to write dynamically to HTML attribute within JSP. How to do this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Want to write dynamically to HTML attribute within JSP. How to do this?" Watch "Want to write dynamically to HTML attribute within JSP. How to do this?" New topic
Author

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

Edward Winchester
Ranch Hand

Joined: Sep 16, 2009
Posts: 65
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 16, 2009
Posts: 65
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
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
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.


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Edward Winchester
Ranch Hand

Joined: Sep 16, 2009
Posts: 65
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

Joined: Sep 16, 2009
Posts: 65
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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Sep 29, 2008
Posts: 12617

(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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Sep 16, 2009
Posts: 65
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

Joined: Aug 14, 2012
Posts: 1
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
 
Consider Paul's rocket mass heater.
 
subject: Want to write dynamically to HTML attribute within JSP. How to do this?