我是Spring MVC框架的新手。我正在做一些自我研究,以扩展我的Java知识。
这就是我从一个教程中理解getProducts()代码定义的方式,但是如果我错了,请纠正我。
Controller请求数据访问对象 > 数据访问对象通过getProductList()方法>从数据库或E 111模型E 212获取数据,然后将列表绑定到模型中。
关于这个我有两个问题。
是否将model作为参数包含在公共String getProducts(Model model)中,认为依赖项注入
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";
}
}如有任何解释或评论,我将不胜感激。
谢谢。
发布于 2016-10-28 11:25:08
是的,模型由spring实例化并注入到您的方法中,这意味着如果任何一个模型属性与请求中的任何内容匹配,它将被填充。它应该是该方法的最后一个参数。
model.addAttribute("products",products);"products“只是一个名称,您可以在视图中使用它,用${products}获取值
发布于 2016-10-28 14:34:58
我的密码。这是样品。
@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;
}https://stackoverflow.com/questions/40303916
复制相似问题