繁体   English   中英

如何在jsf中使用xhtml表单映射模型对象属性?

[英]How to map model object attributes with xhtml form in jsf?

在春季,我们可以将模型对象属性映射到UI表单元素。 如何在JSF中完成此映射? JSF教程中讨论的主题名称是什么? 我们可以在JSP页面中操作映射的模型对象属性。 在xhtml页面中也可以吗?

评论中要求的春季示例样本。

用户域对象被映射到控制器中的jsp表单,如以下示例代码所示。 在此映射之前,在servlet xml中作为command进行。 现在我们可以在控制器本身中做到这一点。

@RequestMapping(value = "/user", method = RequestMethod.GET)
       public ModelAndView user() {
          return new ModelAndView("user", "command", new User());
       }





@RequestMapping(value = "/createuser", method = RequestMethod.POST)
public String saveUser(@ModelAttribute("SpringWeb")User user, 
           ModelMap model) {

    model.addAttribute("name", user.getName());
    model.addAttribute("country", user.getCountry());
    model.addAttribute("id", user.getId());
    userService.saveUser(user);
    return "results";
}

由于我们已经在控制器中映射了对象,因此我们可以按以下方式在jsp中操作属性

    <form:form method="POST" action="/HibernateExample/createuser">
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td><form:label path="country">Country</form:label></td>
        <td><form:input path="country" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>

班级用户

    @Entity
@Table(name="User")
public class User {

    private String id;
    private String name;
    private String country;

JSF

    <h:form>
        <h:outputLabel for="name" value="name" />
        <h:inputText id="name" value="#{fooController.name}" />
        <!--...-->

        <h:commandButton value="Save" action="#{fooController.doSaveUser()}" />
    </h:form>

逻辑(ManagedBean)

@Named //@ManagedScoped if you don't use CDI
@RequestScoped //Or a different scope (e.g. @SessionScoped)
public class FooController {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void doSaveUser() {
        //Save user...
    }

}

我认为您应该首先阅读BalusC的博客或其他任何内容。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM