我想构建一个非常简单的帮助台应用程序,目的是学习grails。我希望用户能够登录,创建请求/事件。 用户选择产品线,该产品线的主题和该主题的子主题。每个子主题都有一个或多个支持用户负责。 另一方面,支持用户可以查看属于其“管辖区”的事件列表。我的域名classess看起来像这样:
class Request{
Date dateCreated
String subject
String startedBy
String description
String status
String priority
Productline productline
Topic topic
Subtopic subtopic
static constraints = {
//id unique: true
dateCreated(blank:true,nullable:true)
subject()
description(maxSize:5000)
status (blank:true,nullable:true)
priority(inList:["Normalan","Hitno","Nije hitno"])
productline(blank:true,nullable:true)
topic(blank:true,nullable:true)
subtopic(blank:true,nullable:true)
company(blank:true,nullable:true)
contact(blank:true,nullable:true)
startedBy(blank:true,nullable:true)
acceptedBy(blank:true,nullable:true)
}
}
class Subtopic {
String subtopic
Productline productline
Topic topic
static hasMany = [responsibilities: Responsibility]
String toString(){
"$subtopic"
}
static constraints = {
subtopic()
productline()
topic()
}
List contacts(){
return responsibilities.collect{it.contact}
}
List addToContacts(Contact contact){
Responsibility.link(contacts, this)
return contacts()
}
List removeFromContacts(Contact contact){
Responsibility.unlink(contact, this)
return contacts()
}
}
class Responsibility {
Contact contact
Subtopic subtopic
static Responsibility link(contact, subtopic){
def r = Responsibility.findByContactAndSubtopic(contact, subtopic)
if(!r){
r = new Responsibility()
contact?.addToResponsibilities(r)
subtopic?.addToResponsibilities(r)
}
return r
}
static void unlink(contact, subtopic){
def r = Responsibility.findByContactAndSubtopic(contact, subtopic)
if(r){
contact?.removeFromResponsibilities(r)
subtopic?.removeFromResponsibilities(r)
r.delete()
}
}
static constraints = {
}
}
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
String toString(){
"$username"
}
static constraints = {
username blank: false, unique: true
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
class Contact {
String realname
String company
String mobile
String fix
String email
User user
String toString(){
"$realname"
}
static hasMany = [responsibilities: Responsibility]
static constraints = {
}
List subtopics(){
return responsibilities.collect{it.subtopic}
}
List addToSubtopics(Subtopic subtopic){
Responsibility.link(this, subtopic)
return subtopics()
}
List removeFromSubtopics(Subtopic subtopic){
Responsibility.unlink(this, subtopic)
return subtopics()
}
}
在我的RequestController中,我有supportList()动作。成功登录后,具有角色ROLE_SUPPORT的用户将被发送到此操作,该操作应过滤数据,以便发送到视图的唯一请求实例是当前登录用户负责的那些。这是行动:
def supportList = {
def contact = Contact.findByUser(springSecurityService.currentUser)
def requestList = contact.subtopics().collect {Request.findAllBySubtopic(it)}
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[requestInstanceList: requestList, requestInstanceTotal: requestList.count()]
}
我要空桌了。实际上,我得到了预期的行数,但所有字段都是空的。我究竟做错了什么?
这是我的GSP:
<%@ page import="com.testapp.Request" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<g:set var="entityName" value="${message(code: 'request.label', default: 'Request')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
<div class="nav">
<span class="menuButton"><g:link class="create" action="create">
<g:message code="default.new.label" args="[entityName]" /></g:link></span>
</div>
<div class="body">
<h1>Lista Zahteva</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div class="list">
<table>
<thead>
<tr>
<g:sortableColumn property="id"
title="${message(code: 'request.id.label', default: 'Id')}" />
<g:sortableColumn property="subject" title="Datum kreiranja" />
<g:sortableColumn property="subject" title="Tema" />
<g:sortableColumn property="priority" title="Prioritet" />
<th><g:message code="incident.startedBy" default="Započeo" /></th>
<g:sortableColumn property="status" title="Status" />
</tr>
</thead>
<tbody>
<g:each in="${requestInstanceList}" status="i" var="requestInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td><g:link action="show" id="${requestInstance.id}">
${fieldValue(bean: requestInstance, field: "id")}</g:link></td>
<td width="200">${fieldValue(bean:requestInstance,field:"dateCreated")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "subject")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "priority")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "startedBy")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "status")}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
<div class="paginateButtons">
<g:paginate total="${requestInstanceTotal}" />
</div>
</div>
</body>
</html>
答案 0 :(得分:2)
使用您当前的模型,您可以查询当前的用户职责:
Responsability.findAllBySupport(springSecurityService.currentUser).
collect {Incident.findAllBySubtopic(it.subtopic)}.flatten()
实际上,责任只是用户和子主题之间的连接表。如果您将其删除,并定义了用户和子主题之间的多对多关系:
class User {
static hasMany = [supportedSubtopics: Subtopic]
...
}
class SubTopic {
static hasMany = [supportingUsers: User]
....
}
你可以这样查询:
springSecurityService.currentUser.supportedSubtopics.collect {Incidents.findBySubtopic(it)}.flatten()
通过将hasMany = [incidents: Incident]
添加到Subtopic,您可以进一步将其简化为
springSecurityService.currentUser.supportedSubtopics*.incidents.flatten()