Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created July 16, 2014 05:07
Show Gist options
  • Save rokon12/b71674c5f80afc441898 to your computer and use it in GitHub Desktop.
Save rokon12/b71674c5f80afc441898 to your computer and use it in GitHub Desktop.
package org.jugbd.mnet.web.controller;
import org.jugbd.mnet.domain.User;
import org.jugbd.mnet.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.mvc.support.RedirectAttributes;
import javax.validation.Valid;
/**
* Created by Bazlur Rahman Rokon on 7/15/14.
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@ModelAttribute("user")
private User getUser() {
return new User();
}
@RequestMapping(params = "form", method = RequestMethod.GET)
public String createForm() {
log.debug("createForm()");
return "user/create";
}
@RequestMapping(value = "create", method = RequestMethod.POST)
public String submitForm(@Valid @ModelAttribute("user") User user, BindingResult result, RedirectAttributes redirectAttrs) {
log.debug("submitForm()");
if (result.hasErrors()) {
return "user/create";
}
if (!user.getPassword().equals(user.getPasswordConfirmed())) {
result.rejectValue("passwordConfirmed","");
}
User userFound = userService.findByUserName(user.getUsername());
if (userFound != null) {
result.rejectValue("password","");
}
userService.create(user);
return "redirect:/user/details/" + user.getId().toString();
}
public String list() {
log.debug("list()");
return "user/list";
}
@RequestMapping(value = "details/{id}", method = RequestMethod.GET)
public String details(@PathVariable("id") Long id) {
return "user/details";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment