java model.addAttribute() 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40303916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:06:28  来源:igfitidea点击:

model.addAttribute() parameters

javaspringspring-mvc

提问by jordan

I'm new to Spring MVCFramework. I'm doing some self study to extend my knowledge in Java.

我是Spring MVC框架的新手。我正在做一些自学以扩展我在 Java 方面的知识。

This is how I understand the getProducts()code definition from a tutorial I'm following but please correct me if I'm wrong.

这就是我getProducts()从我正在关注的教程中理解代码定义的方式,但如果我错了,请纠正我。

Controllerrequests something from the Data Access Object>Data Access Objectgets the data from a Databaseor a Modelthrough the getProductList()method >Stores the information to list >Then binds the list to the model.

控制器请求从一些数据访问对象>数据访问对象从获取数据的数据库模型透过getProductList()方法>将信息存储到列表>然后结合列表的模型。

So I got two question about this.

所以我有两个关于这个的问题。

Is the inclusion of modelas parameter in public String getProducts(Model model)considered the dependency injection

model在 public 中包含as 参数是否被String getProducts(Model model)认为是依赖注入

Is products(within quotes) in model.addAttribute("products",products);just a name which I can change to whatever I like or should it match something?

products在(内引号)model.addAttribute("products",products);只是一个,我可以改变任何我喜欢还是应该符合什么名字?

public class HomeController {

    private ProductDao productDao = new ProductDao();

    @RequestMapping("/")
    public String home(){
        return "home";
    }

    @RequestMapping("/productList")
    public String getProducts(Model model){
        List<Product> products = productDao.getProductList();
        model.addAttribute("products",products);

        return "productList";  //productList string is the productList.jsp which is a view
    }

    @RequestMapping("/productList/viewProduct")
    public String viewProduct(){
        return "viewProduct";
    }
}

I'd appreciate any explanation or comment.

我很感激任何解释或评论。

Thank you.

谢谢你。

回答by kuhajeyan

Yes, model is instantiated by spring and injected to your method, means if any of model attribute matches anything in request it will be filled. and it should be the last param in the method

是的,模型由 spring 实例化并注入到您的方法中,这意味着如果任何模型属性与请求中的任何内容匹配,它将被填充。它应该是方法中的最后一个参数

model.addAttribute("products",products);

"products" is just a name which you can use it in your view get the value with ${products}

“产品”只是一个名称,您可以在视图中使用它来获取价值 ${products}

回答by Byeon0gam

My code. this is sample.

我的代码。这是样本。

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/settings/product")
    public ModelAndView showProduct(ModelAndView mav, HttpServletRequest req, Authentication auth) {
        CustomUserDetail customUserDetail = (CustomUserDetail) auth.getPrincipal();

        int associationIdx = customUserDetail.getAccount().getAssociation().getIdx();

        String language = CookieUtil.getCookieValue(req, "lang");

        Association association = associationService.findAssociationByIdx(associationIdx);

        List<AssociationProductColumnDefine> columns = associationService.findByAssociationAndLanguage(association,
                language);
        List<AssociationProductColumnDefine> source = associationService.findByAssociationAndLanguage(association,
                "ko-kr");

        mav.addObject("association", association);
        mav.addObject("source", source);
        mav.addObject("columns", columns);

        mav.setViewName("/association/association_settings_product");

        return mav;
    }
  1. Yes, you choice model and ModelAndView.
  2. Yes, simple.
  1. 是的,您选择模型和 ModelAndView。
  2. 是的,简单。