• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need some advice on converting scriptlet code to jstl

 
Matt Tinsley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So apparently I got off to a bad start learning how to code JSPs and now I have a lot of scriptlet code that I need to convert to jstl. I've been able to figure out the basics, but if someone could clarify a few things it would be a big help.



Basically the code above outputs the class attribute in the case that errors.hasField("email") returns true (FormErrors is a class I defined). errors will be null in some circumstances. How could I implement this without scriptlets?
 
Paul Clapham
Sheriff
Posts: 21876
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use JSTL's <c:if> element. JSTL is, thankfully, not as anal as Java and it just ignores nulls in cases like this. (The JSTL documentation says it more precisely than I have.)
 
Matt Tinsley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. For some reason I didn't think you could use c:if within an html tag. This is how I'm doing it now:


I have one more question, I've converted the following code:


to



Is this correct? Again thanks for the help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Tinsley wrote:Thanks for the response. For some reason I didn't think you could use c:if within an html tag.

To the JSP processing engine HTML is just template text. It has no ideas about HTML or JavaScript or anything else that's not JSP elements. Everything else is just text.

You might also be able to simplify this by using the ternary operator of the EL:

 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one point of pain in converting to EL is that EL can only call get/set methods on your beans.

As such the method "hasField(...)" can not be invoked directly from EL.

The easiest transformation to make this work with EL would be to expose a Map of field names that have errors.

Your FormErrors class would need a method



You could then use the EL expression errorMap.email or errorMap["email"] to retrieve a specific error condition/message.
You can test for its existence using the "empty" keyword.

So your test would become:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:The one point of pain in converting to EL is that EL can only call get/set methods on your beans.

Unless he's using JSP 2.2.

I have mixed feelings about the addition of method calls to the EL. I can't help but feel it's the "camel's nose" for doing all sorts of inappropriate things in JSPs that weren't possible with the EL's previous limitations.

(Stefan, have you seen my PM to you?)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!