我正在尝试在我的域类中编写公式,这有助于我创建标准。
class MyClass {
//some fields
Date appointmentTime
String ddmmyy
int year
int month
int day
static transients = [
'ddmmyy',
'year',
'month',
'day'
]
static mapping= {
ddmmyy formula('DATE_FORMAT(appointmentTime)')
year formula('YEAR(appointmentTime)')
month formula('MONTH(appointmentTime)')
day formula('DAYOFMONTH(appointmentTime)')
}
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试在我的标准中使用此字段时,它都会抛出错误,即无法解析“myClass”的属性“ddmmyy”。
我的标准是:
Date myDate = Calender.instance.time
def results = MyClass.createcriteria().list{
lt('appointmentTime', date+1)
ge('appointmentTime', date)
projections {
groupProperty('ddmmyy')
count('id')
}
}
Run Code Online (Sandbox Code Playgroud)
知道为什么我会因此获得例外吗?
grails grails-orm grails-domain-class grails-2.0 grails-services
我正在使用Grails 2.0中的jQuery和Resources插件,我的布局如下:
<g:javascript library="jquery" />
<r:layoutResources/>
Run Code Online (Sandbox Code Playgroud)
使用该布局单击视图中的链接时,我在控制台中收到以下错误:
No module found with name [window]
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
谢谢
乔纳斯
我试图在这样的控制器中访问servletContext,但不断获取空指针异常:
def servletContext = getServletContext()
def serverPath = servletContext.getRealPath("/")
Run Code Online (Sandbox Code Playgroud)
...我刚刚在邮件列表上遇到过这个问题,但建议的唯一"正确"解决方法是在BootStrap.groovy的init闭包中设置它:
import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
class BootStrap {
def init = { servletContext ->
SCH.servletContext = servletContext
}
....
Run Code Online (Sandbox Code Playgroud)
......这仍然是这样吗?这个解决方案对我没有任何影响,仍然有NPE
提前致谢
从我的旧Macbook Pro迁移到新的Macbook Pro后,我发现我的项目将不再运行.Grails不断给我一个"Error/Users/Michael/{proj}/{proj}似乎不是Grails应用程序的一部分".刷新依赖项似乎确实成功运行.
我已经验证了以下内容:
可能的一件事是两台机器之间的用户目录名已经更改,但我没有在.project或application.properties或任何设置中看到旧的用户目录名.
我正在将grails插件从1.3.4升级到grails 2.1.1.升级后,我现在有一个失败的集成测试,之前没有失败.它无法使用"as JSON"(grails.converters.JSON).
@Test
public void testConvertCollectionOfEnvironmentSettingsToJSON() {
EnvironmentSetting setting = configurationService.getEnvironmentSetting('ENFORCE_SCHEMA_INSTANCE_RULE')
def jsonSetting = setting as JSON //exception thrown here
def s = jsonSetting as String
assertNotNull jsonSetting
}
Run Code Online (Sandbox Code Playgroud)
异常和堆栈跟踪:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.company.ipsvc.configuration.domain.EnvironmentSettingAllRevs@48c12420' with class 'com.company.ipsvc.configuration.domain.EnvironmentSettingAllRevs' to class 'grails.converters.JSON'
at com.company.ipsvc.configuration.converter.json.basic.BasicEnvironmentSettingJSONIntegrationTests.testConvertCollectionOfEnvironmentSettingsToJSON(BasicEnvironmentSettingJSONIntegrationTests.groovy:28)
Run Code Online (Sandbox Code Playgroud)
我能够成功使用encodeAsJSON().我也有与XML相同的问题.
尝试从Grails中的另一个视图渲染视图.
<g:render contextPath="??" template="first_view" />
Run Code Online (Sandbox Code Playgroud)
我在index.gsp并且愿意渲染在特定控制器下找到的视图(例如:user/first_view.gsp).我理解视图的正确路径应该在文档中所述的"contextPath"中设置:http://grails.org/doc/latest/ref/Tags/render.html
应用程序视图的contextPath是什么?
我有一些(非Grails-artifact)类通过传递grailsApplication对象来访问服务层bean .但是,我无法对以这种方式实现的类进行单元测试.为什么bean不在主上下文中注册?
@TestMixin(GrailsUnitTestMixin)
class ExampleTests {
void setUp() {}
void tearDown() {}
void testSomething() {
defineBeans {
myService(MyService)
}
assert grailsApplication.mainContext.getBean("myService") != null
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码失败了:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'myService'的bean
我想要做的是通过grailsApplication从普通的旧Java类访问服务.这有效,但不适用于单元测试环境.我应该采用不同的方式吗?
class POJO {
MyService myService;
public POJO(GrailsApplication grailsApplication) {
myService = (MyService) grailsApplication.getMainContext().getBean("myService");
}
}
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我区别:
<g:if test="${foo.bar}">
Run Code Online (Sandbox Code Playgroud)
和
<g:if test="\${foo.bar}">
Run Code Online (Sandbox Code Playgroud)
在"$"之前使用"\"的一个真实例子是:
<g:if test="\${flash.message}">
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有简单的控制器代码,如:
// UserController.groovy
class UserController {
static allowedMethods = [
signIn: 'GET',
authenticate: 'POST',
signOut: 'POST',
register: 'GET',
save: 'POST'
]
// ... code omitted
def register() { }
def save() {
render 'ok'
}
}
Run Code Online (Sandbox Code Playgroud)
报名表格:
<!-- register.gsp -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<title>Withme: Register</title>
</head>
<body>
<g:form mapping="register">
<!-- Code omitted -->
<g:actionSubmit value="Register" />
</g:form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和网址映射:
//UrlMappings.groovy
class UrlMappings {
static mappings = {
name register: '/register'(controller: 'user') …
Run Code Online (Sandbox Code Playgroud) 我正在使用grails spring security,并希望在注销后将用户重定向到某个URL.到目前为止我发现的是被调用的特殊属性'logout.afterLogoutUrl'
,grails.plugin.springsecurity.logout.postOnly
应该设置为false.所以在我的Config.groovy中我有:
grails.plugin.springsecurity.logout.postOnly = false
logout.afterLogoutUrl = "/"
Run Code Online (Sandbox Code Playgroud)
我的注销按钮看起来像:
<sec:ifLoggedIn>
<g:remoteLink class="logout buttons" controller="logout"><g:message code="btn.logout"
default="Loading…"/></g:remoteLink>
</sec:ifLoggedIn>
Run Code Online (Sandbox Code Playgroud)
当我点击退出按钮时,会出现以下请求序列:
从最后一个我得到'状态代码:401未授权'但用户仍然看到我点击退出按钮的页面.有谁知道如何处理这种情况?非常感谢你!