본문 바로가기

Scala

Playframework 2.4.x 에서 Intellij 프로젝트 import 할 때~!! object index is not a member of package views.html Ok(views.html.index("Your new application is ready.")) 위와 같은 에러가 발생 할 때 target/scala-2.11/twirl/main 를 추가해보자 더보기
[Scala] Currying 간단하게 설명하면 여러 개의 인자를 받는 함수에 일부 인자값을 넣어서 다시 함수로 만드는 것이다. (참고사이트) object CurryTest extends App { def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) val nums = List(1, 2, 3, 4, 5, 6, 7, 8) println(filter(nums, modN(2))) println(filter(nums, modN(3)))} modN의 함.. 더보기
[Play Framework] Scala Test 1. Test 폴더에 Scala 테스트 클래스를 작성한다. 2. 관련 라이브러리 importimport play.api.test._ import play.api.test.Helpers._ 3. Application 상황에서 테스트를 하고 싶다면val fakeApplicationWithGlobal = FakeApplication(withGlobal = Some(new GlobalSettings() { override def onStart(app: Application) { println("Hello world!") } })) 4. 테스트 Object 작성object ExamplePlaySpecificationSpec extends PlaySpecification { "The specification" sh.. 더보기
[Scala] Generic function 제네릭과 함수를 같이 사용하는 예제이다. def test [T](text:T) (block:(T) => String): Result = { val resultStr = block(text) Ok(Json.obj("result" -> "SUCCESS","object" -> resultStr)) } 함수 사용하기test("aaaaa") { inputText => "bbbbbbb" } 더보기
[Play Framework2] Json polymorphism Scala에서는 동적으로 Json 으로 변환하기가 힘들다. 그래서 찾은 방법이 trait을 만들어서 상속을 통한 방법이다. trait Person object Person { implicit val shapeWrites = Writes[Person] { person => person match { case student: Student => Json.writes[Student].writes(student) case teacher: Teacher => Json.writes[Teacher].writes(teacher) case parent: Parent => Json.writes[Parent].writes(parent)}} } @Entity@Table (name = "teacher")case class Te.. 더보기
[Play Framework2] JPA & Json model Java는 POJO 객체로 JPA와 Json이 아주 자유로웠지만 Scala에서는 정말 헤매는 부분이 많았다. 그리고 본인이 가장 간편하다고 생각하는 방법을 설명하겠다. 특별히 Play Framework2 이외에 다른 라이브러리를 사용하지 않고 내부적인 라이브러리를 통해 쉬운 방법을 찾았다. (one to many 와 many to many 는 아직 방법을 찾지 못함..) import play.db.jpa._import javax.persistence._import play.api.libs.json._import play.api.libs.functional.syntax._import scala.annotation.meta.field @Entity@Table (name = "class")case class .. 더보기
[Play Framework2] Json Java에서는 Pojo 객체를 그대로 Json으로 출력하기 쉬웠지만... Scala에서는 그게 힘든것 같다 아무래도.. Java의 Reflection과 같은 기능이 없어서 그런 것 같다. 그래서 가장 간단하게 Json으로 출력하는 방법을 찾아봤다. @Entity@Table (name = "child")class Child { @Id @GeneratedValue var id : Long =_ @Column(name = "name") var name : String = _} object Child { implicit val childWriters = new Writes[Child] { def writes (child : Child) = Json.obj( "no" -> child.id, "name" -> ch.. 더보기
[Play Framework2] JPA Play Framework2 Scala 에서도 JPA를 지원한다. 설치부터 사용방법 까지 간단하게 설명하자면..(참고 URL =https://www.playframework.com/documentation/2.4.0/JavaJPA) 우선은 라이브러리를 설치한다.libraryDependencies ++= Seq( javaJpa, "org.hibernate" % "hibernate-entitymanager" % "4.3.9.Final" // replace by your jpa implementation ) 그리고 persistence.xml 파일은 생성하고 conf/MATA-INF 폴더에 넣자 없다면 생성해서 넣어두자그리고 파일내용은 아래와 같이 작성한다. org.hibernate.jpa.HibernateP.. 더보기
[Play Framework2] Comet sockets Comet response 란 text/html 로 이루어지고 태그에 감싸여진 응답을 말한다고 한다.... 본인은 처음 알았음..그래서 이걸 쉽게 쓸 수 있는 방법을 소개한다. 참고 사이트 : https://www.playframework.com/documentation/2.3.x/ScalaComet def comet = Action { val events = Enumerator( """""", """""", """""" ) Ok.chunked(events).as(HTML) }위의 코드를 웹 브라우저에서 실행결과로 받아보면 콘솔창에 kiki, foo, bar가 출력되어지는 걸 확인 할 수 있다.이걸 이제 간편화 하는 단계를 살펴보자 import play.twirl.api.Html // Transform .. 더보기
[Play Framework2] Streaming HTTP responses Streaming 기능에 대해서 실제로 구현해서 만들어 본 적이 없는데 이번에 PlayFramework에서 아주 간단하게 제공하는 방법이 있다. 참고 사이트 : https://www.playframework.com/documentation/2.3.x/ScalaStream def index = Action { val file = new java.io.File("/tmp/fileToServe.pdf") val fileContent: Enumerator[Array[Byte]] = Enumerator.fromFile(file) Result( header = ResponseHeader(200), body = fileContent ) }위의 코드로 간단한 스트리밍 기능? 이 구현이 완료가 되었고 혹시나 Enumer.. 더보기