如何使用<表单 :输入>对于非表单对象

发布于 2024-10-19 16:34:06 字数 1609 浏览 0 评论 0原文

Spring标签可以生成带有id和name属性的标签。我认为这个功能很有用,我想在使用非表单对象时使用它。 请看下面的代码。

“dto”对象被添加到“model”对象以及“form”中,然后我想自动生成id属性。但是, 标签似乎能够用于绑定表单对象。我是否必须制作自定义标签才能实现类似的功能?任何帮助将不胜感激?

[Controller]

@RequestMapping(method = RequestMethod.GET)
public String show(Model model, HttpServletRequest request) {


    SampleForm form = new SampleForm();
    form.setName("Name of Form Object");

    SampleDto dto = new SampleDto();
    dto.setName("Name of Dto Object");

    model.addAttribute("form", form);
    model.addAttribute("dto", dto);

    return "sample/input";

}

[JSP]

<body>
<form:form modelAttribute="form" method="post">

    <%-- Generate with id attribute like <input id="name" name="name" type="text" value="Name of Form Object"/>  --%>
    <form:input path="name" />

    <%-- I tried below but an error occured--%>
    <%-- <form:input path="${dto.name}" /> --%>

    <%-- Just a String display like "Name of Dto Object" --%>
    ${dto.name}

    <input type="submit" name="register" value="register" />
</form:form>
</body>

[Form]
public class SampleForm {

    private String name;

    public String getName() {
        return name;
    }

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

[Dto]
public class SampleDto {

    private String name;

    public String getName() {
        return name;
    }

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

}

Spring tag <form:input> can generate <input> tag with id and name attributes. I think this feature is useful and I want to use this when using non form object.
Please take a look at codes below.

"dto" object is added to "model" object as well as "form" then I want to generate id attribute automatically. However, <form:input> tag seems to be able to use for binding form object. Do I have to make a custom tag in order to realize the similar feature? Any help will be appreciated?

[Controller]

@RequestMapping(method = RequestMethod.GET)
public String show(Model model, HttpServletRequest request) {


    SampleForm form = new SampleForm();
    form.setName("Name of Form Object");

    SampleDto dto = new SampleDto();
    dto.setName("Name of Dto Object");

    model.addAttribute("form", form);
    model.addAttribute("dto", dto);

    return "sample/input";

}

[JSP]

<body>
<form:form modelAttribute="form" method="post">

    <%-- Generate with id attribute like <input id="name" name="name" type="text" value="Name of Form Object"/>  --%>
    <form:input path="name" />

    <%-- I tried below but an error occured--%>
    <%-- <form:input path="${dto.name}" /> --%>

    <%-- Just a String display like "Name of Dto Object" --%>
    ${dto.name}

    <input type="submit" name="register" value="register" />
</form:form>
</body>

[Form]
public class SampleForm {

    private String name;

    public String getName() {
        return name;
    }

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

[Dto]
public class SampleDto {

    private String name;

    public String getName() {
        return name;
    }

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-10-26 16:34:06

一个表单只能有一个支持对象。在您的示例中,支持对象是 SampleForm 的实例。您可以在 SampleForm 类中添加对 SampleDto 实例的引用:

public class SampleForm {
  private String name;
  private SampleDto dto;
  public String getName() {
     return name;
  }
  public void setName(String name) {
     this.name = name;
  }
  public SampleDto getDto() {
     return dto;
  }
  public void setDto(SampleDto dto) {
     this.dto = dto;
  }
}

然后您可以在 JSP 中执行此操作:

<form:input path="dto.name"/> 

A form can only have one backing object. In your example the backing object is an instance of SampleForm. You could add a reference to a SampleDto instance in your SampleForm class:

public class SampleForm {
  private String name;
  private SampleDto dto;
  public String getName() {
     return name;
  }
  public void setName(String name) {
     this.name = name;
  }
  public SampleDto getDto() {
     return dto;
  }
  public void setDto(SampleDto dto) {
     this.dto = dto;
  }
}

Then you could do this in your JSP:

<form:input path="dto.name"/> 
久伴你 2024-10-26 16:34:06

如果你想从 dto 生成 id 属性,那么它应该是

<form:input id="${dto.name}" path="name" />

If you want to generate id attribute from dto then it should be

<form:input id="${dto.name}" path="name" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文