Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 35

Thread: How to create a file upload browser in spring roo?

  1. #11

    Default

    I'm trying the different solutions posted in this thread but I'm not able to setup a form to upload a file.

    Could somebody post a complete example of a form with file upload?

    It would be very precious.

    Regards

    LuKe

  2. #12
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    This is my upload form:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
        <jsp:output omit-xml-declaration="yes"/>
        <form:create enctype="multipart/form-data" id="fc_my_domain_image" modelAttribute="image" path="/images">
         	<field:file field="file" id="c_my_domain_image_file" z="user-managed"/>
            <field:input disableFormBinding="true" field="image" id="c_my_domain_image_image" render="false" z="user-managed"/>
            <field:input disableFormBinding="true" field="fileName" id="c_my_domain_image_fileName" render="false" required="true" z="user-managed"/>
            <field:select disableFormBinding="true" field="format" id="c_my_domain_image_format" items="${imageformats}" path="imageformats" render="false" required="true" z="user-managed"/>
            <field:input disableFormBinding="true" field="width" id="c_my_domain_image_width" render="false" required="true" validationMessageCode="field_invalid_integer" z="user-managed"/>
            <field:input disableFormBinding="true" field="height" id="c_my_domain_image_height" render="false" required="true" validationMessageCode="field_invalid_integer" z="user-managed"/>
         </form:create>
    </div>
    Two changes are important here:

    First the form:create has been extended to accept the optional enctype parameter (the previously linked Jira issue shows you how to do that).

    Next I created the field.file tag, which is basically a modified version of the file.input tag with input type="file" as form element.

    Except for the file upload in the create form all other fields are disabled and will be populated automatically.

  3. #13

    Default

    Thanks Harald Walker,

    how can the other field be populated?

    LuKe

  4. #14
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    Quote Originally Posted by lukeb View Post
    Thanks Harald Walker,

    how can the other field be populated?

    LuKe
    That is being shown in
    http://forum.springsource.org/showpo...44&postcount=4

  5. #15

    Default

    Hi Harald,

    I followed your instructions but I got this error:

    2010-11-15 09:26:10,973 [http-8080-5] ERROR org.springframework.web.servlet.tags.MessageTag - No message found under code 'label_my_domain_image' for locale 'en_US'.
    javax.servlet.jsp.JspTagException: No message found under code 'label_my_domain_image' for locale 'en_US'.

    Where is the error?

    LuKe

  6. #16

    Default

    Ok, I found where is the error. I'll try to solve it.

    LuKe

  7. #17
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    Quote Originally Posted by lukeb View Post
    Hi Harald,

    I followed your instructions but I got this error:

    2010-11-15 09:26:10,973 [http-8080-5] ERROR org.springframework.web.servlet.tags.MessageTag - No message found under code 'label_my_domain_image' for locale 'en_US'.
    javax.servlet.jsp.JspTagException: No message found under code 'label_my_domain_image' for locale 'en_US'.

    Where is the error?

    LuKe
    The error message is pretty clear. So either add the message or change id="c_my_domain_image_image" to one of your domain objects which already has an entry in application.properties.

    Have a look at the tag libraries to see why this is happening and how ROO is working. Based on the id attribute the label message key is being determined.

  8. #18

    Default

    Finally I was able to upload my file.
    The problem is that the other fields are not stored on the database table.

    Any idea why?

    Furthermore, normally after saving a record roo redirects to a page that show the record just stored.
    In case of the upload form roo return to the form (but the file has been uploaded).

    LuKe

  9. #19
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    Quote Originally Posted by lukeb View Post
    Finally I was able to upload my file.
    The problem is that the other fields are not stored on the database table.

    Any idea why?
    LuKe
    Could you show us your entity class and controller?

  10. #20

    Default

    Big:

    Code:
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Big {
    @NotNull @Size(max = 10240) private String content;
    @NotNull @Size(max = 20) private String name;
    @Transient private CommonsMultipartFile file; // added private String fileName; // added private long size; //added public CommonsMultipartFile getFile() {
    return file;
    } // save file to disk ,save filename , file size to database public void setFile(CommonsMultipartFile file) {
    this.file = file; this.fileName = file.getOriginalFilename(); this.size = file.getSize(); System.out.println("hehe this.fileName: " + this.fileName + " , " + file.getClass().getName()); try {
    System.out.println("try 01"); InputStream in = file.getInputStream(); FileOutputStream f = new FileOutputStream("/opt/" + file.getOriginalFilename()); int ch = 0; while ((ch = in.read()) != -1) {
    f.write(ch);
    } f.flush(); f.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    BigController:

    Code:
    @RooWebScaffold(path = "bigs", formBackingObject = Big.class)
    @RequestMapping("/bigs")
    @Controller
    public class BigController {
    }
    Last edited by Andrew Swan; Oct 11th, 2011 at 06:25 PM. Reason: Added code tags around code snippets

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •