1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| package iset.master.spring.web;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import iset.master.spring.model.Article;
import iset.master.spring.model.Client;
import iset.master.spring.model.Famille;
import iset.master.spring.model.Tva;
import iset.master.spring.repository.FamilleRepository;
import iset.master.spring.repository.ArticleRepository;
import iset.master.spring.repository.TvaRepository;
@Controller
public class ArticleController {
@Autowired
ArticleRepository articleRepository;
@Autowired
FamilleRepository familleRepository;
@Autowired
TvaRepository tvaRepository;
@RequestMapping(value="/listearticle")
public String articles (Model model,
@RequestParam (name="page", defaultValue="0") int p,
@RequestParam (name="size", defaultValue="5") int s,
@RequestParam (name="motCle", defaultValue="") String mc)
{
Page<Article> pageArticles = articleRepository.chercherParMotCle("%"+mc+"%", new PageRequest(p, s));
model.addAttribute("listeArticles", pageArticles.getContent());
int[] pages = new int[pageArticles.getTotalPages()];
model.addAttribute("pages", pages);
model.addAttribute("size", s);
model.addAttribute("pageCourante", p);
model.addAttribute("motCle", mc);
return "article";
}
@RequestMapping(value="/form",method=RequestMethod.GET)
public String formArticle (Model model)
{
model.addAttribute("article", new Article());
model.addAttribute("famille", new Famille());
model.addAttribute("tva", new Tva());
model.addAttribute("listefamilles", familleRepository.findAll());
model.addAttribute("listetva", tvaRepository.findAll());
// model.addAttribute("article", new Article());
return "formArticle";
}
@RequestMapping(value="/save",method=RequestMethod.POST)
public String save (Model model, @Valid Article article , BindingResult bindingResult)
{
if (bindingResult.hasErrors())
return "formArticle";
articleRepository.save(article);
return "confirmation";
}
@RequestMapping(value="/editarticle",method=RequestMethod.GET)
public String editarticle (Model model, Long id)
{
Article a =(Article) articleRepository.findById(id).orElse(null);
model.addAttribute("article", a);
return "editArticle";
}
@RequestMapping(value="/deletearticle",method=RequestMethod.GET)
public String deletearticle (Long id, int page, int size, String motCle)
{
articleRepository.deleteById(id);
return "redirect:/listearticle?page="+page+"&size="+size+"&motCle="+motCle;
}
} |
Partager