English 中文(简体)
春天、 模型映射、 从jsp 获取属性
原标题:Spring, modelmap, getting attributes from the jsp

我开发了一个带有春季框架和其他一些框架的 MVC 应用程序( 我是一个初学者 ) 。 我有一个控制器来管理jsp 处理, 例如当我想在个人列表中添加新人时, 我调用一个瞬间化个人对象, 并将其传送到与添加方法相应的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";
}

My problem is that now, I want to pass to add to the model two different Objects, for example, I want to pass the new UserDomain() and also an other object which is from an other table in my database, for example a new UserSecurity() . I think I should use a modelMap instead of the model.addAttribute... , but I can t do this, so if someone could help me. I get my model from the jsp by a code like :

<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" />

非常感谢你帮我

问题回答

只需将多个对象传递到视图中就不成问题了,只需使用 < code> model.addAttrimitte 多次,然后您就可以访问两个对象。

但是,如果您想要编辑 > 中的多个模型, 您需要创建一个包含两个对象的类别 :

public class UserDomainSecurity {

    private UserDomain userDomain;
    private UserSecurity userSecurity;

    // getters and setters for both

}

然后,将一个实例传到视野中:

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

以表形式使用:

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

创建所有这些额外的类有时会很烦恼,但它有些逻辑性。包装类在窗体中创建了某种命名空间,从而将您想要编辑的单个对象分隔开来。

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

在此情况下,我假设你们是 " code > User Security 和 " code > UserDomain 彼此相对。

这么说吧

public class UserDomain {

     public UserSecurity userSecurity
     public String firstName;
     public String lastName;

     // getters and setters...
}

并且你有你的用户安全 类似的东西,

public class UserSecurity {

     public String someSecurityProperty;

     // getters and setters...
}

由于 usersecurity 财产可以公开查阅,所以您可以按您在主计长办公室所做的做,

@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";
}

然后在添加页中访问它。 jsp 喜欢它像下面一样是一个物体属性,

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

您请注意, 我通过 UserDomain 类中宣布的属性访问某些安全财产 。





相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签