Use c:set to set a non-string value
Asked Answered
A

2

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?

Arrhythmia answered 17/3, 2010 at 17:33 Comment(0)
E
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}"/>
Expertism answered 17/3, 2010 at 17:43 Comment(6)
+1 although I corrected that you incorrectly called it JSTL instead of EL. JSTL is a taglib as outlined here java.sun.com/products/jsp/jstl/1.1/docs/tlddocs, EL are those ${} things as outlined in this JSP/EL spec: jsp.dev.java.net/spec/jsp-2_1-fr-spec-el.pdfJaninajanine
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.Arrhythmia
Thanks, just a typo. My bad. I've been out of Java land for a while now.Expertism
@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.Janinajanine
@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...Arrhythmia
@GreenieMeanie: then you may find this answer useful: #2095897Janinajanine
B
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 !

Bedside answered 16/8, 2018 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.