3장 스칼라 에코시스템

SBT (Simpel Build Tool)

빌드 툴이다. 스칼라로 작성되어 있으며 스칼라를 위한 편리기능을 제공한다.

Get Started

$ mkdir hello
$ cd hello
$ echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > hw.scala
$ sbt
...
> run
...
Hi!

하위디렉토리에 컴파일된 class 파일과 기타 파일들이이 있는 'target','build.sbt' 등 생성된다.

addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.4.2")
  • build.sbt에 디펜던시 추가
seq(webSettings :_*)

libraryDependencies += "org.mortbay.jetty" % "jetty" % "6.1.22" % "container"

libraryDependencies += "javax.servlet" % "servlet-api" % "2.5" % "provided"

//XML 파서
libraryDependencies += "org.scala-lang.modules" % "scala-xml_2.11" % "1.0.5"
@scala.deprecated("In build.sbt files, this call can be removed.  In other cases, this can usually be replaced by Seq.")
def seq(settings : sbt.Def.Setting[_]*) : sbt.Def.SettingsDefinition = { /* compiled code */ }
  • seq는 deprecated되어 있음.
    • seq -> Seq
  • src.main.scala.simples.SimpleServlet.scalal 생성
package simples

import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}

class SimpleServlet extends HttpServlet{
  override def doGet(req: HttpServletRequest, resp: HttpServletResponse): Unit = {

    resp.setContentType("text/html")
    resp.setCharacterEncoding("UTF-8")

    val responseBody = <html><body><h1>Hello, World!!</h1></body></html>

    resp.getWriter.write(responseBody.toString)

  }
}
  • webapp/WEB-INF/web.xml 추가
<?xml version="1.0" encoding="UTF-8" ?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
         version="2.5">

    <display-name>HelloWorld Application</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>SimpleServlet</servlet-name>
        <servlet-class>simples.SimpleScalaServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SimpleServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
  • 명령어 실행
    • sbt container:start

SBT 주요 명령어

  • clean
  • compile
  • test
  • console
  • run
  • package
  • help
  • reload

Scala WorkSheet

  • Repl을 통한 즉시 피드백을 ide에서 이용
  • https://blog.outsider.ne.kr/846
    • 프로젝트 내부에 생성하기에 test 폴더와 같이 별도의 폴더에 작업하는게 좋을거 같다.

HTTP 다루기(Dispatch Lib)

libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.2"

Scala의 for문

  • Generator
  • Filter
  • Definition

Example 1

var i = 0

for (i <- 1 to 10) { //1 until 10
 println(i);
}

Example 2

for( a <- 1 to 3; b <- 1 to 3){
    println( "Value of a: " + a );
    println( "Value of b: " + b );
}

Example 3

for( a <- List(1,2,3,4) ){
    println( "Value of a: " + a );
}

The for Loop with yield

You can store return values from a for loop in a variable.

Example 1

val nums = for {
    el <- List(1,2,3,4)
} yield "T" + el

Example 2

var a = 0;

var retVal = for{ a <- List(1,2,3,4,5,6,7,8,9,10) 
    if a != 3; if a < 8
}yield a*10

Example 3

//Compile Error
for(a <- List(1,2,3,4)){
    b = a*10
} yield b*10

참고 { } vs ( )

  • link
  • 어디서 사용하냐에 따라 동일하게 적용되기도 하지만 문법적으로 확연히 둘은 다름
    • ex) xxx(a1, a2) != xxx{a1, a2}