spring mvc: How Can I give the <form:textarea /> tag a default value?

前端 未结 7 1409
悲&欢浪女
悲&欢浪女 2021-01-18 17:19

I have a problem with giving the tag a default value.
When I created a .jsp file as follow,



        
相关标签:
7条回答
  • 2021-01-18 18:03

    The Spring form tags are for data binding (e.g. your model attributes are bind to the form via path attribute of the form). If you need to specify defaults, then set the Content attribute of the ModelYouArePassingToView to the desired default value in the controller before it gets to the view.

    If your using Spring MVC and @RequestMapping, a really good place for this in your controller would be your @ModelAttribute method. For example:

    @ModelAttribute("modelYouArePassingToView")
    public ModelYouArePassingToView createDefault() {
       //construct it with default values for "Content" 
       //attribute, and it will show up in textarea after the bindind
       ModelYouArePassingToView myapv = new ModelYouArePassingToView(); 
       myapv.setContent(..); //default value
       return myapv;
    }
    

    In your form, make sure to include the modelAttribute tag attribute:

    <form:form modelAttribute="modelYouArePassingToView" ...>
      <form:textarea path="content" ..> 
    
    0 讨论(0)
提交回复
热议问题