简体   繁体   English

如何使用Hibernate在Spring MVC中保存模型返回查看的列表

[英]How to save list that a model return to view in spring mvc with hibernate

Controller 控制者

 @RequestMapping(value = "/chartofaccount", method = RequestMethod.GET)
public String getChartofAccount(Model model) {

    try {

        List CoaCategoryList = new ArrayList();
        COACategoriesModel obj;
        List<COACategoriesModel> getCoaCategoryList = coaCategoriesService
                .getAllCOACategories();

        if (getCoaCategoryList.size() > 0) {
            for (int i = 0; i < getCoaCategoryList.size(); i++) {

                obj = getCoaCategoryList.get(i);
                CoaCategoryList.add(obj.getId());
                CoaCategoryList.add(obj.getName());
                model.addAttribute("list", CoaCategoryList);

            }
        }

    } catch (Exception ex) {
        log.error("Exception.." + ex);

    }

    return "fin/coa";
}

fin/coa.jsp fin / coa.jsp

<ct:Select setIdentity="coaCategoryId" list="${list}" selected="0" />

the above list successfully populate, In my custom tag. 以上列表已成功填充,在我的自定义标签中。

but 

Another controller 另一个控制器

    @RequestMapping(value = "/addCoaMaintenance", method = RequestMethod.POST)
public String addCoaCategory(
        @RequestParam("mainAccount") long mainAccount,
        @RequestParam("subAccount") long subAccount,
        @RequestParam("accountName") String accountName,
        @RequestParam("coaCategoryId") long coaCategoryId,
        @RequestParam("postingType") int postingType,
        @RequestParam("typicalBalance") int typicalBalance,
        @RequestParam("isActive") int isActive,
        Model model) {

     Date sysdate = null;
     String Message="";

     try{

     sysdate = new Date();

     Message="Operation Fail!";
     COAMaintenanceModel coaMaintenanceModel= new COAMaintenanceModel(mainAccount, subAccount, accountName, coaCategoryId, postingType, typicalBalance, isActive, GetSessionValue.getSysUserId(), GetSessionValue.getSysUserIp(), sysdate, 0);
     coaMaintenanceService.AddCOAMaintenance(coaMaintenanceModel);

     Message="Account Save Successfully";
     model.addAttribute("result_success", Message);

     }catch(Exception ex){
         log.error("Exception.."+ex);
         model.addAttribute("result_fail", Message);
     }



     return "fin/coa";

    }

as the this second one controller return fin/coa.jsp , list is null. 由于第二个控制器返回fin / coa.jsp ,所以list为null。 how to control this? 如何控制呢? I am using spring Mvc + hibernate. 我正在使用Spring Mvc + Hibernate。 How to make the first controller list make available, until I required. 在我需要之前,如何使第一个控制器列表可用。

I'm not sure I understood the problem correctly, however, the way I see it, you didn't include the list in the model in the second handler method / controller. 我不确定我是否正确理解了问题,但是,按照我的看法,您没有在第二个处理程序方法/控制器的模型中包含列表。

Are both handler methods (@RequestMapping annotated methods) inside the same controller class? 两种处理程序方法(带注释的@RequestMapping方法)是否都在同一控制器类中? If yes, one solution is to add @ModelAttribute on a metod that populates the list. 如果是,一种解决方案是在填充列表的方法上添加@ModelAttribute。 Eg you would add the following: 例如,您将添加以下内容:

@ModelAttribute("list")
public List getCategoryList() {
    List CoaCategoryList = new ArrayList();
    COACategoriesModel obj;
    List<COACategoriesModel> getCoaCategoryList = coaCategoriesService
            .getAllCOACategories();
    // this if is unncessary
    // if (getCoaCategoryList.size() > 0) {
    for (int i = 0; i < getCoaCategoryList.size(); i++) {
        obj = getCoaCategoryList.get(i);
        CoaCategoryList.add(obj.getId());
        CoaCategoryList.add(obj.getName());            
    }    
    return CoaCategoryList;
}

This will make attribute "list" available in models of all requests in this controller and you will not need to add it via model.addAttribute(...). 这将使属性“列表”在此控制器中所有请求的模型中可用,并且您无需通过model.addAttribute(...)添加它。

Other solution would be to populate the list manually in each handler and add it manually to the model (you already do that for the first handler method, but not in the second one). 其他解决方案是在每个处理程序中手动填充列表,然后将其手动添加到模型中(您已经在第一个处理程序方法中执行了此操作,但在第二个处理程序方法中已执行此操作)。

If the handler methods are not inside same controller class than you again need to make sure list is in the model before rendering the view (either by making both controller classes extending a common abstract controller were you add functionality to populate the list, or by manually populating the model, or some other way). 如果处理程序方法不在同一控制器类中,则您再次需要在呈现视图之前确保列表在模型中(通过使两个控制器类都扩展一个公共抽象控制器(添加功能来填充列表),或者通过手动填充模型或其他方式)。

PS. PS。 Also, consider taking a look at "Code Conventions for the Java Programming Language" (eg variable names in Java usually start with lowercase letter to distinguish them form class names) http://www.oracle.com/technetwork/java/codeconv-138413.html 另外,考虑考虑一下“ Java编程语言的代码约定”(例如,Java中的变量名通常以小写字母开头,以区分类名) 。http://www.oracle.com/technetwork/java/codeconv- 138413.html

One solution : test if your list is null or empty 一种解决方案:测试您的列表是否为空或为空

<c:if test="${not empty list}">
   <ct:Select setIdentity="coaCategoryId" list="${list}" selected="0" />
</c:if>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM