Modify registration controller

This commit is contained in:
DOHA 2015-03-09 21:33:28 +02:00
parent abd9f11685
commit 1b9a120e2e
2 changed files with 28 additions and 23 deletions

View File

@ -32,12 +32,10 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller @Controller
public class RegistrationController { public class RegistrationController {
@ -67,34 +65,21 @@ public class RegistrationController {
// Registration // Registration
@RequestMapping(value = "/user/registration", method = RequestMethod.GET)
public String showRegistrationPage(final Model model) {
LOGGER.debug("Rendering registration page.");
final UserDto accountDto = new UserDto();
model.addAttribute("user", accountDto);
return "registration";
}
@RequestMapping(value = "/user/registration", method = RequestMethod.POST) @RequestMapping(value = "/user/registration", method = RequestMethod.POST)
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) { @ResponseBody
public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) {
LOGGER.debug("Registering user account with information: {}", accountDto); LOGGER.debug("Registering user account with information: {}", accountDto);
if (result.hasErrors()) { if (result.hasErrors()) {
return new ModelAndView("registration", "user", accountDto); return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
} }
final User registered = createUserAccount(accountDto); final User registered = createUserAccount(accountDto);
if (registered == null) { if (registered == null) {
result.rejectValue("email", "message.regError"); return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
return new ModelAndView("registration", "user", accountDto);
} }
try {
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl)); eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
} catch (final Exception ex) {
LOGGER.warn("Unable to register user", ex); return new GenericResponse("success");
return new ModelAndView("emailError", "user", accountDto);
}
return new ModelAndView("successRegister", "user", accountDto);
} }
@RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET) @RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET)

View File

@ -1,5 +1,13 @@
package org.baeldung.web.util; package org.baeldung.web.util;
import java.util.List;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class GenericResponse { public class GenericResponse {
private String message; private String message;
private String error; private String error;
@ -15,6 +23,18 @@ public class GenericResponse {
this.error = error; this.error = error;
} }
public GenericResponse(final List<FieldError> fieldErrors, final List<ObjectError> globalErrors) {
super();
final ObjectMapper mapper = new ObjectMapper();
try {
this.message = mapper.writeValueAsString(fieldErrors);
this.error = mapper.writeValueAsString(globalErrors);
} catch (final JsonProcessingException e) {
this.message = "";
this.error = "";
}
}
public String getMessage() { public String getMessage() {
return message; return message;
} }