0

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

代码:

用户选择以下按钮

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

2 回答 2

1

我将控制器方法设置为返回字符串,并使用以下内容将用户发送回视图:

return "redirect: crime_registration.htm";
于 2013-04-16T22:26:44.433 回答
1

myCriminalList,myVictimList等似乎不是,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");
}
于 2013-04-16T22:27:48.530 回答