【问题标题】:Spring, modelmap, getting attributes from the jspSpring,modelmap,从jsp获取属性
【发布时间】:2012-05-26 22:26:33
【问题描述】:

我使用 spring 框架和其他一些框架开发了一个 MVC 应用程序(我是初学者)。我有一个控制器来管理 jsp 处理,例如,当我想将新人员添加到我的“人员列表”时,我调用实例化人员对象并将其传递给与 add 方法对应的 jsp 视图。我通过这样的方法做到这一点:

@RequestMapping(value = "/persons/add", method = RequestMethod.GET)
public String getAdd(Model model) {
    logger.debug("Received request to show add page");

    // Create new UserDomain and add to model
    // This is the formBackingOBject
    model.addAttribute("personAttribute", new UserDomain());

    // This will resolve to /WEB-INF/jsp/addpage.jsp
    return "addpage-tiles";
}

我的问题是,现在,我想通过向模型添加两个不同的对象,例如,我想通过“new UserDomain()”以及来自我数据库中另一个表的另一个对象,例如'new UserSecurity()'。 我想我应该使用'modelMap'而不是'model.addAttribute ...',但我不能这样做,所以如果有人可以帮助我。 我通过以下代码从 jsp 获取模型:

<form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">
<table>
    <tr>
        <td><form:label path="firstName">First Name:</form:label></td>
        <td><form:input path="firstName"/></td>
    </tr>
    <tr>
        <td><form:label path="lastName">Last Name</form:label></td>
        <td><form:input path="lastName"/></td>
    </tr>   
     <tr>
        <td><form:label path="userName">User name</form:label></td>
        <td><form:input path="userName"/></td>
    </tr>   
    <tr>
        <td><form:label path="email">E-mail</form:label></td>
        <td><form:input path="email"/></td>
    </tr>           
</table>
<input type="submit" value="Save" />

非常感谢你帮助我。

【问题讨论】:

  • model.addAttribute("userSecurity", new UserSecurity()) 有什么问题?
  • 没有错,但实际上我不能使用 model.addAttribute("userSecurity", new UserSecurity());和 model.addAttribute("personAttribute", new UserDomain());因为在 jsp 中我不能写
    。 jsp中只允许包含一个modelAttribute

标签: spring spring-mvc


【解决方案1】:

简单地将多个对象传递给视图不是问题——只需多次使用model.addAttribute,然后您就可以访问这两个对象。

但是,如果您想在&lt;form:form&gt; 中编辑多个模型,则需要创建一个包含这两个对象的类:

public class UserDomainSecurity {

    private UserDomain userDomain;
    private UserSecurity userSecurity;

    // getters and setters for both

}

然后将 this 的一个实例传递给视图:

 model.addAttribute("userDomainSecurity", new UserDomainSecurity());

并以如下形式使用:

<form:form commandName="userDomainSecurity" method="POST" action="${saveUrl}">
...
  <form:input path="userDomain.firstName"/>
  ....
  <form:input path="userSecurity.someSecurityProperty"/>

有时不得不创建所有这些额外的类很烦人,但这有点合乎逻辑。包装类在表单中创建一种命名空间,从而分隔您要编辑的各个对象。

【讨论】:

    【解决方案2】:

    我想补充一下,因为这确实是我几分钟前遇到的当前问题。

    在这种情况下,我假设您是 UserSecurityUserDomain 是相对的。

    假设你有,

    public class UserDomain {
    
         public UserSecurity userSecurity
         public String firstName;
         public String lastName;
    
         // getters and setters...
    }
    

    你有类似的 UserSecurity,

    public class UserSecurity {
    
         public String someSecurityProperty;
    
         // getters and setters...
    }
    

    既然userSecurity 属性可以公开访问,那么你可以做你在Controller 中所做的事情,

    @RequestMapping(value = "/persons/add", method = RequestMethod.GET)
    public String getAdd(Model model) {
    logger.debug("Received request to show add page");
    
    // Create new UserDomain and add to model
    // This is the formBackingOBject
    model.addAttribute("userDomainSecurity", new UserDomain());
    
    // This will resolve to /WEB-INF/jsp/addpage.jsp
    return "addpage-tiles";
    }
    

    然后只需在 addpage.jsp 中访问它,就像它是下面的对象属性一样,

    <form:form commandName="userDomainSecurity" method="POST" action="${saveUrl}">
    ...
    <form:input path="firstName />
    <form:input path="lastname />
      ....
      <form:input path="userSecurity.someSecurityProperty"/>
    

    如您所见,我通过 UserDomain 类中声明的属性访问 someSecurityProperty。

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2012-01-28
      相关资源
      最近更新 更多