【问题标题】:How can I avoid having too many arguments in Spring Java controllor如何避免在 Spring Java 控制器中有太多参数
【发布时间】:2013-08-09 07:07:48
【问题描述】:

在我的 Spring Web 应用程序中:

    @RequestMapping(value = NEW)
public String addProduct(@RequestParam String name, @RequestParam(required = false) String description,
                         @RequestParam String price, @RequestParam String company, ModelMap model,
                         @RequestParam(required = false) String volume, @RequestParam(required = false) String weight) {
    try {
        productManagementService.addNewProduct(name, description, company, price, volume, weight);
        model.addAttribute("confirm", PRODUCT_ADDED);
        return FORM_PAGE;
    } catch (NumberFormatException e) {
        logger.log(Level.SEVERE, INVALID_VALUE);
        model.addAttribute("error", INVALID_VALUE);
        return FORM_PAGE;
    } catch (InvalidUserInputException e) {
        logger.log(Level.SEVERE, e.getMessage());
        model.addAttribute("error", e.getMessage());
        return FORM_PAGE;
    }
}

减少/绑定参数总数的可能方法是什么。

【问题讨论】:

    标签: java spring-mvc parameters controller parameter-passing


    【解决方案1】:

    创建一个class,封装该类中的所有属性,然后接受该类对象作为您的@ModelAttribute。比如:

    public class MyData {
    
        private String name;
    
        private String description;
    
        private String price;
    
        private String company;
    
        private String volume;
    
        private String weight;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public String getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = price;
        }
    
        public String getCompany() {
            return company;
        }
    
        public void setCompany(String company) {
            this.company = company;
        }
    
        public String getVolume() {
            return volume;
        }
    
        public void setVolume(String volume) {
            this.volume = volume;
        }
    
        public String getWeight() {
            return weight;
        }
    
        public void setWeight(String weight) {
            this.weight = weight;
        }
    
    }
    

    然后像这样修改你的 addProduct 方法:

    public String addProduct(@ModelAttribute MyData myData, ModelMap model) 
    

    【讨论】:

    • 记下参数的名称及其getter/setter。
    • 封装将在哪里进行?在视图级别?如果是的话,你能描述一下吗?
    • @Prashant 您只需要创建一个类并封装该类中的属性,如我的回答中所述。
    • @JunedAhsan:我得到了封装在类中的概念,但我的困惑是当数据以表单形式提交时。并发送了一个请求。然后它将接近相关的控制器。所以在这个将数据从jsp页面传输到控制器的操作中,我们在哪里对myData类进行封装。在jsp页面中?
    • @Juned,您需要以ModelAttribute 而不是RequestParam 的身份执行此操作。
    【解决方案2】:

    创建表单类,即

    class MyForm{
    String name;
    String price;
    String description;
    ...
     // Getters and setters included
    }
    

    并且喜欢

    @RequestMapping(value = NEW)
    public String addProduct(@ModelAttribute MyForm myForm)
    

    MyForm 的实例化以及将请求参数绑定到其属性并添加到 ModelMap 是由 spring 在幕后完成的。

    来源:Spring Docs

    方法参数上的@ModelAttribute 表示该参数应该 从模型中检索。如果模型中不存在,则参数 应该先实例化,然后添加到模型中。一旦出现 在模型中,参数的字段应该从所有 具有匹配名称的请求参数。这被称为数据 Spring MVC 中的绑定,这是一种非常有用的机制,可以让您从 必须单独解析每个表单字段。

    【讨论】:

    • @GriffeyDog:所以你说我需要在最后一个操作中创建模型并将其添加到模型映射并将其传递到我将封装它的 jsp 页面,然后将请求提交给 addProduct行动
    • 这真的很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多