0

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

}

2 Answers 2

1

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"/> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your advice. But I want to use SampleDto as a read-only object so I suppose SampleDto should not be in the backing object.. sorry my question is not make sense.
If it's read-only, then why would you need to have an <input> element based on it?
1

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

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

2 Comments

Thank you for your answer and I apologize for my confusing question. In your answer, "name" could be a field of "SampleForm" class but I want to show value of "SampleDto.name" using <form:input> tag.
@yusaku no problems. What you are looking for is not yet there in Spring (Please see jira.springsource.org/browse/SPR-5025). Instead of using <form:input>, you will have to use JSTL or some other way to build input tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.