创建标准主题
Creating a standard topic
我在使用 grails 创建网络论坛时遇到了一些问题。在我的控制器中,我需要为网站工作创建一个标准主题,我正在使用教程代码。所以我的问题是:如何创建标准主题才能使此代码正常工作?
我需要创建的部分在第 11 行。
控制器:
class ForumController {
def springSecurityService
def home() {
[sections:Section.listOrderByTitle()]
}
def topic(long topicId) {
Topic topic = Topic.get(topicId)
if (topic == null){
}
params.max = 10
params.sort = 'createDate'
params.order = 'desc'
[threads:DiscussionThread.findAllByTopic(topic, params),
numberOfThreads:DiscussionThread.countByTopic(topic), topic:topic]
}
def thread(long threadId) {
DiscussionThread thread = DiscussionThread.get(threadId)
params.max = 10
params.sort = 'createDate'
params.order = 'asc'
[comments:Comment.findAllByThread(thread, params),
numberOfComments:Comment.countByThread(thread), thread:thread]
}
@Secured(['ROLE_USER'])
def postReply(long threadId, String body) {
def offset = params.offset
if (body != null && body.trim().length() > 0) {
DiscussionThread thread = DiscussionThread.get(threadId)
def commentBy = springSecurityService.currentUser
new Comment(thread:thread, commentBy:commentBy, body:body).save()
// go to last page so user can view his comment
def numberOfComments = Comment.countByThread(thread)
def lastPageCount = numberOfComments % 10 == 0 ? 10 : numberOfComments % 10
offset = numberOfComments - lastPageCount
}
redirect(action:'thread', params:[threadId:threadId, offset:offset])
}
}
您的问题很不清楚,但是如果您询问如何创建 Topic
域 class 的初始实例(以便您可以将其加载到 thread
行动),你可以在 Bootstrap.groovy
中这样做:
def init = { servletContext ->
if(!Topic.list()) { //if there are no Topics in the database...
new Topic(/*whatever properties you need to set*/).save(flush: true)
}
当前您正在尝试首先找到与提供的 topicId 对应的主题域 class 的实例,然后检查主题是否为空。
这是一个问题,如果topicId为空,查找将失败并抛出空指针异常。
要解决此问题,您只需将查找包装在 if-null 检查中,如下所示,以确保您确实拥有有效的 topicId。
你的另一个问题(如何实际设置默认值)更直观一些。如果没有找到主题,只需创建一个默认的默认构造函数或向构造函数提供 key:value 对。 [示例见下面的代码]。有关 Grails 对象关系映射系统的更多信息,您应该查看 their documentation。
def topic(long topicId) {
Topic topic
/* If you have a valid topicId perform a lookup. */
if (topicId != null){
topic = Topic.get(topicId)
}
/* If the topic hasn't been set correctly, create one with default values. */
if (topic == null) {
topic = new Topic()
/* You may want to have a look at the grails docs to see how this works. */
toipic = new Topic(name: "Default", priority: "Highest")
}
params.max = 10
params.sort = 'createDate'
params.order = 'desc'
[threads:DiscussionThread.findAllByTopic(topic, params),
numberOfThreads:DiscussionThread.countByTopic(topic), topic:topic]
}
我在使用 grails 创建网络论坛时遇到了一些问题。在我的控制器中,我需要为网站工作创建一个标准主题,我正在使用教程代码。所以我的问题是:如何创建标准主题才能使此代码正常工作?
我需要创建的部分在第 11 行。
控制器:
class ForumController {
def springSecurityService
def home() {
[sections:Section.listOrderByTitle()]
}
def topic(long topicId) {
Topic topic = Topic.get(topicId)
if (topic == null){
}
params.max = 10
params.sort = 'createDate'
params.order = 'desc'
[threads:DiscussionThread.findAllByTopic(topic, params),
numberOfThreads:DiscussionThread.countByTopic(topic), topic:topic]
}
def thread(long threadId) {
DiscussionThread thread = DiscussionThread.get(threadId)
params.max = 10
params.sort = 'createDate'
params.order = 'asc'
[comments:Comment.findAllByThread(thread, params),
numberOfComments:Comment.countByThread(thread), thread:thread]
}
@Secured(['ROLE_USER'])
def postReply(long threadId, String body) {
def offset = params.offset
if (body != null && body.trim().length() > 0) {
DiscussionThread thread = DiscussionThread.get(threadId)
def commentBy = springSecurityService.currentUser
new Comment(thread:thread, commentBy:commentBy, body:body).save()
// go to last page so user can view his comment
def numberOfComments = Comment.countByThread(thread)
def lastPageCount = numberOfComments % 10 == 0 ? 10 : numberOfComments % 10
offset = numberOfComments - lastPageCount
}
redirect(action:'thread', params:[threadId:threadId, offset:offset])
}
}
您的问题很不清楚,但是如果您询问如何创建 Topic
域 class 的初始实例(以便您可以将其加载到 thread
行动),你可以在 Bootstrap.groovy
中这样做:
def init = { servletContext ->
if(!Topic.list()) { //if there are no Topics in the database...
new Topic(/*whatever properties you need to set*/).save(flush: true)
}
当前您正在尝试首先找到与提供的 topicId 对应的主题域 class 的实例,然后检查主题是否为空。
这是一个问题,如果topicId为空,查找将失败并抛出空指针异常。
要解决此问题,您只需将查找包装在 if-null 检查中,如下所示,以确保您确实拥有有效的 topicId。
你的另一个问题(如何实际设置默认值)更直观一些。如果没有找到主题,只需创建一个默认的默认构造函数或向构造函数提供 key:value 对。 [示例见下面的代码]。有关 Grails 对象关系映射系统的更多信息,您应该查看 their documentation。
def topic(long topicId) {
Topic topic
/* If you have a valid topicId perform a lookup. */
if (topicId != null){
topic = Topic.get(topicId)
}
/* If the topic hasn't been set correctly, create one with default values. */
if (topic == null) {
topic = new Topic()
/* You may want to have a look at the grails docs to see how this works. */
toipic = new Topic(name: "Default", priority: "Highest")
}
params.max = 10
params.sort = 'createDate'
params.order = 'desc'
[threads:DiscussionThread.findAllByTopic(topic, params),
numberOfThreads:DiscussionThread.countByTopic(topic), topic:topic]
}