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