devdar
devdar

Reputation: 5654

Spring MVC Returning a Blank View To User

I have a form when the user inserts a record into the database they may wish to create a new record at the same time and they can click on a new button. I have a Controller defined to accept this command and return to the user a blank view only with the list boxes being populated, however the view is not coming back blank it has the data from the previous insert into the database.

Code:

User select the following button

<button class="btn" value="new" onclick="submitPage('${pageContext.request.contextPath}/crime_registration_new.htm');" type="button" >New</button>

Controller Method:

I would like to get rid of the old crime object

    @RequestMapping(value="crime_registration_new.htm", method = RequestMethod.POST)
    public ModelAndView loadNew(HttpServletRequest request, 
        HttpServletResponse response,
        @ModelAttribute Crime crime,
        Model model) throws Exception {

    try{

        logger.debug("In Crime Registration Controller");

        myCriminalList.put("dbcriminalList", this.citizenManager.getListOfCriminals());
        myVictimList.put("dbvictimList", this.citizenManager.getListOfVictims());
        myStatusList.put("statusList", this.statusManager.getStatusList());
        myCrimeCategoryList.put("crimeCategoryList", this.crimeCategoryManager.getCrimeCategoryList());
        myCrimeLevelList.put("crimeLevelList", this.crimeLevelManager.getCrimeLevelList());
        myOfficerList.put("investigatingOfficerList", this.officerManager.getOfficersList());

        model.addAttribute("dbcriminals", myCriminalList);
        model.addAttribute("dbvictims",   myVictimList);
        model.addAttribute("status", myStatusList);
        model.addAttribute("crimeCategory", myCrimeCategoryList);
        model.addAttribute("crimeLevel",myCrimeLevelList);
        model.addAttribute("officers",myOfficerList);
        model.addAttribute("crimeRecordNoStatus", "true");
        model.addAttribute("crimeRecordNoStatus", "true");
        model.addAttribute("save", "enabled");

        return  new ModelAndView("crime_registration");          
    }catch(Exception e){                
        logger.debug("Exception In CrimeRegistration Controller : " + e.getMessage());
        return new ModelAndView("crime_registration");               
    }    
}

Upvotes: 0

Views: 3134

Answers (2)

kryger
kryger

Reputation: 13181

myCriminalList, myVictimList, etc. don't seem to be local variables but instance variables so they're shared and accessible by subsequent method calls. Make sure you declare these fields as local so they're bound to that single request and can't be seen outside that request scope.

@RequestMapping(value="crime_registration_new.htm", method=RequestMethod.POST)
public ModelAndView loadNew( (...) ) throws Exception {
        // local variable - only accessible by current request
        List<Criminal> myCriminalList = new ArrayList<Criminal>();

        myCriminalList.put("dbcriminalList",
                this.citizenManager.getListOfCriminals());
        model.addAttribute("dbcriminals", myCriminalList);
        return new ModelAndView("crime_registration");
        // (...)
}

And as a side-note, why not simplify it even further:

@RequestMapping(value = "crime_registration_new.htm", method = RequestMethod.POST)
public ModelAndView loadNew( (...) ) throws Exception {
        // no need for intermediary List
        model.addAttribute("dbcriminals", this.citizenManager.getListOfCriminals());
        // (...)
        return new ModelAndView("crime_registration");
}

Upvotes: 1

devdar
devdar

Reputation: 5654

I set the Controller method to return String and i used the following to send the user back to the view:

return "redirect: crime_registration.htm";

Upvotes: 1

Related Questions