用Scala 开发 Spring MVC

      最近尝试用scala来写spring应用。工程是经过ant来编译部署的,由于经过eclipse直接部署到tomcat的话,scala文件不会被编译。后来才发现Spring已经有一个在开发中的项目 Spring Scala。后面有时间会尝试使用一下 java

环境及工具:eclipse4.2,eclipse scala-2.10插件,spring3.1.2.RELEASE,tomcat7 node

deplib: 存放scala 的相关jar包,在ant编译scala文件的时候会用到。 git

dist: ant 打包生成的目标目录 github

lib:spring web工程相关jar web

src/main/webapp:web工程的webapp目录,存放 web.xml 页面等等 spring

Ant 的相关配置:

     ant的相关配置须要添加对scala编译的支持,能够参考 http://www.scala-lang.org/node/98 官网给的相关说明。 tomcat

    1. 添加scala编译的ant tool app

<taskdef resource="scala/tools/ant/antlib.xml">
    <classpath>
	<pathelement location="deplib/scala-compiler.jar" />
	<pathelement location="deplib/scala-library.jar" />
	<pathelement location="deplib/scala-reflect.jar" />
    </classpath>
</taskdef>
    2. 在编译的时候优先编译 scala文件,由于好比若是你的代码中混用java和scala的话,在java去集成scala定义的接口的时候,不先编译出scala的trail,先编译java的话,会报错。
<target name="compile" depends="clean,mkdir">

    <scalac srcdir="${java.src.dir}" destdir="${class.dir}" >
	<compilerarg line="-encoding UTF-8 " />
	<classpath refid="classpath" />
    </scalac>

    <javac srcdir="${java.src.dir}" destdir="${class.dir}" target="1.6"
		source="1.6" debug="on">
	<compilerarg line="-encoding UTF-8 " />
	<classpath refid="classpath" />
    </javac>
</target>
   3. 打包的时候,将 scala-library.jar 文件也放入到 WEB-INF/lib 下面。否则会有找不到class的错误。

这样打包之后,就放到tomcat里面直接跑就能够了。不过这样不知道怎么去支持热部署  ,有知道的朋友望告知一下。不过jrebel对scala是免费的,这个还没去研究怎么整合进来。 eclipse

package me.ours.controllers

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView

@Controller
class IndexController {

  @RequestMapping(Array { "/" })
  def index(): String = "index"
    
  
  @RequestMapping(Array {"/test"})
  def test(): ModelAndView ={
    return new ModelAndView("index","message","Scala!!")
  }
}
上面是用scala写的一个spring的controller。若是有人须要工程的话,我能够分享出来。
相关文章
相关标签/搜索