一到多个图像grails

时间:2012-07-11 02:15:46

标签: jquery image grails one-to-many

我尝试使用grails和jquery上传几个(一对多)图像。我一直关注这个post,但到目前为止我无法做到。问题是看起来一切正常,但是当你试图保存图像(例如创建一个建筑物)时,它不会保存任何图像。

我认为,问题的关键在于文件类型(图像)的gsp参数。这些是到达我的控制器的参数: [名称:1,类型:1,创建:Crear, imagesList [0]。数据:org.springframework.web.multipart.commons.CommonsMultipartFile@d9278f, imagesList [0]:data:org.springframework.web.multipart.commons.CommonsMultipartFile@d9278f],action:save,controller:building]

所以,我认为这些参数对我想做的事情是错误的。任何想法,或示例代码来解决这个问题?谢谢!

楼宇控制器中的我的save()方法

def save() {
    def buildingInstance = new Building(params)
    if (!buildingInstance.save(flush: true)) {
        render(view: "create", model: [buildingInstance: buildingInstance])
        return
    }                    
        flash.message = message(code: 'default.created.message', args: [message(code: 'building.label', default: 'Building'), buildingInstance.id])
    redirect(action: "show", id: buildingInstance.id)
}

我的域名:

 class Building {

    def Integer type
    def String name

    List images = new ArrayList()    
    static hasMany = [images: Image]

    static constraints = {
        name()
        type() 
    }   

    static mapping = {
        images cascade:"all-delete-orphan"
    }

def getImagesList() {
    return LazyList.decorate(images,FactoryUtils.instantiateFactory(Image.class))
}

}

 class Image {
    def byte[] data 
    static belongsTo = [building: Building]

    boolean deleted 
    static transients = [ 'deleted' ]

    static constraints = { data(maxSize:10000000,required:true,blank:false)  }
 }

我的JSP:

_Images.jsp

<script type="text/javascript">
function addChild() {
var childCount = ${buildingInstance?.images.size()} + 0;
var htmlId = "image" + childCount;
var deleteIcon = "${resource(dir:'images/skin', file:'icon_delete.png')}";
var templateHtml = "<div id='" + htmlId + "' name='" + htmlId + "'>\n";
templateHtml += "<input type='file' id='imagesList[" + childCount + "].data' name='imagesList[" + childCount + "].data' />\n";
templateHtml += "<span onClick='$(\"#" + htmlId + "\").remove();'><img src='" + deleteIcon + "' /></span>\n";
templateHtml += "</div>\n"; 
$("#childList").append(templateHtml);
 childCount++; 
}
</script>

<div id="childList">
    <g:each var="image" in="${buildingInstance.images}" status="i">
        <!-- Render the image template (_image.gsp) here -->
        <g:render template='image'
            model="['image':image,'i':i,'hidden':false]" />
    </g:each>
</div>
<input type="button" value="Add image" onclick="addChild();" />

_image.gsp

<div id="image${i}" class="image-div" <g:if test="${hidden}">style="display:none;"</g:if>>
    <g:hiddenField name='imagesList[${i}].id' value='${image?.id}'/>
    <g:hiddenField name='imagesList[${i}].deleted' value='false'/>

    <input type="file" name="imagesList[${i}].data"  value="${image?.data}" />

    <span class="del-image">
        <img src="${resource(dir:'images/skin', file:'icon_delete.png')}" 
            style="vertical-align:middle;"/>
    </span>
</div>

1 个答案:

答案 0 :(得分:1)

我已经找到了错误的地方。刚刚将gsp中的“imagesList”值更改为“images”作为域中的名称并且它可以正常工作。我关注的帖子似乎错了。