Scala Collection in JSF


Question: 

I have developed a Scala and JSF application for learning purpose. In this app I have to convert all my Scala collection objects to Java cllectios before it get rendered in JSF. Is there any easy way this can be achived with something like ScalaElResolver, if yes anybody have a sample code for ScalaElResolver. Thanks in Advance Philip




2 Answers: 

This code is based on ScalaElResolver by Werner Punz. I have stripped it down, so it just handles the conversion from a Scala Iterable to a java.lang.Iterable:

class SimpleScalaElResolver extends ELResolver {
  override def getValue(elContext: ELContext, base: AnyRef, 
                        prop: AnyRef): AnyRef = {
    println(s"SimpleElResolver: getValue: Entering: $base.$prop")
    if (base == null) {
      null
    } else {
      val method = base.getClass.getDeclaredMethod(prop.toString)
      if (method != null) {
        val res = method.invoke(base)
        if (res.isInstanceOf[Iterable[_]]) {
          val iter = res.asInstanceOf[Iterable[_]]
          println("getValue: Wrapping as Java iterable")
          elContext.setPropertyResolved(true)
          JavaConversions.asJavaIterable(iter)
        } else {
          null
        }
      } else {
        null
      }
    }
  }

This is sufficient to get it running using sbt and its web plugin (which uses jetty under the hood), even if all other methods remained "not yet implemented", like this:

override def getCommonPropertyType(elContext: ELContext, o: AnyRef): Class[_] = {
  ???
}

The other methods were not called in my case.

I've tested it only from within a .jspx; as far as I know this should work with JSF as well.


Example: If you have a class

class Model(val list: List[Int])

and in your controller

val model = new Model(List(1))

httpRequest.setAttribute("model", model)

you can access the instance in EL

        <ul>
            <c:forEach var="i" items="${ model.list }">
                <li>
                    <c:out value="${ i }"/>
                </li>
            </c:forEach>
        </ul>

so the property name in EL exactly matches the name of the val in your model class. Otherwise you get a java.lang.NoSuchMethodException.

 

Hi I just opened a scalaelresolver project on github, https://github.com/werpu/scalaelresolver the resolver does besides resolving scala properties also the collection conversions. An example is included.

 

More Articles


php - how to set a page active automatically when it is loaded

i want to change the active class when each page loaded. my jquery code is$(document).ready(function () {$('.nav li a').click(function(e) { $('.nav li a.active').removeClass('active'); $(this).addClass('active'); e.preventDefault();});});and my html code is<ul class="nav nav-stacked bg-n

reactjs - What is the benefit of @emotion/core over emotion for a React project?

If I install emotion then the API is nice and clear: package.json:"dependencies": { "emotion": "^10.0.9",React component: import React from "react";import { css } from "emotion";const someStyle = css` display: none;`function MyComponent() { return ( <div className={someStyle} /> );}Ho

specifications - Essential techniques for pinpointing missing requirements?

An initial draft of requirements specification has been completed and now it is time to take stock of requirements, review the specification. Part of this process is to make sure that there are no sizeable gaps in the specification. Needless to say that the gaps lead to highly inaccurate estimates,


javascript - jQuery .load: any way to load the same page when page is refreshed

So I have a website that loads pages to a container div:function goto(addr) { $("#content").load(addr);}and a link that executes it<a href="#" id="aboutus" onclick="goto('page/aboutus.php');">About us</a>My problem is that whenever the page is refreshed, the loaded content resets to t

cookies - PHP Unique Computer ID

Is it possible for php(or javascript in the worst case) to create a unique id for a user that is not cookie or ip dependant. I have seen on myminicity.com that on each city the count only goes up once a day(it has a unique id for everyone i think) and even if I delete my cookies and refresh ip it st

css - How to add equal padding between buttons?

http://buttonspace.com/I want to put equal space between the 4 buttons at the top, but I'm having trouble. At first the "social-buttons" id was in a DIV floating left, but that caused the slider to get pushed to the right.So I changed everything to and now the padding style doesn't seem to have an


javascript - Why are props being shared between seperate instances of an emotion-js component?

Why does emotion-js appear to share the props from seperate component instances?<Button isDisabled={'true'} /><Button />const Button = styled.button` background-color: ${props => props.isDisabled !== 'true' ? 'green' : 'grey'};`;export default (props) => { return (<Button>

mysql - PHP-FPM crashes when having too many users while doing a heavy job

I have a Server running Apache/2.2.22 (Debian), PHP 5.6.17 as FPM and MySQL 5.6.25.The project runs using a CMS called Redaxo (I don't think it's that important, but I'll tell ya anyway). In Redaxo there are some functions which take some time (e.g. deleting cache and rebuilding it takes 1-2 minutes

Java's "forEach" to Scala

How can the following be converted to scala? If I live it as it is, I'm getting a big type mismatch expected.entrySet().forEach(entry -> {..})I tried specifying entry to java.util.Map.Entry, or changing to scala foreach, doesn't work.Let me know if you need any more info/code, and I'll create som

macos - What is the difference between installing python from the website and using brew?

I have a Mac with OSX 10.11.6. I used brew to install python3. It installed python 3.5.2, but I need python 3.5.1. I've been googling, but can't figure out how I would install 3.5.1 via brew. So I went to python.org and downloaded the python-3.5.1-macosx10.6.pkg. I searched for how installing python