scalatra是否在幕后使用抑扬(或反之亦然)?它们之间的主要区别是什么,你会使用哪一个?
这两个框架都受到Sinatra的启发,并且外观相同.
使用scalatra请求路由:
class ScalatraExample extends ScalatraServlet {
get("/date/:year/:month/:day") {
<ul>
<li>Year: {params("year")}</li>
<li>Month: {params("month")}</li>
<li>Day: {params("day")}</li>
</ul>
}
Run Code Online (Sandbox Code Playgroud)
circumflex中的示例代码:
class Main extends RequestRouter {
get("/posts/:id") = "Post #" + uri("id")
}
}
Run Code Online (Sandbox Code Playgroud) 如何将scala或scalatra部署到Jetty servlet容器上?有没有人有经验或可以指向我在线的一些资源?
我想从我的scalatra'控制器'调用另一个内部URL.我无法进行简单的重定向,因为有一些安全设置意味着用户只能访问第一个网址.
有没有办法做到这一点?
我正在查看Scalatra的文档,并注意到一段有趣的代码片段,我还没有看到过:http://www.scalatra.org/2.2/guides/persistence/introduction.html
具体来说,就是这样:
trait DatabaseSessionSupport { this: ScalatraBase =>
import DatabaseSessionSupport._
Run Code Online (Sandbox Code Playgroud)
除了this: ScalatraBase =>细分之外,这里的一切都很有意义.这有什么意义?它是针对下面的导入还是针对整个特征的?
有没有人成功使用Maven和Scalatra 2.2.1?
我发现这个旧的原型https://github.com/Srirangan/scalatra-maven-prototype并试图更新依赖版本,但我一直在遇到不兼容问题.
我想用:
我认为Jetty在制造战争方面存在一些问题.
我非常感谢你对这一点的帮助.
这是我正在使用的示例项目:http: //www.scalatra.org/2.2/getting-started/first-project.html
我目前的pom.xml:
<?xml version='1.0' encoding='UTF-8'?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-scalatra-web-app</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<scalatra.version>2.2.1</scalatra.version>
</properties>
<build>
<finalName>scalatra-maven-prototype</finalName>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.17</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.scalatra</groupId> …Run Code Online (Sandbox Code Playgroud) 这通常是一个直接的问题,但我不完全确定如何在Scala中解决这个问题,因为它是类型敏感的.我有一个类,我设置一个简单的连接到我的MongoDB实例.现在在本地,我做了一个神奇的概念,只是评论生产配置行并取消注释本地/开发者.以下是每个人的样子:
// production
object MongoReplicaConnection {
def mongoDb = {
val addresses = List(new ServerAddress("10.1.1.2" , 27017), new ServerAddress("10.1.1.3" , 27017), new ServerAddress("10.1.1.4" , 27017))
val mongoConn = MongoConnection(addresses)
val db = mongoConn("mydb")
db
}
}
// local development
object MongoReplicaConnection {
def mongoDb = {
val mongoConn = MongoConnection()
val db = mongoConn("mydb_local")
db
}
}
Run Code Online (Sandbox Code Playgroud)
就传入数据库名称而言,这是微不足道的 - 它只是从配置文件中获取一个字符串.我无法想到一个干净的解决方案是如何处理我使用List of ServerAddress实例来初始化MongoConnection()与在本地/ dev设置中不将任何List传递给MongoConnection这一事实.
你怎么能在这里使用类型安全配置这样的工具?我假设我可以将一个空的List /数组传递给MongoConnection()以获取本地数据库连接,但我不完全确定如何在不修改mongoDb函数的情况下配置生产数据库.我一直在寻找一个这样的工具来帮助我完成这个过程:https://github.com/typesafehub/config
我正在使用Jetty 7.6和Scalatra Web框架.在一些请求中,我需要将一个大文本作为响应体发送给客户端,我使用HttpServletResponse.getWriter()来编写响应.
我注意到在客户端我收到413 Header Full错误.显然,Jetty中解决这个问题的一个方法是增加jetty的header-buffer-size值.
我想知道HttpServletResponse.getWriter()与请求标头的大小有什么关系?!据我所知,HttpServletResponse.getWriter()写入响应体而不是响应头.
如果有人能解释这个问题我很感激.
这是一个两部分问题,首先是设计问题而不是如何实现它,其次是Akka的一些实现问题.
我正在使用Scalatra构建一个REST服务端点,当被调用时,它将从多个源中提取图像,操纵它们并返回它们.这可能是一个相当长的运行过程,并且可能会更长,然后单个http请求/响应周期可以接受.
我的想法是,当调用时,我将启动一堆akka Actors来提取图像资源,让他们将结果交给Image处理Actors进行扩展等.初始请求本身会立即返回某种形式处理ID,可用于对后续轮询调用另一个端点,该端点将在处理结果时返回结果,并带有一个标志,用于确定是否有更多结果可供客户端知道停止轮询.
我的问题如下:
实施部分
假设我最终得到了类似于上面的设计,我对Scalatra&Akka(或任何Actor范例)都很新,我有几个问题.
好的,第一个是我认为的Scala/Scalatra特定问题.好的,我查看了akka http://www.scalatra.org/guides/async/akka.html上的Scalatra文档,在这个例子中,他们设置了应用程序引导程序,如下所示:
// Get a handle to an ActorSystem and a reference to one of your actors
val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])
// In the init method, mount your servlets with references to the system
// and/or ActorRefs, as necessary.
override def init(context: ServletContext) {
context.mount(new PageRetriever(system), "/*")
context.mount(new MyActorApp(system, myActor), "/actors/*")
}
Run Code Online (Sandbox Code Playgroud)
我假设scalatra的bootstraping在应用程序启动时发生一次,system.actorOf(Props [MyActor])是否为每个请求创建一个实例或一个实例?
其次,说我的MyActor类(或更明智的名字)做了以下事情:
class MyActor extends Actor {
def receive = …Run Code Online (Sandbox Code Playgroud) 默认情况下,Scalatra期望"webapp"目录位于src/main/webapp.怎么可能改成,例如content/doc-root?
sbt允许使用以下内容自定义其默认目录:
scalaSource <<= (baseDirectory)(_ / "src")
Run Code Online (Sandbox Code Playgroud)
所以我认为这只是知道使用正确的"配置键"的问题......
我正在使用Scalatra,但这个问题应该适用于任何Scala编程.我来自Ruby on Rails背景.简单地说,使用诸如XML Builder或jsonbuilder(https://github.com/rails/jbuilder)之类的模板化系统,我可以通过创建模板来完全控制RESTful API中的JSON或XML输出.以下:
Jbuilder.encode do |json|
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format: :json)
end
if current_user.admin?
json.visitors calculate_visitors(@message)
end
json.comments @message.comments, :content, :created_at
json.attachments @message.attachments do |attachment|
json.filename attachment.filename
json.url url_for(attachment)
end
end
Run Code Online (Sandbox Code Playgroud)
这里的理想是,我将一个@message对象与控制器+动作中所需的任何逻辑组合在一起.这会传递给具有逻辑的模板,例如if current_user.admin?包含一些东西,否则不会.
什么是Scala或Scalatra中可用的类似工具?我知道serializer会让我覆盖从特定模型生成的JSON或XML,但这在Ruby中是相同的(如果我错了,请纠正我)作为覆盖as_json或as_xml.然而,有时候,模板要复杂得多,包括多个模型,特定的数据结构,特定的数据排序等.这就是我需要的灵活性.目前有一个工具可以在Scala/Scalatra环境中进行这样的模板化吗?