こんにちは、カスタム Grails アプリにドリルダウン タイプのパンくずナビゲーションを実装しようとしていますが、Crumb オブジェクトが Arraylist に保存されない理由がわかりません。
ブレッドクラムが作成され、リストに追加されますが、別のページに移動するたびにリストがリセットされます。
Grails ブレッドクラム プラグインがあることは知っていますが、この特定のアプリケーションには使用できません。
ここにコードのサンプルがあります
ドメイン クラス:
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
ArrayList <Crumb> BreadCrumbs = new ArrayList<Crumb>();
static auditable = true
static constraints = {
username blank: false, unique: true
password blank: false
enabled(default:true)
}
class Crumb {
String itemName
String url
static belongsTo = User
static constraints = {
itemName(nullable: false)
url(nullable: false)
}
def printData(){
println("Crumb Data PrintOut: \nName: " + itemName +"\nUrl: " + url)
}
}
class BreadCrumbService {
Crumb makeCrumb(String objectType, String name, long objectId){
def ext = "/its/dcmd/" + objectType + "/show?id=" + objectId;
def type = objectType;
Crumb c = new Crumb(itemName:name,url:ext)
return c
}
コントローラ:
class PersonController{
def setBreadCrumbForCurrentItem = {
println("Action was called to set breadcrumb for current page")
def user = User.load(SecurityContextHolder.context.authentication.principal.id)
def username = user.username
def userId = user.id
println("User Id: " + user.id)
println("User Version: " + user.version)
def objectType = params.pageType
println("objectType: " + objectType)
def itsName = ""
def objectId = null;
if (objectType.equals('asset')){
Asset assetInstance = params.instance
itsName = assetInstance.itsId
objectId = assetInstance.id
println("asset name: " + itsName)
println("asset id: " + objectId)
}
else if (objectType.equals('host')){
Host hostInstance = Host.get(params.id)
itsName = hostInstance.hostname
objectId = hostInstance.id
println("host name:" + itsName)
println("host id: " + objectId)
}
else if (objectType.equals('application')){
Application appInstance = new Application(params)
itsName = appInstance.applicationTitle
objectId = appInstance.id
println("application name:" + itsName)
println("application id: " + objectId)
}
else if (objectType.equals('service')) {
Service service = new Service(params)
itsName = service.serviceTitle
objectId = service.id
println("service name:" + itsName)
println("service id: " + objectId)
}
Crumb c = breadCrumbService.makeCrumb(objectType,itsName,objectId);
c.save(failOnError: true, flush: true)
if (!c.save()) {
user.errors.each {
println it
}
}
c.printData()
println("current BreadCrumbs saved...")
for (int j = 0; j < user.BreadCrumbs.size(); j++){
println(user.BreadCrumbs.get(j).toString())
}
user.save(failOnError: true, flush: true)
println("User Version: " + user.version)
if (!user.save()) {
user.errors.each {
println it
}
}
}
見る:
<html>
<head>
<g:set var="assetType" value="${fieldValue(bean:assetInstance, field: 'assetType')}" />
%{--<g:set var="objectId" value="{assetId}" />--}%
<meta content="main" name="layout" />
<title><g:message code="default.dcmd.label" /></title>
<jqDT:resources jqueryUi="true" type="js" />
<r:require modules="ui,menu"/>
<script language="javascript" type="text/javascript" src="../js/mustache.js"></script>
<script type="text/javascript">
<g:include controller="person" action="setBreadCrumbForCurrentItem" params="[pageType: 'asset', instance :assetInstance]"/>
</script>
</head>
<body>
<div id="container">
<s:info/>
<g:render template="../show_secondary" model="[pageType:'asset', objectId:assetId, action:'show', assetType: assetType, assetName: assetInstance]" />
<g:render template="../breadcrumbs" model="[pageType:'asset', action:'show']"/>
フィードバックは事前に大歓迎です! ありがとう