简体   繁体   English

model.addAttribute()参数

[英]model.addAttribute() parameters

I'm new to Spring MVC Framework. 我是Spring MVC框架的新手。 I'm doing some self study to extend my knowledge in Java. 我正在做一些自学,以扩展我的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()代码定义的方式,但是如果我错了,请更正我。

Controller requests something from the Data Access Object > Data Access Object gets the data from a Database or a Model through 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 model as parameter in public String getProducts(Model model) considered the dependency injection model作为参数包含在公共String getProducts(Model model)被视为依赖项注入

Is products (within quotes) in model.addAttribute("products",products); model.addAttribute("products",products); products (带引号model.addAttribute("products",products); just a name which I can change to whatever I like or should it match something? 只是一个名称,我可以将其更改为自己喜欢的名称,或者应该与之匹配?

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. 谢谢。

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. 是的,模型是通过spring实例化并注入到您的方法中的,这意味着如果模型属性中的任何一个与请求中的任何内容匹配,它将被填充。 and it should be the last param in the method 它应该是方法中的最后一个参数

model.addAttribute("products",products);

"products" is just a name which you can use it in your view get the value with ${products} “产品”只是一个名称,您可以在视图中使用它,并通过${products}获得价值

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. 是的,您选择模型和ModelAndView。
  2. Yes, simple. 是的,很简单。

暂无
暂无

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

相关问题 model.addAttribute() 对于每个循环 - model.addAttribute() For Each Loop Model.addAttribute 不向 .jsp 传递任何东西 - Model.addAttribute not passing any thing to the .jsp 春天的@ModelAttribute,model.addAttribute有什么区别? - What is the difference between @ModelAttribute, model.addAttribute in spring? 做“model.addAttribute()”和“session.setAttribute()”的区别 - difference in doing “model.addAttribute()” and “session.setAttribute()” request.setAttribute()和model.addAttribute之间有什么区别? - what differencies between request.setAttribute() and model.addAttribute? Spring Boot model.addAttribute - 如何让消息正确显示? - Spring Boot model.addAttribute - how to get message to display correctly? 在Spring MVC中-model.addAttribute用于动态数据以及如何将数据放置在jsp文件中 - In Spring MVC - model.addAttribute usage for dynamic data and how to place the data in jsp file 如何使用 Model.addAttribute() 添加的变量作为参数传递给 thymeleaf 的 hasRole 方法? - How can I use the variables that added by Model.addAttribute() to pass as a parameter in hasRole method of thymeleaf? 在model.addAttribute(“name”,value)和mv.addObject(“name”,value)之间进行区分? - Differencing between model.addAttribute(“name”,value) and mv.addObject(“name”,value)? frontpage从model.addAttribute获取对象的长类型ID失去精度 - frontpage get object's long type id from model.addAttribute lose precision
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM