scala, spring, and scalate

2019-07-01 10:01发布

问题:

I have a web project written in scala, spring, sitemesh, and jspx/jstl. The problem is with the jspx. I keep running into issues where it wants the types to be java collection types and so I have to keep converting between scala/java collections. And sometimes I forget, and my view blows up, etc.

I'm wondering if there's a blog post out there somewhere that describes how/what's involved to migrate from jsp/jstl/jspx to scalate? Because otherwise it just seems a bit overwhelming to switch, no matter now annoying I think jspx is.

回答1:

Ok so I took the plunge and just tried to see how this whole thing works. It turns out to be fairly easy. Here's the steps for anyone interested:

Maven pom dependencies:

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-compiler</artifactId>
        <version>${scala.version}</version>
    </dependency>

    <!-- scalate templating engine -->
    <dependency>
        <groupId>org.fusesource.scalate</groupId>
        <artifactId>scalate-spring-mvc</artifactId>
        <version>${scalate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.scalate</groupId>
        <artifactId>scalate-wikitext</artifactId>
        <version>${scalate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.scalate</groupId>
        <artifactId>scalate-page</artifactId>
        <version>${scalate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.scalamd</groupId>
        <artifactId>scalamd</artifactId>
        <version>${scalamd.version}</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.scalate</groupId>
        <artifactId>scalate-test</artifactId>
        <version>${scalate.version}</version>
        <scope>test</scope>
    </dependency>

mvc-servlet.xml:

<bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>
<bean id="scalateViewResolver" class="org.fusesource.scalate.spring.view.ScalateViewResolver"
    p:order="1" p:prefix="/WEB-INF/view/" p:suffix=".scaml" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     p:order="2" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/view/" p:suffix=".jspx" />

Then I renamed some jspx and started translating code. To get access to model objects I do this at the top of my scaml file:

-@ var x:String
-@ var y:List[com.xxx.model.MyModelObject]

Then I just followed the scaml docs. Super easy. The only thing that was awkward was trying to put inline javascript...it complained about indentation or something. So I moved that out to a separate file.

Didn't have to remove sitemesh at all (but I can in the future whenever I'm ready), and I can migrate jspx files at my leisure. Couldn't be easier.