Announcement Announcement Module
Collapse
No announcement yet.
Hide @ModelAttribute variables from appearing in URL? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hide @ModelAttribute variables from appearing in URL?

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

    Code:
    @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
    Code:
    @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.
    Code:
    http://localhost:8081/Ambase/success?Filepath=filePath
    And I want this way if it's possible
    Code:
    http://localhost:8081/Ambase/success

  • #2
    Hello

    The success view should be a RedirectView as you need to clear the model. When using Redirects, you can do

    redirectView.setExposeModelAttributes(false);
    Then no more ModelAttribute in URL

    Comment


    • #3
      Originally posted by shoa View Post
      Hello

      The success view should be a RedirectView as you need to clear the model. When using Redirects, you can do



      Then no more ModelAttribute in URL
      But here do I put this? in the success?

      Comment


      • #4
        Why do you need Filepath

        Code:
        @RequestMapping(value="FileUploadForm",method=RequestMethod.POST)
         ...
         return new ModelAndView(new RedirectView("success?Filepath="+filePath, true, true, false));
          ...

        You many need

        Code:
          return new ModelAndView(new RedirectView("success");

        Comment

        Working...
        X