验证和Freemarker

时间:2015-06-01 20:44:26

标签: validation message freemarker

我们必须在页面上进行验证,如果是空字段则显示消息。 它是我的班级,控制器的方法和消息的设置。 我无法访问Freemarker显示错误消息页面。

public class CreateCourseDTO {
@NotEmpty  
private String name;
@NotEmpty  
private String category;
@NotEmpty  
private String description;
@NotEmpty  
private String links;

public CreateCourseDTO() {
}


NotEmpty.createCourseDTO.name = Name is required!
NotEmpty.createCourseDTO.category = Category is required!
NotEmpty.createCourseDTO.description = Description is required!
NotEmpty.createCourseDTO.links = Links is required!


<bean class="org.springframework.context.support.ResourceBundleMessageSource"
    id="messageSource">
    <property name="basename" value="messages" />
</bean>


 @RequestMapping(value = "/create", method = RequestMethod.POST, params = {
        "name", "category", "description", "links" })
public String createCoursePost(Model model, HttpSession session,
        HttpServletRequest request, @Valid CreateCourseDTO createCourseDTO,
        BindingResult result) {
    model.addAttribute("eMail", session.getAttribute("eMail"));

    String title = request.getParameter("name");
    String description = request.getParameter("description");
    String links = request.getParameter("links");
    String category = request.getParameter("category");

    if (result.hasErrors() ) {

如何修复freemarker页面?

<#import "/spring.ftl" as spring />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
<style>
.error {
color: #ff0000;
}

.errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
</head>
 <body>
  <div class="container">
  <header>
    <h1>
      Create Course
      <div class="logout">
        <span id="currentUserLogin">
          ${eMail}
        </span>
        <a href="logout.html">
          <i class="icon-off"></i>
        </a>
      </div>
    </h1>
  </header>



  <form class="form-horizontal" commandName="createCourseDTO"  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">Category</label>
        <div class="controls">
          <select id="category" name="category" class="span5">
            <option></option>
             <#list listCategories as category>
             <option>${category.category}</option>
             </#list>
         </select>
         <@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 class="control-group">
        <label class="control-label">Links</label>
        <div class="controls">
          <textarea id="links" name="links" class="span5" rows="3"></textarea>
          <@spring.showErrors "<br>"  />
        </div>
      </div>
      <div class="form-actions">
        <button id="createButton" name="createButton" class="btn btn-primary" type="submit">Create</button>

      </div>
    </fieldset>
  </form>

    <a class="btn" href="courses.html">Cancel</a>
</div>

1 个答案:

答案 0 :(得分:0)

直到发生这种情况。

<#import "/spring.ftl" as spring />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
</head>
 <body>
 <div class="container">
  <header>
    <h1>
      Create Course
      <div class="logout">
        <span id="currentUserLogin">
          ${eMail}
        </span>
        <a href="logout.html">
          <i class="icon-off"></i>
        </a>
      </div>
    </h1>
  </header>

  <form  action="create" method="POST"/>
   <fieldset>
    <@spring.bind "сreateCourseDTO" />
    <@spring.showErrors '*', 'errors' />

      <div class="control-group">   
      <label class="control-label">Name</label>
        <div class="controls">  

          <@spring.formInput "createCourseDTO.name" ""/>
        <@spring.showErrors  '<br>',"error" />

        </div>
      </div>
       <div class="control-group">

      <label class="control-label">Category</label>
        <div class="controls">
           <@spring.formSingleSelect "createCourseDTO.category" categoryList "" />
            <option></option>
             <#list listCategories as category>
             <option>${category.category}</option>
             </#list>
         </select>
            <@spring.showErrors "createCourseDTO.category","error" />
        </div>
      </div>

      <div class="control-group">
      <label class="control-label">Description</label>
        <div class="controls">  
            <@spring.formInput "createCourseDTO.description"/>
            <@spring.showErrors "createCourseDTO.description","error" />
        </div>
      </div>
     <div class="control-group">
          <label class="control-label">Links</label>
        <div class="controls">
         <@spring.formInput "createCourseDTO.links"/>
        <@spring.showErrors "createCourseDTO.links","error" />
        </div>
      </div>
      <div class="form-actions">
        <button id="createButton" name="createButton" class="btn btn-primary" type="submit">Create</button>

      </div>
    </fieldset>
  </form>

    <a class="btn" href="courses.html">Cancel</a>
    </div>
    </body>
   </html>

引发错误

==&GT;赋值:status = springMacroRequestContext.getBindStatus(path)[在第120行,spring.ftl第9列] 在user-directive绑定[在spring.ftl的第159行,第5列] 在用户指令spring.formInput [on line 33,pages / create.ftl中的column 15]

在线&lt; @ spring.formInput&#34; createCourseDTO.name&#34; &#34;&#34; /&GT;

我已经修好了bin

<bean id="viewResolver"  class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
    p:cache="true" p:prefix="/pages/" p:suffix=".ftl" p:exposeSpringMacroHelpers="true"/>