给定具有属性和构造函数的对象,我希望将构造函数参数复制到属性中,然后在构造函数中执行一些额外的工作.
import groovy.transform.TupleConstructor
@TupleConstructor
class Thing{
def one
def two
public Thing(one, two){
doSomething()
}
def doSomething(){
println "doing something with one : $one and two: $two"
}
}
println new Thing(1, 2).dump()
Run Code Online (Sandbox Code Playgroud)
如果我在构造函数中不执行任何其他操作,这将成功将args复制到属性,但如果我在构造函数中调用"doSomething()",则不会复制属性.
我正在寻找"Groovy"方法将args复制到属性.
在根据请求设置正文时,我可以使用Jackson而不是JSON-lib和Groovy的HTTPBuilder吗?
例:
client.request(method){
uri.path = path
requestContentType = JSON
body = customer
response.success = { HttpResponseDecorator resp, JSONObject returnedUser ->
customer = getMapper().readValue(returnedUser.content[0].toString(), Customer.class)
return customer
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我在处理响应时使用了Jackson,但我相信请求是使用JSON-lib.
我正在使用Groovy.我已经解析了一个文本文件,其行包含信息,包括日期.我现在只有日期,例如:
08:13:16,121
09:32:42,102
10:43:47,153
Run Code Online (Sandbox Code Playgroud)
我想比较这些值之间的增量; 我怎样才能做到这一点?即,我想从第二个中减去第一个,并将该值与第三个和第二个之间的差值进行比较.我会保存最大的值.
在我的设计中,我的所有daos都继承自父类,这个父类包含hibernateTemplate字段和一个setSessionFactory,它在使用spring设置会话时创建hibernateTemplate
这里的问题是,即使它似乎已经设置但是当我实际执行代码并且调用daos时,hibernateTemplate对象似乎为null.但是,当我使用会话工厂注入Dao对象而不是父通用类时,它就像一个魅力
AbstractDaoSupport类的一部分
/** The hibernate template. */
private HibernateTemplate hibernateTemplate;
/**
* Sets the session factory.
*
* @param sessionFactory the new session factory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}
/**
* Sets the hibernate template.
*
* @param hibernateTemplate the hibernateTemplate to set
*/
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
Run Code Online (Sandbox Code Playgroud)
这是当前有问题的代码,其中hibernateTemplate在运行时为null
<!-- the DataSource for application usage -->
<bean id="applicationDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/taxidb"/>
<property name="username" value="root"/>
<property …Run Code Online (Sandbox Code Playgroud) 我正在运行以下代码
/**
* @param args
*/
public static void main(String[] args) throws SQLException {
System.out.println("starting");
org.postgresql.Driver driver = new org.postgresql.Driver();
DriverManager.registerDriver(driver);
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/epcfe/", "postgres", "aap123!");
Statement st = con.createStatement();
st.executeQuery("select * from epcfeschema.PRODUCT");
System.out.println("done");
}
Run Code Online (Sandbox Code Playgroud)
我一直在 Exception in thread "main" org.postgresql.util.PSQLException: ERROR: relation "epcfeschema.product" does not exist
如果我创建一个包含小写名称产品的表,这可以正常工作,但我需要它来处理具有全部大写字母的表.如何防止JDBC小写我的表?
我想要做的是找到一个域,然后创建一个域或保存预先存在的域.这是我目前正在使用的代码(在此项目中,skeleton是包名称):
def save() {
Class lob = grailsApplication.getDomainClass('skeleton.'+params.lob.name)
def instance = lob.get(params.lob.id)
if (instance){
params.data.each { name, value ->
if (instance.metaClass.hasProperty(name)){
instance[name] = value
}
}
}else{
instance = new lob()
params.data.each { name, value ->
if (instance.metaClass.hasProperty(name)){
instance[name] = value
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.任何人都可以帮我解决这个问题吗?
情况是:我有一个名为 status 的 javascript bool 变量。如果是真的,我想渲染一个 Grails 模板。如果它是假的,我想呈现另一个 Grails 模板。我的代码:
HTML/JS
<div id="content">
<g:javascript>
$( function(){
if( status==true ) {
$.ajax({
url: '/tool/home/renderSplash',
type: 'POST',
failure: function(){alert("Failed loading content frame"})
}
else{
$.ajax({
url: '/tool/home/renderContent',
type: 'POST',
failure: function(){alert("Failed loading content frame")}
})
}
} )
</g:javascript>
</div>
Run Code Online (Sandbox Code Playgroud)
圣杯:
class HomeController {
def index() {}
def renderSplash() {
render( template: "splash" )
}
def renderContent() {
render( template: "content" )
}
}
Run Code Online (Sandbox Code Playgroud)
我可以看到,POST 包含正确的模板数据,但没有显示在屏幕上。
我这样做是否正确?
我正在开发樱桃,我从python脚本开始.
为了更好的开发,我想知道在主进程中阻止cherrypy的正确方法是什么(而不是使用ctrl-c或SIGTERM从外部阻止).
我假设我必须从主应用程序注册一个回调函数,以便能够从工作线程中停止樱桃主进程.
但是如何从内部停止主流程呢?
我有现有的Java Web应用程序.现在我写了一个groovy hello world class.我应该如何用现有的java类编译它?从http://groovy.codehaus.org/看起来我需要一个不同的编译器,因为命令是groovy -e"println'Hello'+ args [0]"世界改为使用javac.
我想从命令提示符和eclipse编译命令,以便使用现有的java类编译groovy类.