Spring MVC向用户返回空白视图

时间:2013-04-16 22:00:32

标签: java html spring-mvc

我有一个表单,当用户将记录插入数据库时​​,他们可能希望同时创建一个新记录,并且他们可以单击一个新按钮。我有一个控制器被定义为接受此命令,并且仅在填充了列表框的情况下向用户返回空白视图,但是视图不会返回空白,它具有从先前插入到数据库中的数据。

代码:

用户选择以下按钮

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

控制器方法:

我想摆脱旧的犯罪对象

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

2 个答案:

答案 0 :(得分:1)

我将Controller方法设置为返回String,并使用以下内容将用户发送回视图:

return "redirect: crime_registration.htm";

答案 1 :(得分:1)

myCriminalListmyVictimList等似乎不是local variables而是instance variables,因此后续方法调用可以共享和访问它们。确保将这些字段声明为本地字段,以便它们绑定到该单个请求,并且在该请求范围之外无法看到。

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

作为旁注,为什么不进一步简化它:

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