2015-09-28 75 views
1

我試圖在我的視圖中執行一個簡單的驗證,我知道jsf它是非常簡單的做更多在春天mvc給我頭痛... 任何人都可以幫助我通過此驗證?看看我做錯了什麼?如何使用thymeleaf和bean驗證進行驗證?

我的模型

package br.com.nextinfo.multimedia.web.model; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.OneToOne; 
import javax.persistence.SequenceGenerator; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 

import org.hibernate.validator.constraints.NotBlank; 

@Table 
@Entity(name="slideshow") 
public class SlideShow { 

private Long condigo; 

private String titulo; 

private String subTitulo; 

private ImagemSlider imagemSlider; 


@Id 
@GeneratedValue(generator="codigo",strategy=GenerationType.AUTO) 
@SequenceGenerator(name = "codigo", sequenceName = "codigo_slideshow")  
@Column(name = "CODIGO") 
public Long getCondigo() { 
    return condigo; 
} 

@NotNull 
@NotBlank 
@Column(name = "TITULO" ,nullable = false) 
public String getTitulo() { 
    return titulo; 
} 
@OneToOne(cascade = CascadeType.ALL) 
@JoinColumn(name ="CODIGO_IMAGEM") 
public ImagemSlider getImagemSlider() { 
    return imagemSlider; 
} 

@NotNull 
@NotBlank 
@Column(name = "SUBTITULO" ,nullable = false) 
public String getSubTitulo() { 
    return subTitulo; 
} 

public void setTitulo(String titulo) { 
    this.titulo = titulo; 
} 

public void setSubTitulo(String subTitulo) { 
    this.subTitulo = subTitulo; 
} 


public void setImagemSlider(ImagemSlider imagemSlider) { 
    this.imagemSlider = imagemSlider; 
} 

public void setCondigo(Long condigo) { 
    this.condigo = condigo; 
} 


} 

我控制器

package br.com.nextinfo.multimedia.web.controller; 


import java.io.IOException; 
import java.util.List; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.validation.Valid; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.servlet.mvc.support.RedirectAttributes; 

import br.com.nextinfo.multimedia.web.model.ImagemSlider; 
import br.com.nextinfo.multimedia.web.model.SlideShow; 
import br.com.nextinfo.multimedia.web.services.AbstractFactoryService; 
import br.com.nextinfo.multimedia.web.services.CreateSlideShowService; 
import br.com.nextinfo.multimedia.web.services.ImagemServiceDatabase; 

@Controller 
@RequestMapping("/arquivosbanco/") 
public class FilesDatabaseController implements AbstractControllerApp ,PadraoCrudMetodos<ImagemSlider> { 

@Autowired 
private ImagemServiceDatabase imagemService; 

@Autowired 
private CreateSlideShowService slideShowService; 

@Override 
public String getRequisicaoPadrao(Model model) { 
    List<ImagemSlider> lista = this.getAbstractService().realizaPaginacao("0", "10" ,null).getContent(); 
    model.addAttribute("listaObjetoPageStart", lista); 
    model.addAttribute("img", new ImagemSlider()); 
    return this.getUrlInicial(); 
} 

@Override 
public String getUrlInicial() { 
    return "arquivos/database/listarquivosbancodedados"; 
} 

@ResponseBody 
@RequestMapping(value = "/img", method = RequestMethod.GET) 
public void showImage(@RequestParam("id") Long id, HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException { 
    response.setContentType("image/jpeg, image/jpg, image/png, image/gif"); 
    ImagemSlider imgdatabase = imagemService.getAbstractService().getBean(id); 
    if (imgdatabase!=null) { 
     response.getOutputStream().write(imgdatabase.getImage()); 
     response.getOutputStream().close(); 
    } 
} 

@Override 
public AbstractFactoryService<ImagemSlider> getAbstractService() { 
    return imagemService; 
} 
@RequestMapping(value = "/createslideshow/{imagemid}" ,method=RequestMethod.GET) 
public String createslider(@PathVariable("imagemid") Long image,Model model){ 
    final ImagemSlider img = this.imagemService.getBean(image); 
    model.addAttribute("imgslider", img); 
    model.addAttribute("slideshow", new SlideShow()); 

    return "arquivos/createdisplay"; 
} 
@RequestMapping(value = "/save" ,method=RequestMethod.POST) 
public String saveSlideShowr(@RequestParam Long imagemid ,@Valid final SlideShow slideshow,final BindingResult result ,RedirectAttributes redirectAttrs){ 

    ImagemSlider img = this.imagemService.getBean(imagemid); 
    if (result.hasErrors()) { 
     redirectAttrs.addFlashAttribute("org.springframework.validation.BindingResult.strategy", result); 
     redirectAttrs.addFlashAttribute("slideshow", slideshow); 
     return "redirect:/arquivosbanco/createslideshow/"+imagemid; 
    }else{ 
     this.slideShowService.salva(slideshow); 
     if (img != null) { 
      slideshow.setImagemSlider(img); 
      this.slideShowService.salva(slideshow); 
     } 
    } 
    return this.getUrlInicial(); 
    } 
    } 

    <html xmlns:th="http://www.thymeleaf.org" 
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
    layout:decorator="/templates/layouts/principal"> 
    <head> 
    <title>Criacao slideshow</title> 
    </head> 
<body> 

<div class="container-fluid" layout:fragment="corpopagina"> 
    <h3 class="page-header text-center">Criacao slide show</h3> 

<form id="registration-form" class="form-horizontal" th:object="${slideShow}" th:action="@{/arquivosbanco/save/?imagemid=}+${imgslider.codigo}" method="post"> 

<!-- Print all errors here!--> 
<div class="col-md-4 col-xs-12"> 
    <div class="thumbnail"> 
     <img class="img-responsive" th:attr="[email protected]{'/upload/img?codigo='+${imgslider.codigo}}" width="400" height="400" /> 
     <span class="text-center"></span> 
     </div> 
     <div class="caption"> 
      <h5>Codigo <span th:text="${imgslider.codigo}" class="badge" style="margin-left:20px">5</span></h5> 
      <h5>Nome <span th:text="${imgslider.nome}" class="badge" style="margin-left:20px">5</span></h5> 
     </div> 
    </div> 
    <div class="col-md-8 col-xs-12"> 
      <div class="form-group"> 
       <label class="col-xs-3 control-label">Titulo principal</label> 
       <div class="col-md-8"> 
        <input type="text" class="form-control" name="titulo" /> 
       </div> 
      </div> 
      <div class="form-group"> 
       <label class="col-xs-3 control-label">Sub titulo</label> 
       <div class="col-md-8"> 
        <input type="text" class="form-control input-xs" name="subTitulo" /> 
       </div> 
      </div> 
      <div class="form-actions"> 
       <label class="col-md-3 control-label"></label> 
       <div class="col-md-7"> 
       <button type="submit" class="btn btn-success btn-large">Salvar</button> 
       </div> 
       <div class="col-md-1"></div> 
      </div> 
     </div> 
    </form> 
</div> 
</body> 

在我的控制器通常驗證更視圖不顯示任何東西我該怎麼解決?

我計算器上相當長的一段時間,最不能找到解決辦法......

回答

0

你的觀點並沒有顯示你有沒有包含在模板中的錯誤消息的任何東西。下面是一個例子。

<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Name Error</td> 
+0

檢查下面的鏈接,我試圖複製的是已經在文章中工作示例http://spring.io/guides/gs/validating-form-input/ – ArunM

+0

,更不行! 如果我把你提到的一個昂貴的錯誤,應該很容易做到,因爲jsf和非常簡單。如果你把它放在這裏

名稱錯誤出現錯誤 錯誤:Bean類的無效屬性'slideshow'[br.com.nextinfo.multimedia.web.model.SlideShow]:Bean屬性'幻燈片「不可讀或具有無效的getter方法:getter的返回類型是否與setter的參數類型匹配? –