简体   繁体   English

spring mvc use model.addAttribute(nav)无法使用$ {nav.id}获取jsp中父类的属性

[英]spring mvc use model.addAttribute(nav) can not get the parent class’s attributes in jsp with ${nav.id}

i am new Spring learner. 我是新的春季学习者。

  1. i'm really confused about model.addAttribute 我真的对model.addAttribute感到困惑
  2. how can i get all attributes(include parent) 我如何获取所有属性(包括父项)

    in below there are my code,see this code,please: 在下面有我的代码,请参见以下代码:


Controller: 
@RequestMapping("/nav/addOrEdit")
public String navAdd(Nav nav,ModelMap model){
    if(nav.getId()!=null&&nav.getId()!=0) {
        nav=siteService.getNav(nav.getId());
        model.addAttribute(nav);
        System.out.println("nav.id:"+nav.getId());
    }
    return "CJ/nav/addOrEdit";
}

Nav:
public class Nav extends PO{
    private static final long serialVersionUID = -13569672251584L;

    protected String code;
    @NotEmpty
    protected String name;

    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

PO:
public class PO implements Serializable {

    private static final long serialVersionUID = 4572077184754045588L;
    protected Long Id;

    public Long getId() {return Id;}
    public void setId(Long id) {Id = id;}
}

jsp:
console.log("${nav}"); -->Nav{code=1, name=test},

no id,how can i get the parent class's attributes? 没有ID,如何获取父类的属性?

In your case you are adding your parent class object in Map . 在您的情况下,您要在Map中添加父类object So you can not directly access that object instead of putting object directly into Map add that object with some key and directly access it on JSP. 因此,您不能直接访问该object而不能将对象直接放入Map ,并使用一些键添加该对象并在JSP上直接访问它。 ie

model.addAttribute("nav",nav);

More details check Spring Documention of Model interface 更多详细信息请查看Spring Documentation of Model接口

Now you can access your value directly. 现在,您可以直接访问您的价值。

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

相关问题 在Spring MVC中-model.addAttribute用于动态数据以及如何将数据放置在jsp文件中 - In Spring MVC - model.addAttribute usage for dynamic data and how to place the data in jsp file frontpage从model.addAttribute获取对象的长类型ID失去精度 - frontpage get object's long type id from model.addAttribute lose precision Model.addAttribute 不向 .jsp 传递任何东西 - Model.addAttribute not passing any thing to the .jsp model.addAttribute()参数 - model.addAttribute() parameters Spring MVC:在Controller中使用model.addAttribute()将各种列表发送到Ajax方法 - Spring MVC : Using model.addAttribute() in Controller to send various Lists to Ajax method 如何使用 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? 春天的@ModelAttribute,model.addAttribute有什么区别? - What is the difference between @ModelAttribute, model.addAttribute in spring? model.addAttribute() 对于每个循环 - model.addAttribute() For Each Loop “ model.addAttribute”从控制器发送到JSP的数据类型是什么? - What is the type of data which, is sent by `model.addAttribute` from controller to JSP 做“model.addAttribute()”和“session.setAttribute()”的区别 - difference in doing “model.addAttribute()” and “session.setAttribute()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM