【问题标题】:ajaxform not submit the value if attachment is not available如果附件不可用,ajaxform 不提交值
【发布时间】:2014-11-17 14:27:25
【问题描述】:

我是一名 Java 初学者,打算创建一个 Web 应用程序。我正在使用ajaxform 向控制器提交一份表格。元素很少,其中之一是type=file

我的控制器方法如下

    @RequestMapping(value = { "/addReplies" }, method = {
        org.springframework.web.bind.annotation.RequestMethod.POST,
        org.springframework.web.bind.annotation.RequestMethod.GET } )
@ResponseBody
public Map<String,Object> addReplies(
        @Valid @ModelAttribute("Replies") Replies replies,
        BindingResult result, HttpServletRequest request,
        HttpServletResponse response,Locale locale,Principal principal,
        @RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[]) throws ServletException,
        IOException {
             //perform some opraton
        }

如果数据中有可用的附件,则一切正常,否则不会进入此方法。

如果我从方法中删除 @RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[] 这个参数,那么效果会很好,但通过这种方式我无法获取附件。

请尝试理解我的问题,并尽快给我所有可能的解决方案。

如果我不使用 ajax 并使用 regul 提交表单,此方法效果很好

【问题讨论】:

    标签: java jquery ajax spring-mvc ajaxform


    【解决方案1】:

    我也这样做 但我的编码也可以工作

    做这个解决方案

    @RequestMapping(value="/saveUserTask", method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> saveUserTask(
            @RequestParam( value = "title" , required = false) String title,
            @RequestParam( value = "projectid" , required = false) int projectid,
            @RequestParam( value = "mileid" , required = false,defaultValue ="-1") int mileid,
            @RequestParam( value = "responsible" , required = false) int responsible[],
            @RequestParam( value = "startDate" , required = false) Date startDate,
            @RequestParam( value = "endDate" , required = false) Date endDate,
            @RequestParam( value = "description" , required = false) String description,
            @RequestParam( value = "priority" , required = false, defaultValue="1") int priority,
            @RequestParam( value = "workHours" , required = false) int workHours,
            @RequestParam(value = "attachment" , required = false) MultipartFile myFile,
            HttpServletRequest request,Principal principal,RedirectAttributes redirectAttributes,Locale locale) throws ParseException, IOException{
        String fileAttachPath;
        if(myFile!=null)
            fileAttachPath=request.getSession().getServletContext().getRealPath("/WEB-INF/attechments/"+ new Date().getTime() + "_" + myFile.getOriginalFilename());
        else
            fileAttachPath="";
    }
    

    【讨论】:

      【解决方案2】:

      要正常上传文件,您需要在表单中使用 encType="multipart/form-data"。如果你想使用 Ajax 上传文件,除了简单的 ajax 调用,你还需要使用它的方法

      你正在上传文件,在序列化 ajaxForm 之前首先检查文件部分是否为空,如果它为空,则禁用该元素以获得更长的可用性并提交

      beforeSerialize: function($form, options){
      //before serializing the data have to disable element if the data is not avaliable
               $(".attachfile").filter(function(){ 
                    return !this.value; }).attr("disabled", "disabled");
               },
      

      【讨论】:

        【解决方案3】:

        您之前是否尝试过提交 ajax 表单数据对象?

        @RequestMapping(value ="/settingsSim",method=RequestMethod.POST)
           @ResponseBody
           public Map uploadSimSettings(@RequestBody String body) {
            /*
            some controller logic 
            */
           }
        

        【讨论】:

          【解决方案4】:

          我刚刚这样做了:

          $("#myform").bind('ajax:complete', function() {
              // tasks to do  with ajax submit
          });
          

          一切都很顺利。

          请参阅此 api documentation 了解更多详细信息。

          【讨论】:

            猜你喜欢
            • 2012-08-24
            • 1970-01-01
            • 2020-11-24
            • 1970-01-01
            • 2012-05-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-23
            相关资源
            最近更新 更多