spring - jsr303 and Freemarker, Valid example -


please make validation jsr 303 pages freemarker. there're class course, controller, file messages.properties, created bean id = "messagesource"> need make page in freemarker create new course. when filling empty fields or incorrect range output error message.

public class course { @notempty  private string name; @notempty   private string category; @range(min = 20, max = 25)   int age;  //get , set }   notempty.course.name = name required! notempty.course.category= categoryis required! range.course.age = age value must between 20 , 25   @controller @requestmapping("/customer")  public class signupcontroller {  @requestmapping(value = "/signup", method = requestmethod.post) public string addcustomer(@valid course course, bindingresult result) {      if (result.haserrors()) {         return "signupform";     } else {         return "done";     }   }   @requestmapping(method = requestmethod.get)  public string displaycustomerform(modelmap model) {      model.addattribute("course", new course());     return "signupform";   }   }   <!-- bind messages.properties --> <bean   class="org.springframework.context.support.resourcebundlemessagesource"     id="messagesource">     <property name="basename" value="messages" /> </bean> 

how make page?

<html> <head> <style> .error { color: #ff0000; }  .errorblock { color: #000; background-color: #ffeeee; border: 3px solid #ff0000; padding: 8px; margin: 16px; } </style> </head>  <body> <h2>course:</h2>  <form class="form-horizontal" commandname="course"  method=post>     <fieldset>       <div class="control-group">         <label class="control-label">name</label>         <div class="controls">           <input id="name" name="name" class="span5" type="text"/>           <@spring.showerrors "<br>"  />           </div>       </div>         <div class="control-group">         <label class="control-label">description</label>         <div class="controls">           <textarea id="description" name="description" class="span5" rows="3"></textarea>           <@spring.showerrors "<br>"  />         </div>       </div>        </div>       <div class="form-actions">         <button id="createbutton" name="createbutton" class="btn btn-primary" type="submit">create</button>        </div>     </fieldset>   </form>  </body> </html> 

you should use spring form generate validations this, have add spring tag library path in html  go this..      <%@taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>      <html>      <head>     <style>     .error {     color: #ff0000;     }      .errorblock {     color: #000;     background-color: #ffeeee;     border: 3px solid #ff0000;     padding: 8px;     margin: 16px;     }     </style>     </head>      <body>     <h2>course:</h2>      <sf:form class="form-horizontal" commandname="course"  method=post>         <fieldset>           <div class="control-group">             <label class="control-label">name</label>             <div class="controls">               <sf:input id="name" name="name" path="name"  class="span5" type="text"/>               <sf:errors path="name" cssclass="error" />               </div>           </div>             <div class="control-group">             <label class="control-label">description</label>             <div class="controls">               <sf:textarea id="category" name="category" path="category"  class="span5" rows="3"></textarea>               <sf:errors path="category" cssclass="error"/>               </div>            </div>    <div class="control-group">             <label class="control-label">description</label>             <div class="controls">               <sf:textarea id="age" name="age" path="age"  class="span5" rows="3"></textarea>               <sf:errors path="age" cssclass="error"/>               </div>            </div>             </div>           <div class="form-actions">             <button id="createbutton" name="createbutton" class="btn btn-primary" type="submit">create</button>            </div>         </fieldset>       </form>      </body>     </html>  

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -