Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.pw.ism.controllers; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; 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.servlet.ModelAndView; import com.pw.ism.message.Message; import com.pw.ism.message.MessageRepository; import com.pw.ism.message.PageDeprecated; import com.pw.ism.models.MailMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; /** * * @author Narsel */ @Controller @RequestMapping("/message") public class MessageController { private final static Logger LOGGER = LoggerFactory.getLogger(MessageController.class); private MessageRepository messageRepository; @Autowired public MessageController(MessageRepository messageRepository) { this.messageRepository = messageRepository; } public void setMessageRepository(MessageRepository mr) { this.messageRepository = mr; } @RequestMapping(params = { "p", "s" }) public ModelAndView listWithPage(@RequestParam(required = false, value = "p") int pageNum, @RequestParam(required = false, value = "s") int pageSize) { if (pageNum < 0 || pageSize < 0 || (pageNum * pageSize) > this.messageRepository.count()) { return new ModelAndView("error"); } else { Pageable pageable = new PageRequest(pageNum, pageSize, Sort.Direction.DESC, "id"); Page<Message> messages = this.messageRepository.findAll(pageable); ModelAndView mvc = new ModelAndView("messages/list"); mvc.addObject("messages", messages); return mvc; } } @RequestMapping public ModelAndView list() { Pageable pageable = new PageRequest(0, 30, Sort.Direction.DESC, "id"); Iterable<Message> messages = this.messageRepository.findAll(pageable); ModelAndView mvc = new ModelAndView("messages/list"); mvc.addObject("messages", messages); return mvc; } @RequestMapping(value = "{id}", method = RequestMethod.GET) public ModelAndView view(@PathVariable("id") Message message) { return new ModelAndView("messages/view", "message", message); } @RequestMapping(params = "form", method = RequestMethod.GET) public String createForm(@ModelAttribute Message message) { return "messages/form"; } // @RequestMapping(params = "form", method = RequestMethod.POST) // public ModelAndView create(@Valid Message message, BindingResult result, // RedirectAttributes redirect) { // if (result.hasErrors()) { // return new ModelAndView("messages/form", "formErrors", result.getAllErrors()); // } // message = this.messageRepository.save(message); // redirect.addFlashAttribute("globalMessage", "Successfully created a new message"); // return new ModelAndView("redirect:/message/{message.id}", "message.id", message.getId()); // } @RequestMapping(value = "sendmail", method = RequestMethod.POST, headers = { "Content-type=application/json" }) public ResponseEntity<String> sendMail(@Valid @RequestBody MailMessage mail, BindingResult result) { if (result.hasErrors()) { for (ObjectError error : result.getAllErrors()) { LOGGER.info("not valid: " + error.toString()); } return new ResponseEntity<>("NOK!", HttpStatus.BAD_REQUEST); } else { return new ResponseEntity<>("OK!", HttpStatus.OK); } } }