【问题标题】:Grails:display results in gspGrails:以 gsp 显示结果
【发布时间】:2013-05-27 18:16:11
【问题描述】:

我有 3 个域类

Class Application { 
    Candidate candidate;
    Vote vote;
    static belongsTo = [candidate:Candidate,vote:Vote]

}

Class Candidate {
    String username;
    static hasMany =[capplications:Application]
}

Class Vote {
    String name;
    Date startdate;
    Date enddate;
    static hasMany =[capplications:Application]

} 

我有一个服务类,其中包含一个显示投票名称的方法

class  ApplicationService {

    def serviceMethod() {
    }


    def candidatesByVName(def vname){
        def results = Candidature.createCriteria().list() {
            createAlias("candidat","candidatAlias")
            createAlias("vote","voteAlias")
            eq('voteAlias.name',vname)
            projections {
                // property('votes.name')
                property('candidatAlias.username')
            }
        }
        return results
    }
    def candidatsGrpByVte(){

        def results = Application.withCriteria {
            createAlias("vote", "vote")
            projections {
                groupProperty("vote.name")
            }
        }
        return results;
    }
}

我从控制器开始

class ApplicationController{
    def applicationService;
    def displayVote={
        def votes=applicationService.candidatsGrpByVte()
        return [VoteList:votes]
    }
    def displayCandByVote={
        def candidate=applicationService.candidatesByVName(params.vote)
    }
}

我想在视图中显示每个投票的候选人列表

【问题讨论】:

    标签: jquery grails groovy gsp


    【解决方案1】:

    首先,您不应该在 Application 类中对相同的关系(候选人和投票)同时使用字段引用和 belongsTo - 这是多余的

    其次,您应该尽可能避免使用 def,因此如果您知道您的服务方法返回一个列表,请将其声明为返回 List,而不是 def(它有助于您的 IDE)

    第三,您的 displayCandByVote 操作不会返回地图,但如果您想显示它应该返回 - 否则您将无法在 gsp 中引用,例如

    ApplicationController.groovy:

    class ApplicationController{
        ...
        def displayCandByVote={
            def candidate=applicationService.candidatesByVName(params.vote)
            return [candidates: candidate]
        }
    }
    

    /views/application/displayCandByVote.gsp:

    <ul>
    <g:each in="${ candidates }">
        <li>${ it }</li>
    </g:each>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 2011-05-28
      • 2017-11-02
      • 1970-01-01
      • 2014-07-29
      相关资源
      最近更新 更多