有没有办法返回位于.jar存档中的JavaScript?目前我有以下结构:
webapp
resources
scripts
SomeJavaScript.js
...
Run Code Online (Sandbox Code Playgroud)
这种.js文件的列表非常大.并且有一个包含所有此类文件的.jar文件.
在Spring配置文件中我有:
<mvc:resources mapping="/resources/**" location="/resources/"/>
Run Code Online (Sandbox Code Playgroud)
通过我的调度程序servlet处理所有静态资源.但是,我希望它能从.jar存档中读取JavaScript文件.最简单的方法是什么?
我认为为此目的编写自己的控制器不是最好的选择.
PS:我找到了以下解决方案:
我正在使用嵌入式Tomcat 7,使用Maven插件启动它.这里提到资源文件需要在下面WEB-INF/lib/{\*.jar}/META-INF/resources但是查看内部spring-js-resources.jar,实际位置是WEB-INF/lib/{\*.jar}/META-INF/web-resources.生成的页面包含以下链接:
<script type="text/javascript" src="/SomeProjectName/dojo/dojo.js"></script>
Run Code Online (Sandbox Code Playgroud)
此文件不可用.我该怎么做才能解决问题?
谢谢.
这是一个简单的服务示例,其方法返回reader:
trait Service1_1{
def s1f1:Reader[Map[String,Int],Int] =
Reader(_("name"))
def s1f2:Reader[Map[String,Int],Int] =
Reader(_("age"))
}
Run Code Online (Sandbox Code Playgroud)
这是一个服务使用者,它接受参数,映射并返回读取器本身:
trait Service1_2 {
def s12f1(i:Int, map:Map[String,Int]):Reader[Service1_1, Int] =
Reader(s => {
val r = for {
r1 <- s.s1f1
r2 <- s.s1f2
} yield r1 + r2
r.run(map) + i
})
}
Run Code Online (Sandbox Code Playgroud)
好的,要使用Service1_2.s12f1,我必须在参数列表中具有map:
object s1 extends Service1_1
object s2 extends Service1_2
val r = s2.s12f1(3, Map("age"-> 1, "name"-> 2)).run(s1)
Run Code Online (Sandbox Code Playgroud)
问题:如何实施Service1_2.s12f2:
trait Service1_2 {
def s2f2 = ???
}
Run Code Online (Sandbox Code Playgroud)
为了能够像这样运行它:
s2.s2f2(2)
.run(s1)
.run(Map("age"-> 1, "name"-> 2)) …Run Code Online (Sandbox Code Playgroud) dependency-injection scala composition reader-monad scala-cats
import cats.data.ReaderT
import cats.instances.either._
trait Service1
trait Service2
case class Cats(name:String)
type FailFast[A] = Either[List[String], A]
type Env = (Service1, Service2, Cats)
type ReaderEnvFF[A] = ReaderT[FailFast, Env, A]
def toReaderEnvFF[A](input:A):ReaderEnvFF[A] =
ReaderT((_:Env) => Right(input))
def c:ReaderEnvFF[Cats] =
for {
cats <- toReaderEnvFF((_:Env)._3)
} yield cats // This line is 26
Run Code Online (Sandbox Code Playgroud)
错误:
错误:(26,11)类型不匹配;找到:T1.this.Env => com.savdev.Cats(展开为)(((com.savdev.Service1,com.savdev.Service2,com.savdev.Cats)))=> com.savdev.Cats需要的猫:com .savdev.Cats}产生猫
您能解释一下,为什么猫不是com.savdev.Cats吗?以及为什么在错误中说它被扩展为具有return方法的功能[Cats],而不是botFailFast[Cats]
我尝试应用与此处完全相同的逻辑:
trait Service1 { def s1f = Option(10) }
trait Service2 {
type ReaderS1[A] = ReaderT[Option,Service1,A]
import cats.syntax.applicative._ …Run Code Online (Sandbox Code Playgroud) 我有一个任何一个列表,它代表错误:
type ErrorType = List[String]
type FailFast[A] = Either[ErrorType, A]
import cats.syntax.either._
val l = List(1.asRight[ErrorType], 5.asRight[ErrorType])
Run Code Online (Sandbox Code Playgroud)
如果所有这些都正确,我想获得 [A] 的列表,在这种情况下 - List[Int]
如果有任何Either剩余,我想合并所有错误并返回它。
我在 [ How to reduce a Seq[Either[A,B]] to a Each[A,Seq[B]]找到了一个类似的主题
但那是很久以前的事了。例如,其中一个答案提供使用partitionMap,我目前找不到。可能有更好、更优雅的解决方案。使用 scala-cats 的例子会很棒。
我想如何使用它:
for {
listWithEihers <- someFunction
//if this list contains one or more errors, return Left[List[String]]
//if everything is fine, convert it to:
correctItems <- //returns list of List[Int] as right
} yield correctItems
Run Code Online (Sandbox Code Playgroud)
这个 for-comprehension 的返回类型必须是:
Either[List[String], List[Int]]
Run Code Online (Sandbox Code Playgroud) type MapReaderOrOption[A] = ReaderT[Option, Map[String,String], A]
Run Code Online (Sandbox Code Playgroud)
我可以从创建它ReaderT.apply:
def f:MapReaderOrOption[Int] = ReaderT(_ => Option(10))
Run Code Online (Sandbox Code Playgroud)
从A类型通过类型丰富和纯方法:
import cats.Applicative
import cats.syntax.int._
def f:MapReaderOrOption[Int] = 10.pure[MapReaderOrOption]
Run Code Online (Sandbox Code Playgroud)
我想找到类似的东西。每次使用ReaderT(..)都不那么方便。当然,我可以创建一个辅助方法。问题是,还有其他选择吗?
预期类似:
def f:MapReaderOrOption[Int] = Option(10).asReaderT[MapReaderOrOption]
Run Code Online (Sandbox Code Playgroud) 一方面,有一个新的@RunWith注释,可以动态更改单元测试框架.但另一方面,Spring文档说org.springframework.test.annotation.ExpectedException:
因此,我的代码将取决于单元测试框架.请解释一下.
第二个问题.目前我使用Spring @RunWith注释实现测试.但我还将jUnit特定org.junit.Test注释添加到每个测试方法中.再次,如果我正确理解最好的方法 - 编写测试,那么我可以将例如jUnit更改为TestNg.春天@RunWith帮助我做到这一点.但是如何避免使用org.junit.Test注释呢?
当在类定义中有可变的私有字段,并且公开它们的getter / setter时,我们得到的异常如下所示:
[INFO] path.getInsertDate() may expose internal representation by returning Ttt.insertDate path.Ttt] At Ttt.java:[line 119]
Run Code Online (Sandbox Code Playgroud)
我添加了一个可变的类:
public class Test {
public String test;
}
Run Code Online (Sandbox Code Playgroud)
添加了此Test类和getters / setter方法的私有字段。但是finbugs很喜欢它。
public class ExposingTest {
private Test test;
//No warning here.
public Test getTest() {
return test;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么对于此警告,Findbugs仅检查Java标准库类,而不检查用户定义的类?有办法控制吗?
In the Scala with Cats:
import scala.concurrent.{Future, ExecutionContext}
implicit def futureFunctor
(implicit ec: ExecutionContext): Functor[Future] = …
Run Code Online (Sandbox Code Playgroud)
Whenever we summon a Functor for Future , either directly using Functor.apply or indirectly via the map extension method, the compiler will locate futureFunctor by implicit resolu on and recursively search for an ExecutionContext at the call site. This is what the expansion might look like:
// We write this:
Functor[Future]
// The compiler expands to this first:
Functor[Future](futureFunctor)
// And then to …Run Code Online (Sandbox Code Playgroud) 我试图获得天数差异,将结果转换为小数:
SELECT
CAST( TO_DATE('2999-01-01','yyyy-mm-dd') - TO_DATE('2909-01-01','yyyy-mm-dd') AS DECIMAL )
;
Run Code Online (Sandbox Code Playgroud)
现在,如果我在第二个日期上添加 1 个月:
SELECT
CAST( TO_DATE('2999-01-01','yyyy-mm-dd') - (TO_DATE('2909-01-01','yyyy-mm-dd') + INTERVAL '1 MONTH' * (1) ) AS DECIMAL )
;
Run Code Online (Sandbox Code Playgroud)
我收到错误: 错误:无法将类型间隔转换为数字
好的,我可以转换为 char 以获得结果:
SELECT
CAST( TO_CHAR( TO_DATE('2909-02-10','yyyy-mm-dd') - (TO_DATE('2909-01-01','yyyy-mm-dd') + INTERVAL '1 MONTH' * (1) ), 'DD') AS DECIMAL )
;
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,使用 TO_CHAR 转换修改的第一个查询停止工作:
SELECT
CAST( TO_CHAR(TO_DATE('2999-01-01','yyyy-mm-dd') - TO_DATE('2909-01-01','yyyy-mm-dd'), 'DD') AS DECIMAL )
;
Run Code Online (Sandbox Code Playgroud)
我收到错误:多个小数点。
所以,我的问题是,如何使用相同的 sql 语句获得天数?对于这两个 sql 查询。
这是方法的定义,它ExecutionContext隐式使用,并允许客户端覆盖它.两个执行上下文用于测试它:
val defaultEc = ExecutionContext.fromExecutor(
Executors.newFixedThreadPool(5))
Run Code Online (Sandbox Code Playgroud)
线程名称如下所示:'pool-1-thread-1'到'pool-1-thread-5'
而斯卡拉的第二个:
scala.concurrent.ExecutionContext.Implicits.global
Run Code Online (Sandbox Code Playgroud)
线程名称如下所示:'scala-execution-context-global-11'
客户端可以覆盖默认隐式通过:
implicit val newEc = scala.concurrent.ExecutionContext.Implicits.global
Run Code Online (Sandbox Code Playgroud)
不幸的是,只有在没有调用带隐式的方法时,它才可以覆盖():
val r = FutureClient.f("testDefault") //prints scala-execution-context-global-11
Run Code Online (Sandbox Code Playgroud)
不工作:
val r = FutureClient.f("testDefault")() //still prints: pool-1-thread-1
Run Code Online (Sandbox Code Playgroud)
问题是为什么它以这种方式工作?因为它使API的客户端变得更加复杂
以下是运行和播放的完整代码:
object FutureClient {
//thread names will be from 'pool-1-thread-1' to 'pool-1-thread-5'
val defaultEc = ExecutionContext.fromExecutor(
Executors.newFixedThreadPool(5))
def f(beans: String)
(implicit executor:ExecutionContext = defaultEc)
: Future[String] = Future {
println("thread: " + Thread.currentThread().getName)
TimeUnit.SECONDS.sleep(Random.nextInt(3))
s"$beans"
}
}
class FutureTest {
//prints thread: pool-1-thread-1
@Test …Run Code Online (Sandbox Code Playgroud) scala ×6
scala-cats ×5
reader-monad ×3
implicit ×2
java ×2
composition ×1
either ×1
findbugs ×1
future ×1
immutability ×1
javascript ×1
junit ×1
overriding ×1
postgresql ×1
spring ×1
spring-mvc ×1
sqldatatypes ×1
testing ×1
validation ×1
webresource ×1