我已经构建了我的第一个grails应用程序.我的URL映射是默认应用程序提供的:
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
Run Code Online (Sandbox Code Playgroud)
塞纳里奥
我有一个名为控制器,ColorController用行动save和list.它只是做这样的事情:
def save () {
def colorInstance = new Color(params)
colorInstance.save(flush: true)
}
def list () {
[colorList: Color.list, colorTotal: Color.count()]
}
Run Code Online (Sandbox Code Playgroud)
我想为这些操作构建一个简单的API.
问题
http://<domain>/<app>/rest/controller/action)当我运行 groovy 时出现以下错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Users\kbekur\MuleStudio\workspace\GroovyTest\src\com\test\SQLGroovy.groovy: 3: unexpected token: @ @ line 3, column 2.
@Grab(group='org.hsqldb', module='hsqldb', version='2.3.2')
^
1 error
Run Code Online (Sandbox Code Playgroud)
我的代码是:
@Grapes([
@GrabConfig(systemClassLoader = true)
@Grab(group='org.hsqldb', module='hsqldb', version='2.3.2')
])
import groovy.sql.Sql
def db = [url:'jdbc:hsqldb:hsql://localhost/testdb', user:'sa', password:'', driver:'org.hsqldb.jdbc.JDBCDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
println 'Some GR8 projects:'
sql.eachRow('select * from Persons') { row ->
println "${row.lastname.padRight(10)} ($row.personid)"
}
Run Code Online (Sandbox Code Playgroud)
经过分析,我发现
You can't annotate a statement.
You should put the grab annotation on an import, for instance.
Run Code Online (Sandbox Code Playgroud)
我不清楚上述声明,我需要进行哪些更改来解决编译问题并加载 …
我的项目有如下代码:
params.stringValue?.trim().replaceAll('aa', 'a')
Run Code Online (Sandbox Code Playgroud)
我们预计,如果params.stringValue为空,这两者 trim()并replaceAll()不会被调用.
但是我们NullPointerException在这一行上得到的结论replaceAll()是无法在null对象上调用.
我们必须将代码更改为:
params.stringValue?.trim()?.replaceAll('aa', 'a')
Run Code Online (Sandbox Code Playgroud)
为什么上面的第一个代码段不起作用?这是Groovy中的一个错误,它在一次遇到null之后继续评估表达式吗?
我想动态地创建一个带有一些参数的链接到一个页面,其中将使用这些值:
<td><a href="${createLink(controller:'display', action="viewer", params: ['file' : '${stream.file}', 'media' : '${stream.media}'])}">${fieldValue(bean: streamInstance, field: "media")}</a></td>
Run Code Online (Sandbox Code Playgroud)
在我的viewer.gsp我想在显示视频流中的链接使用这些值:
<source src="path/to/my/videodir/${media}" type='video/mp4'></source>
Run Code Online (Sandbox Code Playgroud)
但到目前为止我没有取得多大成功:-(.有人可以帮助我吗?
朋友我必须在项目中捏造一些东西,我发现了一些困难,如下:
String name1 = "Bharath" // its length should be 12
String name2 = "Raju" // its length should be 8
String name3 = "Rohan" // its length should be 9
String name4 = "Sujeeth" // its length should be 12
String name5 = "Rahul" // its length should be 11 " Means all Strings with Variable length"
Run Code Online (Sandbox Code Playgroud)
我有字符串和它们的长度.我需要输出如下格式.通过使用字符串连接和填充.我需要在Groovy中回答,即使Java也很好..
"Bharath Raju Rohan Sujeeth Rahul "
Run Code Online (Sandbox Code Playgroud)
手段:
Bharath以5个黑色空格作为lenth是12(7 + 5 = 12),
Raju从4个黑色空间开始,因为lenth是8(4 + 4 = 8),
Rohan向前开了4个黑色空格,因为lenth是9(5 + 4),
Sujeeth向前5个黑色空间,因为lenth是12(7 …
我正在使用GBench.对于常规方法基准测试,它非常酷.现在我想要的是对Recursive函数进行基准测试.
让我们说:
def recursive() {
//do something
return recursive()
}
Run Code Online (Sandbox Code Playgroud)
对于这个功能,我需要做Benchmark.添加@Benchmark方法顶部为每个递归调用提供基准.但我想要的是获得整个recursive方法的Benchmark .
有可能吗?
在我的项目的目标目录中,有一个名为stacktrace.log的文件.我已经意识到文件的大小已经超过3千兆字节.我删除这个文件是否安全?删除后是否会导致任何文件未找到异常?谢谢你的时间.
- -编辑
如果确实导致文件未找到异常,我该如何解决问题?
我在resources.groovy中定义了Spring bean.我可以在控制器中访问它们.我在视图中也有一些没有控制器的GSP.我想知道如何像在JSF中的EL表达式一样访问GSP中的bean?
例如,给定一个这样的域类:
class TestBean {
def name = "hello"
}
Run Code Online (Sandbox Code Playgroud)
在spring/resources.groovy:
test(mydomain.TestBean) { bean -> bean.scope = 'session' }
Run Code Online (Sandbox Code Playgroud)
在UrlMapping.groovy中:
'/test'(view:'/test')
Run Code Online (Sandbox Code Playgroud)
然后,在views/test.gsp中:
${test.name}
Run Code Online (Sandbox Code Playgroud)
但是上面的代码会引发异常,因为$ {test}为null.那么,如何在没有控制器的情况下在GSP中访问TestBean?
我正在使用Grails 2.2.谢谢!
我使用Grails 2.2.2和GGTS 3.3M1.当我运行我的应用程序(grails run-app)并在groovy文件中更改某些内容时,Grails会重新编译该文件.在此过程中会发生以下错误输出:
| Error Unexpected problem clearing ThreadGroupContext beaninfo:
| Error java.lang.ClassCastException: java.beans.WeakIdentityMap cannot be cast to java.util.Map
| Error at org.springsource.loaded.agent.JVMPlugin.reloadEvent(JVMPlugin.java:77)
| Error at org.springsource.loaded.TypeRegistry.fireReloadEvent(TypeRegistry.java:1594)
| Error at org.springsource.loaded.ReloadableType.loadNewVersion(ReloadableType.java:396)
| Error at org.springsource.loaded.TypeRegistry.loadNewVersion(TypeRegistry.java:805)
| Error at org.springsource.loaded.agent.ReloadableFileChangeListener.fileChanged(ReloadableFileChangeListener.java:51)
| Error at org.springsource.loaded.agent.Watcher.determineChangesSince(FileSystemWatcher.java:218)
| Error at org.springsource.loaded.agent.Watcher.run(FileSystemWatcher.java:205)
| Error at java.lang.Thread.run(Thread.java:722)
Reloading: JVMPlugin: warning: unable to clear BEANINFO_CACHE, cant find field
Run Code Online (Sandbox Code Playgroud)
编译似乎工作正常,但错误仍然发生.
Groovy断言包含:
assert testList.contains(4)
| |
| false
[1, 2, 6, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我疯了吗?
这是测试代码:
List testList = tester.getFactors(12)
assert testList.size() == 5
assert testList.contains(1)
assert testList.contains(2)
assert testList.contains(3)
assert testList.contains(4)
assert testList.contains(6)
Run Code Online (Sandbox Code Playgroud)
如果我删除除contains(4)和contains(6)之外的所有内容,则它们中的任何一个或两个都会失败.
这是getFactors方法:
List getFactors(int number)
{
def retList = new ArrayList();
(1..Math.sqrt(number)).each() { i ->
if(number % i == 0)
{
//add both the number and the division result
retList.add(i)
if(i>1)
retList.add(number / i)
}
}
retList;
}
Run Code Online (Sandbox Code Playgroud)
任何想法都非常感激.
grails ×5
groovy ×5
api ×1
benchmarking ×1
grails-2.0 ×1
gsp ×1
java ×1
rest ×1
spring ×1
string ×1