Announcement Announcement Module
Collapse
No announcement yet.
How to create a file upload browser in spring roo? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create a file upload browser in spring roo?

    I'm new in spring roo. I want to create a page with file upload. I used spring roo to create all pages and i try to use it to create a file browser button in file upload page. The problem is spring roo using spring form tag which doesn't have file browser. I solve this problem by using html input type="file" tag instead, but the spring roo showed the error like this "Failed to invoke handler method [public void egat.spring.roo.ptu.io.web.ImportExcelController.p ost(java.lang.Long,org.springframework.ui.ModelMap ,javax.servlet.http.HttpServletRequest,javax.servl et.http.HttpServletResponse)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping "

    How can i solve this problem?

  • #2
    Hi,

    I also struggled with fileupload maybe my post os stackoverlfow can help you.

    HTH

    Comment


    • #3
      You may like to vote for or watch issue https://jira.springsource.org/browse/ROO-442.

      Comment


      • #4
        a way for upload file

        tested in Roo 1.0.2

        1: modify create.jsp: add enctype="multipart/form-data" and <input type="file" name="file"/>

        PHP Code:
        <form:form action="${form_url}method="POST" modelAttribute="big" enctype="multipart/form-data">
          
        file:  <input type="file" name="file" /> 

        2: modify entity:

        PHP Code:

        @Entity
        @RooJavaBean
        @RooToString
        @RooEntity
        public class Big {

            @
        Size(max 10240)
            private 
        String content;

            @
        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 {
                    
        InputStream in file.getInputStream();
                    
        FileOutputStream f = new FileOutputStream("f:/work/tempDir/"
                            
        + new Date().getTime());

                    
        int ch 0;
                    while ((
        ch in.read()) != -1) {
                        
        f.write(ch);
                    }
                    
        f.flush();
                    
        f.close();
                } catch (
        FileNotFoundException e) {
                    
        // TODO Auto-generated catch block
                    
        e.printStackTrace();
                } catch (
        IOException e) {
                    
        // TODO Auto-generated catch block
                    
        e.printStackTrace();
                }

            } 

        Comment


        • #5
          Alternatively you can also store the file content directly into the database by using a propertyeditor to bind the file-contents to a byte-array property of your entity.

          Depending on your usecase this may or may not be a good idea. It is a tradeoff between database size and speed of file retrieval (db-io is probably faster than filesystem-io)

          See this thread for a full discussion.

          Comment


          • #6
            thankz huang, your code working perfectly..

            Comment


            • #7
              trying with 1.1.0 M2

              I'm trying this solution with Roo 1.1.0 M2 and not having much luck. I've munged around with the generated jspx file and it appears to be generating the proper HTML (multipart encoded, <input type="file">), but my object never has setFileData called on it.

              Has anyone gotten file upload to work simply with Roo 1.1.0 M2?

              -pete

              Comment


              • #8
                thanks

                Nice, Huang! Thank you for helping out... I was also having same issue, your instructions worked out with mine too!

                Comment


                • #9
                  Originally posted by huang View Post
                  tested in Roo 1.0.2 (...)
                  Your approach combined with the modified tags as described in https://jira.springsource.org/browse/ROO-442 (I actually added a field:file tag) worked for me as well (Roo 1.1.0) including storage in the database. I had to add commons-io as dependency to get it to work.

                  Comment


                  • #10
                    Where are the Dojo CSS files?

                    I am trying to get the file upload styled nicely.

                    In the Dojo demo (http://demos.dojotoolkit.org/demos/u.../?forceNoFlash) they have styles like 'dijitFileInputReal' for the file upload but I don't see them in the included ROO Dojo CSS files, which I can't find anywhere.

                    If you leave the default ROO form styling with Spring.addDecoration the file upload field is not usable (at least on a Mac).

                    Comment


                    • #11
                      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

                      Comment


                      • #12
                        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.

                        Comment


                        • #13
                          Thanks Harald Walker,

                          how can the other field be populated?

                          LuKe

                          Comment


                          • #14
                            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

                            Comment


                            • #15
                              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

                              Comment

                              Working...
                              X