我尝试将变量绑定到Groovy,并从Groovy返回zu Java:
Java代码:
Binding binding = new Binding();
binding.setVariable("SRESULT", "foo");
GroovyShell gs = new GroovyShell(binding);
gs.evaluate(script);
String sResult = (String) gs.getContext().getVariable("SRESULT");
System.out.println("FROM GROOVY: " + sResult);
Run Code Online (Sandbox Code Playgroud)
Groovy代码:
class Est {
static SRESULT
public static void main(String[] args) {
println 'From Java: '+SRESULT
SRESULT = 'bar'
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
From Java: foo
FROM GROOVY: foo
Run Code Online (Sandbox Code Playgroud)
我的问题:我想SRESULT在Groovy中进行更改并可以访问Java中的Value.
有谁能够帮我?
我有一个场景,将 char 值列表传递给 Grails 中的查询。
当我通过列表时,这就是发生的事情
def accounts = ['A', 'B']
AND acct.account_status_cd in '${accounts}
Run Code Online (Sandbox Code Playgroud)
查询看起来像“AND acct.account_status_cd in '[A,B]'”
但它应该是“AND acct.account_status_cd in ('A','B')”
如何实现这一目标?
我正在尝试测量应用程序的每个操作所花费的时间,因为我正在重建遗留系统.
现在我在控制器级别这样做:
def actionStart() {
session.startTime = new Date().getTime()
}
def actionEnd() {
def endTime = new Date().getTime()
timeHandler(endTime, session.startTime)
}
def timeHandler(end, start){
return end - start
}
Run Code Online (Sandbox Code Playgroud)
重要提示:我想将其移动到过滤器并在每个操作的开始和结束时自动执行它.
什么应该是最好的方法?提前致谢.
PS.:获取System.currentTimeMillis()和实例化对象有什么区别?性能?
我对RESTful api感到困惑. 码:
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
def isMailer = new HTTPBuilder( 'http://mailer-api.com' )
isMailer.request( GET, JSON ) {
uri.path = '/is/mail/rest/json/' + token
isMailer.auth.basic 'ddd', 'aaa'
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'
response.success = { resp, json ->
// response handler for a success response code:
System.out << json
if(json.has("DISP_NAME")) {
println "************************"
res = "Yes"
} else if (json.has("ListError")) {
res = "No"
}
}
}
// handler for any failure status code: …Run Code Online (Sandbox Code Playgroud) 我有这样一段代码:
private String getUsername(PersonalAccount account) {
User usr = (User)account?.usr
String name = usr?.getName()
return name
}
Run Code Online (Sandbox Code Playgroud)
在PersonalAccount类中我们有字段:
SimpleUser usr
Run Code Online (Sandbox Code Playgroud)
用户扩展了SimpleUser
这意味着什么:?在这两行?
User usr = (User)account?.usr
String name = usr?.getName()
Run Code Online (Sandbox Code Playgroud) 使用Grails OAuth插件需要在Config.groovy中提供绝对回调URL.但是,每个环境都有不同的serverURL.
有没有办法从Config.groovy内部获取当前环境,这是我想要做的一个例子:
def devServerUrl = 'http://dev.example.com'
def prodServerUrl = 'http://prod.example.com'
def currentServerUrl = grailsApplication.metadata.environment == 'development' ? devServerUrl : prodServerUrl;
environments {
development {
grails {
serverURL = devServerUrl
}
}
production {
grails {
serverURL = prodServerUrl
}
}
}
oauth {
providers {
runkeeper {
api = RunKeeperApi
key = 'key'
secret = 'secret'
callback = currentServerUrl + '/oauth/runkeeper/callback'
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!
commandObject当我提交表单时,我正在尝试使用它来验证我的数据.我可以在中验证hasMany关系吗commandObject?我的cenario是这样的.
牵引简单的classeswhith有很多关系:
class Book{
String nameBook
}
class Author{
String nameAuthor
static hasMany = [books:Book]
}
Run Code Online (Sandbox Code Playgroud)
很简单commandObject,我想在提交表单时验证hasMany.
@grails.validation.Validateable
class MyValidateCommand{
String nameAuthor
static hasMany = [books:Book]
static constraints = {
nameAuthor nullable:false
books nullable:false
}
}
Run Code Online (Sandbox Code Playgroud)
ps:我知道这个commandObject是错误的,它不编译.但我可以这样做吗?
我有一个从Groovy的HTTPBuilder返回的JSON对象.JSON包含一些表示为JSONNull对象的空值.问题是当我尝试在响应中渲染JSON时,我在尝试渲染JSONNull时出现错误.我得到的回复只是部分呈现的.我希望它呈现为"null".我该怎么做呢?
码:
render(contentType: "text/json") {
listOfJSONObjectsThatIncludeJSONNulls
}
Run Code Online (Sandbox Code Playgroud)
错误:
| Error 2013-09-17 11:33:56,965 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - JSONException occurred when processing request: [GET] /my/action
Object is null. Stacktrace follows:
Message: Object is null
Line | Method
->> 69 | isEmpty in net.sf.json.JSONNull
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 199 | …Run Code Online (Sandbox Code Playgroud) 根据gradle文档/第13.5.2节,我们可以省略方法调用中的括号:
括号是方法调用的可选项.
但是当我们尝试应用java插件时它似乎不起作用.如果脚本包含以下行:
apply [plugin: 'java']
Run Code Online (Sandbox Code Playgroud)
我们会收到错误:
Maybe something should be set in parentheses or a comma is missing?
@ line 1, column 8.
apply [plugin: 'java']
^
Run Code Online (Sandbox Code Playgroud)
但是如果我们把这个Map-literal放到括号中它就可以了.
apply([plugin: 'java'])
Run Code Online (Sandbox Code Playgroud)
因此,当参数为a时Map,我们不能省略括号,是吗?
我们习惯于重写toString方法,这样我们就可以通过调用来获取字段值
println"对象详细信息 - > $ object"
我们考虑为我们的构建编写测试用例作为一种良好实践并遵循TDD.
我的测试用例因一些数据缺失而失败.测试用例如下所示:
void "test method:toString"() {
given:
CSV csv = new CSV(accountId: '1', accountName: 'testName')
when:
String exp = "[accountId=" + csv.accountId + ", groupId)" + csv.groupId + ", accountName=" + csv.accountName +"]"
String string = csv.toString()
then:
string == exp
}
Run Code Online (Sandbox Code Playgroud)
以下是我的课程:
public class CSV {
String accountId
String groupId
String accountName
String chargeMode
String invoiceId
String date
@Override
public String toString() {
return "ChargingCsvLine [accountId="
+ accountId + ", groupId)" + groupId …Run Code Online (Sandbox Code Playgroud) grails ×5
groovy ×5
json ×2
binding ×1
embedding ×1
gradle ×1
grails-2.0.4 ×1
grails-orm ×1
httpbuilder ×1
java ×1
null ×1