1

I'm working on a Spring Boot web application, and have the main page which has a dropdown box of files, and a button which reads each in.

When they are read in, it redirects to a page where the data is displayed. But when I refresh the page or go back to home, then read it again, the array storing the file data gets added onto, which is then displayed each time. How can I fix this?

@RequestMapping(value = "cnfgRead", method = RequestMethod.GET)
    public String readCNFG(Model model, @RequestParam String cnfgListId) {
        ReadCNFG readCNFG = new ReadCNFG();

        //Reads in data from specified file using param
        ReadCNFG.readCNFG(cnfgFile + cnfgListId);

        model.addAttribute("Array", readCNFG.getOrigFitLines());
        return "cnfgView";
    }

Or Do I need to put the array back to empty somehow before the data is read in within the controller?

Travisty
  • 195
  • 2
  • 11
  • 1
    @Scope("request") would "reset" the Controller for each request. Your problem looks like the scope. It's not recommended to use variables on the class scope unless you are really sure is that what you need. – Marcelus Trojahn Sep 12 '16 at 14:50
  • This was indeed the issue, but was resolved by simply included a arr.clear(); before data is added to them :) – Travisty Sep 12 '16 at 14:53
  • Keep in mind though, that a different person in another browser would also clear that same instance of that array. It might not be what you want on every case. – Marcelus Trojahn Sep 12 '16 at 16:43
  • @M.'Coderdood'Trojahn how would I get around people on different browsers clearing the array? – Travisty Sep 13 '16 at 15:27
  • Check this out. Very straight forward explanation http://stackoverflow.com/questions/16795303/must-spring-mvc-classes-be-thread-safe – Marcelus Trojahn Sep 13 '16 at 16:42

0 Answers0