在BlogPost coldfusion

时间:2015-12-02 21:51:38

标签: coldfusion

我正在通过learncfinaweek.com学习Coldfusion,并希望帮助克服这个困难:

BLOTPOST中未定义元素标题 错误发生在C:/ColdFusion11/cfusion/wwwroot/learncfinaweek/chapter1solution/admin/content/blog/listblogpost.cfm:line 34

我认为"未定义在BLOGPOST"是指BlogPost.cfc,下面的底部页面。这是对的吗?

第一个是listblogpost.cfm

<cfimport taglib="../../customTags" prefix="ct" />
<ct:securityCheck redirectPage="#cgi.script_name#"/>

    <cfset adminPath =    createObject('learncfinaweek.chapter1solution.admin.cfc.system').getBasePath(    cgi.script_name) />

<!--- Pull Blog Posts --->
<cfset blogPosts=EntityLoad('BlogPost') />

<cfoutput>
    <ct:layout section="blog">
        <ct:navigation section="blog" active="post"/>

        <div class="span10">
            <h2>Blog</h2>
                <form class="navbar-form pull-right">
                    <a class="btn btn-primary" href="<cfoutput>#adminPath#</cfoutput>/content/blog/editblogpost.cfm">
                        <i class="icon-plus icon-white"></i>
                        New Blog Post
                    </a>
                </form>
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Publish Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <cfloop array="#blogPosts#" index="blogPost">
                        <tr>
                            <td>
                                <!--- Title --->
                                #blogPost.title#
                            </td>
                            <td>
                                <!--- Date Posted --->
                                #dateFormat(blogPost.datePosted,"mm/dd/yyyy")#
                            </td>
                            <td>
                            <!--- Edit Post --->
                                <a href="#adminPath#/content/blog/editblogpost.cfm?id=#blogPost.id#"><i class="icon-edit"></i></a>
                            </td>
                        </tr>
                    </cfloop>
                </tbody>
            </table>
        </div>
        </ct:layout>
</cfoutput>

这是editblogpost.cfm页面:

<cfimport taglib="../../customTags" prefix="ct" />
<ct:securityCheck redirectPage="#cgi.script_name#"/>

<cfparam name="url.id" default="0" />
<cfparam name="form.submitted" default="0" />
<cfparam name="form.id" default="0" />
<cfparam name="form.title" default="" />
<cfparam name="form.summary" default="" />
<cfparam name="form.body" default="" />
<cfparam name="form.datePosted" default="" />
<cfparam name="form.categories" default="" />

<cfset errorBean =   createObject('learncfinaweek.chapter1solution.admin.cfc.errorBean').init() />

<cfif form.submitted>

    <!--- check if data is valid--->
    <cfif !len(trim(form.title))>
        <cfset errorBean.addError('Title is required','title') />
    </cfif>

    <cfif !len(trim(form.summary))>
        <cfset errorBean.addError('A summary is required','startDate') />
    </cfif>

    <cfif !len(trim(form.body))>
        <cfset errorBean.addError('You need to provide a body','details') />
    </cfif>

    <cfif !len(trim(form.datePosted))>
        <cfset errorBean.addError('You need to provide a release date','details') />
    </cfif>

    <!--- Only process if there are no errors --->
    <cfif !errorBean.hasErrors()>
        <cfif val(form.id)>
            <!--- Edit Entity  --->
            <cfset blogpost=EntityLoad('BlogPost',form.id,true) />
            <cfset blogPost.title = form.title />
            <cfset blogPost.summary = form.summary />
            <cfset blogPost.body = form.body />
            <cfset blogPost.datePosted = form.datePosted />
            <cfset blogPost.modifiedDateTime = now() />
            <cfloop array="#blogPost.getCategories()#" index="category">
                    <cfset entityDelete(category) />
                </cfloop>
        <cfelse>
            <!--- Create Entity --->
            <cfset blogPost=EntityNew('BlogPost') />
            <cfset blogPost.title = form.title />
            <cfset blogPost.summary = form.summary />
            <cfset blogPost.body = form.body />
            <cfset blogPost.datePosted = form.datePosted />
            <cfset blogPost.createdDateTime = now() />
        </cfif>
        <cfset entitySave(blogPost) />

    <!--- Add Category to Entity --->
        <cfloop list="#form.categories#" index="categoryID">
            <cfset blogPostCategory = EntityNew('blogPostCategory') />
            <cfset blogCategory = EntityLoad('blogCategory',categoryID,true) />
            <cfset blogPostCategory.blogCategory = blogCategory />
            <cfset blogPostCategory.blogPost = blogPost />
            <cfset entitySave(blogPostCategory) />
        </cfloop>

        <cfset ormFlush() />

        <cflocation url="listblogpost.cfm?message=#urlencodedformat('Blog Post Saved')#" addtoken="false" />
    </cfif>
</cfif>

<cfif val(url.id)>
    <!--- Get Entity Data --->
    <cfset blogPost = EntityLoad('BlogPost',url.id,true) />
    <cfset form.id = blogPost.id />
    <cfset form.title = blogPost.title />
    <cfset form.summary = blogPost.summary />
    <cfset form.body = blogPost.body />
    <cfset form.datePosted = blogPost.datePosted />
    <cfset form.categories = blogPost.categoryids />
</cfif>

<cfset categories = entityLoad('BlogCategory' ) />

<cfoutput>
    <ct:layout section="blog">
        <ct:navigation section="blog" active="post"/>

        <div class="span10">
            <cfif val(url.id)>
                <h2>Edit Blog Post</h2>
            <cfelse>
                <h2>Add Blog Post</h2>
            </cfif>

            <cfif errorBean.hasErrors()>
                <div class="alert alert-error">
                    <strong>Error</strong><br />
                    The following errors occured:
                    <ul>
                        <cfloop array="#errorBean.getErrors()#" index="error">
                            <li>#error.message#</li>
                        </cfloop>
                    </ul>
                </div>
            </cfif>

            <form class="form-horizontal" action="#cgi.script_name#" method="post">
                <div class="control-group">
                    <label class="control-label" for="title">Title</label>
                    <div class="controls">
                        <input type="text" id="title" name="title" va    lue="#form.title#">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="title">Summary</label>
                    <div class="controls">
                        <textarea rows="6" id="summary" name="summary"     class="input-xlarge">#form.summary#</textarea>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="title">Body</label>
                    <div class="controls">
                        <textarea rows="3" id="body" name="body" class="wysiwyg">#form.body#</textarea>
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="details">Publish Date</label>
                    <div class="controls">
                        <input type="text" id="datePosted"  name="datePosted" class="datepicker"  value="#dateFormat(form.datePosted,'mm/dd/yyyy')#">
                    </div>
                </div>

                <div class="control-group">
                <label class="control-label"  for="details">Categories</label>
                    <div class="controls">
                            <label class="checkbox">
                            <cfloop array="#categories#" index="category">
                                <input type="checkbox" name="Categories" value="#category.id#"
                                    <cfif listfind(form.categories,category.id)>checked</cfif>>#category.name#<br />
                            </cfloop>
                        </label>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </div>
                <input type="hidden" name="submitted" value="1" />
                <input type="hidden" name="id" value="#url.id#" />
            </form>
        </div>
    </ct:layout>
</cfoutput>

blogPost.cfc页面:

component persistent="true" {
    Property name="id" column="blogpostid" fieldtype="id" generator="increment";
    Property name="title" ormtype="text";
    Property name="summary" ormtype="text";
    Property name="body" ormtype="text";
    Property name="datePosted" ormtype="timestamp";
    Property name="createdDateTime" ormtype="timestamp";
    Property name="modifiedDateTime" ormtype="timestamp";
    Property name="deleted" ormtype="boolean";

    Property name="comments" singularname="comment" fieldtype="one-to-many" cfc="blogComment" fkcolumn="blogpostid" cascade="all";
    Property name="categories" fieldtype="one-to-many" cfc="blogPostCategory" fkcolumn="blogpostid";

    public string function getCategoryIDs(){
        var categoryList = '';
        if(hasCategories()){
            for(var categoryPost in getCategories()){
                categoryList = listAppend(categoryList, categoryPost.blogCategory.id);
            }
        }

        return categoryList;
    }
        public string function getCategoryNames(){
        var categoryList = '';
        if(hasCategories()){
            for(var categoryPost in getCategories()){
                categoryList = listAppend(categoryList, categoryPost.blogCategory.name,', ');
            }
        }
        return categoryList;
    }
}

1 个答案:

答案 0 :(得分:4)

如果出现错误X is undefined in Y并且他们为您提供了特定的行号,请跳转到该文件中的该行号并查看其中的内容。

  1. 错误为Element TITLE is undefined in BLOGPOST
  2. 该文件的第34行包含:#blogPost.title#
  3. 因此,它无法在title中找到元素blogpost。您定义了blogpost吗?
  4. 您正在循环使用数组<cfloop array="#blogPosts#" index="blogPost">,因此您已定义blogPost。接下来的问题是,blogPost是一个结构还是一个对象?
  5. 您可以在该循环中轻松添加<cfdump var="#blogPost#">,以查看该数组的任何元素是否缺少title键。