2015-06-01 84 views
0

我們必須在頁面上進行驗證,在空字段顯示消息的情況下。 這是我的課,控制器的方法和消息的設置。 我無法登上頁面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> 

回答

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> 

引發錯誤

==>賦值:狀態= springMacroRequestContext.getBindStatus(路徑)[在線120上,在spring.ftl柱9] 在用戶指令綁定[上線159,在spring.ftl] 第5欄用戶指令spring.formInput [第33行,在網頁柱15/create.ftl]

上線<

@ spring.formInput 「createCourseDTO.name」 「」/>

我有修復程序d bin

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