6

Whenever doing <c:set var="name" value="1"/>, #{name} is always a String as evidenced by #{name.class}.

Is there any way to in a JSF/Facelets context to set a scoped attribute that is an Integer or Long literal value?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
GreenieMeanie
  • 3,560
  • 4
  • 34
  • 39

2 Answers2

4

EL has Automatic Type Conversion. This article has some good information. However, the short of it is that you shouldn't care. You should be able to do things like the following as long as param.month is, in fact, an Integer.

<c:set var="myInteger" value="${param.month}"/>
<p>
The value of myInteger is:<c:out value="${myInteger}"/>
Perform a multiplication operation to show that the type is correct:
<c:out value="${myInteger *2}"/>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Randy Simon
  • 3,324
  • 21
  • 19
  • +1 although I corrected that you incorrectly called it JSTL instead of EL. JSTL is a taglib as outlined here http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/, EL are those `${}` things as outlined in this JSP/EL spec: https://jsp.dev.java.net/spec/jsp-2_1-fr-spec-el.pdf – BalusC Mar 17 '10 at 17:47
  • Ah, I just need to use an expression and not a literal, so if I do value="#{1}" then it will be a long. I still don't like how you can't control whether it is long or int though. – GreenieMeanie Mar 17 '10 at 17:47
  • Thanks, just a typo. My bad. I've been out of Java land for a while now. – Randy Simon Mar 17 '10 at 17:48
  • 2
    @GreenieMeanie: That's the nature of EL and usually shouldn't harm. If you want type safety, use JSF components instead. If it troubles, ask a new question how it hurts then we'll provide solutions/workarounds. By the way, using JSTL in JSF is not always considered a good practice. – BalusC Mar 17 '10 at 17:49
  • @Balus: The whole JSP/JSF/EL/JSTL/Facelets thing is a mess and so hard to keep track of the exact terms and versions these days... – GreenieMeanie Mar 17 '10 at 17:49
  • @GreenieMeanie: then you may find this answer useful: http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp/2097732#2097732 – BalusC Mar 17 '10 at 17:50
0

On JSF xhtml page, I use technics to reduce number of characters to type !

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      >

    <!-- JSF ViewController of this page -->
    <c:set var="vC" value="#{optionsViewController}"/> 
...
    <h:outputText 
        value="#{vC.txtOriginator.value}"  
        rendered="#{vC.txtOriginator.protected}"
        />

instead of

<h:outputText 
    value="#{optionsViewController.txtOriginator.value}"  
    rendered="#{optionsViewController.txtOriginator.protected}"
    />

Instead of typing optionsViewController more than 100 types, I write define only vC JSTL variable one time at begin of my xhtml file and use it each time I use optionsViewController.

OTHER advantages:

  1. The xhtml code is more short and more readable.

  2. When I copy some lines of code using Paste/Copy between distinct xhtml pages, vC variable must not be replaced !

schlebe
  • 3,387
  • 5
  • 37
  • 50