3

I'm using Spring MVC 3 and I have the following Controller

@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");
    }

}

This controller get the filepath and use to do a blast

@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";

}

Everything works fine, but I still get the filepath in the url.

http://localhost:8081/Ambase/success?Filepath=filePath

And I want this way if it's possible

http://localhost:8081/Ambase/success
10
  • why did you annotate filepath with @ModelAttribute("Filepath")? should you not using @PathVarible("Filepath") to get the POST/GET parameter? Commented Sep 27, 2012 at 19:25
  • also, it is impossible to redirect view with a POST request so it is impossible to hide the query path. redirection is done on the client side, not on the server. Commented Sep 27, 2012 at 19:27
  • Well, I removed the POST request, but the other one i didn't get, i use modelattribute to pass the path where the file is created to the other controller so i can use the file Commented Sep 27, 2012 at 19:36
  • if you want to use the variable passing to another controller via redirection, you should use PathVariable instead of MoldelAttribute, especially in this case where the variable is a string. Also, it is a design problem if you need to rely on a crucial information during redirection. Redirection can be ignored by client-side web browser. It is used to prevent user from repeatedly submitting the request when they hit refresh button. Commented Sep 27, 2012 at 19:54
  • So, I just need to change to pathvariable there? or i need to do something else? Commented Sep 27, 2012 at 19:58

2 Answers 2

3

try adding this code to servlet-config.xml

<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />
Sign up to request clarification or add additional context in comments.

1 Comment

In Spring 4.x.x it is ignore-default-model-on-redirect="true"
0

To avoid this issue you should use RedirectAttributes. It will add the value of filePath to the redirect view params and you can get that in the controller blasta.

To achieve this you need to add one more parameter in the controller function processForm. At the end of all the parameters add RedirectAttributes attributes and then add following line just above the RedirectView statement.

attributes.addFlashAttribute("Filepath",filePath);

And then you can get this attribute in the ModelMap inside blasta controller function.

Hope this helps you. Cheers.

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.