我试图在我的视图中执行一个简单的验证,我知道jsf在春天mvc做更多很简单让我很头疼... 任何人都可以帮我验证这个问题吗?看看我做错了什么?
我的模特
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="src=@{'/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>
在我的控制器中验证通常更多在视图中没有显示任何内容我该如何解决?
我在stackoverflow上已经有一段时间了,大多数都找不到解决方案...
答案 0 :(得分:0)
您的视图未显示任何内容,因为您未在模板中包含错误消息。以下是一个例子。
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Name Error</td>