0

I am working on a project only needs one file upload at a time. And I am using struts2 and jquery file upload plugin. I see many examples but I don't know how to activate struts2 action in jquery file upload function.

This is my jsp

<form id="uploadForm" action="/myapp/trading_upload.action" method="POST" enctype="multipart/form-data">
    <div class="row">
        <div class="col-sm-9">
            <div class="panel">
                <div class="panel-heading">
                    <span class="panel-title">Upload</span>
                </div>
                <div class="panel-body">
                    <div class="row">

                        <div class="col-sm-6">
                                <span class="btn btn-success fileinput-button">
                                    <i class="glyphicon glyphicon-plus"></i> 
                                    <span>Select file</span>
                                    <input id="fileupload" type="file" name="files">
                                </span>
                        </div>          
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

This is my js, basically it's just the jQuery File Upload example code

$(function() {
    $('#fileupload').fileupload({
        dataType: 'json',
        url: '/myapp/trading_upload.action',
        add: function (e, data) {
            data.context = $('<button/>').text('Upload')
                .appendTo(document.body)
                .click(function () {
                    data.context = $('<p/>').text('Uploading...').replaceAll($(this));
                    data.submit();
                });
        },
        done: function (e, data) {
            consolo.log('complete');
            alert("complete");
            data.context.text('Upload finished.');
        }
    });
});

And this is my struts.xml

<action name="trading_upload" class="tradingAction" method="upload">
    <interceptor-ref name="fileUpload">
        <param name="maximumSize">2097152</param>
    </interceptor-ref>
</action>

This is my tradingAction, it's just a print since I want to see it's activated

public String upload() throws Exception {
        System.out.println("upload");
        return SUCCESS;
}

Should I use url: '/myapp/trading_upload.action' to activate action? But after I select file nothing happen. No action is activated. Thanks in advance!

1 Answer 1

1

Finally I realized I need to implement struts2 upload first, then I can combine it with file upload plugin to activate the action. And it works

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.