jsr303和Freemarker,有效示例

时间:2015-06-02 19:31:23

标签: spring validation freemarker

请帮助为Freemarker页面进行jsr 303验证。 有课程,控制器,文件messages.properties,创建bean id =“messageSource”> 我需要在Freemarker中创建一个页面来创建一个新课程。填充空字段或不正确的范围输出错误消息。

public class Course {
@NotEmpty 
private String name;
@NotEmpty  
private String category;
@Range(min = 20, max = 25)  
int age;

//get and set
}


NotEmpty.course.name = Name is required!
NotEmpty.course.category= Categoryis required!
Range.course.age = Age value must be between 20 and 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 your messages.properties -->
<bean   class="org.springframework.context.support.ResourceBundleMessageSource"
    id="messageSource">
    <property name="basename" value="messages" />
</bean>

如何制作此页面?

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

1 个答案:

答案 0 :(得分:0)

You should use Spring Form to generate the validations
For this, you have to add spring tag library path in your html

Go like 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>