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 ua.com.rocketlv.spb; import java.io.UnsupportedEncodingException; import java.util.Base64; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; 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 ua.com.rocketlv.spb.model.Users; /** * * @author rocketlv */ @Controller @RequestMapping(value = "/find/") public class UserController { @Autowired RequestBean reqBean; public void setReqBean(RequestBean reqBean) { this.reqBean = reqBean; } @Autowired Store store; public void setStore(Store store) { this.store = store; } @Autowired UsersRepository repository; public void setRepository(UsersRepository repository) { this.repository = repository; } @Autowired MessageSource messages; public void setMessages(MessageSource messages) { this.messages = messages; } @RequestMapping(value = "/list/{name}") public String find(@PathVariable String name, Model model) { Locale locale = LocaleContextHolder.getLocale(); String mess = messages.getMessage("wellcome", null, locale); System.out.println(mess); Iterable<Users> lst = null; if (name.equals("all")) { lst = repository.findAll(); } else { lst = repository.findByNameuIgnoreCaseContaining(name); } try { String encoded = Base64.getEncoder() .encodeToString("Wellcome on Andrey secured web site !".getBytes("utf-8")); model.addAttribute("encoded", encoded); } catch (UnsupportedEncodingException ex) { Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex); } model.addAttribute("data", lst); model.addAttribute("store", store); return "search"; } @RequestMapping(value = "/add/", method = RequestMethod.GET) public String adduserget(Model model) { Locale locale = LocaleContextHolder.getLocale(); String mess = messages.getMessage("find.add", null, locale); model.addAttribute("users", new Users("-", "-", "0000")); return "searchadd"; } @RequestMapping(value = "/edit/{idu}", method = RequestMethod.GET) public String adduserget(@PathVariable Long idu, Model model) { Locale locale = LocaleContextHolder.getLocale(); String mess = messages.getMessage("find.edit", null, locale); Users users = null; if (idu != null) { users = repository.findOne(idu); } else { users = new Users("-", "-", "0000"); } reqBean.setAction(" request - " + users.getNameu()); model.addAttribute("users", users); model.addAttribute("reqBean", reqBean); return "searchadd"; } @RequestMapping(value = "/add/", method = RequestMethod.POST) public String adduserpost(@Valid Users users, BindingResult bindingResult) { if (bindingResult.hasErrors()) { System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); return "searchadd"; } System.out.println(users.getNameu()); repository.save(users); return "redirect:/find/list/all"; } }