Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 隐藏@modeldattribute变量,使其不显示在URL中?春季MVC_Java_Spring_Spring Mvc - Fatal编程技术网

Java 隐藏@modeldattribute变量,使其不显示在URL中?春季MVC

Java 隐藏@modeldattribute变量,使其不显示在URL中?春季MVC,java,spring,spring-mvc,Java,Spring,Spring Mvc,我使用的是SpringMVC3,我有以下控制器 @RequestMapping(value="FileUploadForm",method=RequestMethod.GET) public String showForm(ModelMap model){ UploadForm form = new UploadForm(); model.addAttribute("FORM", form); return "FileUploadForm"; } @RequestMap

我使用的是SpringMVC3,我有以下控制器

@RequestMapping(value="FileUploadForm",method=RequestMethod.GET)
public String showForm(ModelMap model){
    UploadForm form = new UploadForm();
    model.addAttribute("FORM", form);
    return "FileUploadForm";
}

@RequestMapping(value="FileUploadForm",method=RequestMethod.POST)
public ModelAndView processForm(@ModelAttribute(value="FORM") UploadForm form,BindingResult result){
    if(!result.hasErrors()){
        FileOutputStream outputStream = null;
        String filePath = System.getProperty("java.io.tmpdir") + "/" + form.getFile().getOriginalFilename();
        try {
            outputStream = new FileOutputStream(new File(filePath));
            outputStream.write(form.getFile().getFileItem().get()); 
            outputStream.close();
            System.out.println(form.getName());


             return new ModelAndView(new RedirectView("success?Filepath="+filePath, true, true, false));
        } catch (Exception e) {
            System.out.println("Error while saving file");
            return new ModelAndView("FileUploadForm");
        }

    }else{
        return new ModelAndView("FileUploadForm");
    }

}
此控制器获取文件路径并用于执行爆炸

@RequestMapping(value="success")
public String blasta(@ModelAttribute("Filepath") String filepath, Model model){
    Blast sb = new Blast("somepath");
    String[] blastIt = sb.blast("somepath", filepath);
    String newLine = System.getProperty("line.separator");
    ArrayList<Object> result = new ArrayList<>();

    for (int i = 5; i < blastIt.length; i++) {
        if(blastIt[i].startsWith("Lambda")){
            break;
        } else {
            seila.add(blastIt[i]);
            System.out.println(blastIt[i]);
        }
        model.addAttribute("RESULT", result);

    }      
    File f1 = new File(filepath);
    f1.delete();
    return "success";

}
如果可能的话,我想这样

http://localhost:8081/Ambase/success

要避免此问题,应使用
重定向属性
。它将把filePath的值添加到重定向视图参数中,您可以在控制器blasta中获得该值

要实现这一点,您需要在控制器函数processForm中再添加一个参数。在所有参数的末尾添加
RedirectAttributes
,然后在RedirectView语句的正上方添加以下行

attributes.addFlashAttribute("Filepath",filePath);
然后,您可以在blasta控制器函数中的ModelMap中获得该属性


希望这对你有帮助。干杯。

尝试将此代码添加到servlet config.xml

<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />


为什么要用
@modeldattribute(“filepath”)
注释filepath?您是否应该使用@PathVariable(“Filepath”)获取POST/get参数?此外,无法使用POST请求重定向视图,因此无法隐藏查询路径。重定向是在客户端完成的,而不是在服务器上。我删除了POST请求,但另一个我没有得到的请求,我使用modelattribute将创建文件的路径传递给另一个控制器,因此如果要使用通过重定向传递给另一个控制器的变量,我可以使用该文件,您应该使用PathVariable而不是MoldelAttribute,尤其是在变量为字符串的情况下。此外,如果在重定向过程中需要依赖关键信息,这也是一个设计问题。客户端web浏览器可以忽略重定向。它用于防止用户在点击刷新按钮时重复提交请求。所以,我只需要在那里更改为pathvariable?或者我需要做些别的事情?在Spring4.x.x中,它是
忽略redirect=“true”上的默认模型

<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />