1

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

1

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

HiThanks you very much for the quick response.
I am getting an exception WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception java.lang.NoSuchMethodException: com.sun.faces.context.FacesContextImpl.externalContext() at java.lang.Class.getDeclaredMethod(Class.java:1956) at com.philipj.scala.web.booking.ScalaElResolver.getValue(ScalaElResolver.scala:18) at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) at com.sun.faces.el.DemuxCompositeELResolver.
@PhilipJ I have added an example to the answer. Please check if this solves your problem.
0

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.

1 Comment

Includes ownership disclosure, hence not spam.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.