update source code for VRaptor article
This commit is contained in:
parent
6b3ba33119
commit
70d58b370f
@ -1,8 +1,6 @@
|
|||||||
package com.baeldung.controllers;
|
package com.baeldung.controllers;
|
||||||
|
|
||||||
import br.com.caelum.vraptor.Controller;
|
import br.com.caelum.vraptor.*;
|
||||||
import br.com.caelum.vraptor.Path;
|
|
||||||
import br.com.caelum.vraptor.Result;
|
|
||||||
import br.com.caelum.vraptor.freemarker.FreemarkerView;
|
import br.com.caelum.vraptor.freemarker.FreemarkerView;
|
||||||
import br.com.caelum.vraptor.validator.Validator;
|
import br.com.caelum.vraptor.validator.Validator;
|
||||||
import com.baeldung.config.UserInfo;
|
import com.baeldung.config.UserInfo;
|
||||||
@ -36,45 +34,51 @@ public class AuthController {
|
|||||||
this.userInfo = userInfo;
|
this.userInfo = userInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Path("/register")
|
@Get("/register")
|
||||||
public void register(User user, HttpServletRequest request) {
|
public void registrationForm() {
|
||||||
|
|
||||||
if(request.getMethod().equals("GET")) {
|
|
||||||
result.use(FreemarkerView.class).withTemplate("auth/register");
|
result.use(FreemarkerView.class).withTemplate("auth/register");
|
||||||
}
|
}
|
||||||
else if(request.getMethod().equals("POST")) {
|
|
||||||
|
@Post("/register")
|
||||||
|
public void register(User user, HttpServletRequest request) {
|
||||||
|
|
||||||
validator.validate(user);
|
validator.validate(user);
|
||||||
validator.onErrorRedirectTo(this).register(user, request);
|
|
||||||
|
if(validator.hasErrors()) {
|
||||||
|
result.include("errors", validator.getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
validator.onErrorRedirectTo(this).registrationForm();
|
||||||
|
|
||||||
if(!user.getPassword().equals(request.getParameter("password_confirmation"))) {
|
if(!user.getPassword().equals(request.getParameter("password_confirmation"))) {
|
||||||
result.include("error", "Passwords Do Not Match");
|
result.include("error", "Passwords Do Not Match");
|
||||||
result.redirectTo(this).register(null, request);
|
result.redirectTo(this).registrationForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
|
user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
|
||||||
|
|
||||||
Object resp = userDao.add(user);
|
Object resp = userDao.add(user);
|
||||||
|
|
||||||
if(resp != null) {
|
if(resp != null) {
|
||||||
result.include("status", "Registration Successful! Now Login");
|
result.include("status", "Registration Successful! Now Login");
|
||||||
result.redirectTo(this).login(request);
|
result.redirectTo(this).loginForm();
|
||||||
} else {
|
} else {
|
||||||
result.include("error", "There was an error during registration");
|
result.include("error", "There was an error during registration");
|
||||||
result.redirectTo(this).register(user, request);
|
result.redirectTo(this).registrationForm();
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
result.notFound();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Path("/login")
|
@Get("/login")
|
||||||
public void login(HttpServletRequest request) {
|
public void loginForm() {
|
||||||
|
|
||||||
if(request.getMethod().equals("GET")) {
|
|
||||||
result.use(FreemarkerView.class).withTemplate("auth/login");
|
result.use(FreemarkerView.class).withTemplate("auth/login");
|
||||||
}
|
}
|
||||||
else if(request.getMethod().equals("POST")) {
|
|
||||||
|
@Post("/login")
|
||||||
|
public void login(HttpServletRequest request) {
|
||||||
|
|
||||||
if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) {
|
if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) {
|
||||||
result.include("error", "Email/Password is Required!");
|
result.include("error", "Email/Password is Required!");
|
||||||
result.redirectTo(AuthController.class).login(request);
|
result.redirectTo(AuthController.class).loginForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = userDao.findByEmail(request.getParameter("user.email"));
|
User user = userDao.findByEmail(request.getParameter("user.email"));
|
||||||
@ -84,12 +88,7 @@ public class AuthController {
|
|||||||
result.redirectTo(IndexController.class).index();
|
result.redirectTo(IndexController.class).index();
|
||||||
} else {
|
} else {
|
||||||
result.include("error", "Email/Password Does Not Match!");
|
result.include("error", "Email/Password Does Not Match!");
|
||||||
result.redirectTo(AuthController.class).login(request);
|
result.redirectTo(AuthController.class).loginForm();
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.include("error", "Invalid Request");
|
|
||||||
result.notFound();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,28 +34,26 @@ public class PostController {
|
|||||||
this.validator = validator;
|
this.validator = validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get("/post/add")
|
||||||
@Path(value = "/post/add")
|
public void addForm() {
|
||||||
public void add(Post post, HttpServletRequest request) {
|
|
||||||
|
|
||||||
if(request.getMethod().equals("GET")) {
|
|
||||||
|
|
||||||
if(Objects.isNull(userInfo.getUser())) {
|
if(Objects.isNull(userInfo.getUser())) {
|
||||||
result.include("error", "Please Login to Proceed");
|
result.include("error", "Please Login to Proceed");
|
||||||
result.redirectTo(AuthController.class).login(request);
|
result.redirectTo(AuthController.class).loginForm();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.use(FreemarkerView.class).withTemplate("posts/add");
|
result.use(FreemarkerView.class).withTemplate("posts/add");
|
||||||
}
|
}
|
||||||
else if(request.getMethod().equals("POST")) {
|
|
||||||
|
@br.com.caelum.vraptor.Post("/post/add")
|
||||||
|
public void add(Post post) {
|
||||||
|
|
||||||
post.setAuthor(userInfo.getUser());
|
post.setAuthor(userInfo.getUser());
|
||||||
|
|
||||||
validator.validate(post);
|
validator.validate(post);
|
||||||
if(validator.hasErrors())
|
if(validator.hasErrors())
|
||||||
result.include("error", "Post's Title (min of 5 chars) and Body (min of 10 chars) is Required");
|
result.include("errors", validator.getErrors());
|
||||||
validator.onErrorRedirectTo(this).add(post, request);
|
validator.onErrorRedirectTo(this).addForm();
|
||||||
|
|
||||||
Object id = postDao.add(post);
|
Object id = postDao.add(post);
|
||||||
|
|
||||||
@ -64,12 +62,7 @@ public class PostController {
|
|||||||
result.redirectTo(IndexController.class).index();
|
result.redirectTo(IndexController.class).index();
|
||||||
} else {
|
} else {
|
||||||
result.include("error", "There was an error creating the post. Try Again");
|
result.include("error", "There was an error creating the post. Try Again");
|
||||||
result.redirectTo(this).add(post, request);
|
result.redirectTo(this).addForm();
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.include("error", "Invalid Request Type");
|
|
||||||
result.redirectTo(IndexController.class).index();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.models;
|
package com.baeldung.models;
|
||||||
|
|
||||||
|
import org.hibernate.validator.constraints.Email;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
@ -20,17 +22,7 @@ public class User {
|
|||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(unique = true)
|
@Column(unique = true)
|
||||||
@NotNull @Pattern(regexp = "" +
|
@NotNull @Email
|
||||||
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+" +
|
|
||||||
"(?:\\\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"" +
|
|
||||||
"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\" +
|
|
||||||
"[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@" +
|
|
||||||
"(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9]" +
|
|
||||||
"(?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" +
|
|
||||||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]" +
|
|
||||||
":(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\" +
|
|
||||||
"[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])",
|
|
||||||
message = "Invalid Email Address")
|
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -87,6 +87,13 @@
|
|||||||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||||
<p>Register</p>
|
<p>Register</p>
|
||||||
<h5 style="color:darkred;">${error!""} ${status!""}</h5>
|
<h5 style="color:darkred;">${error!""} ${status!""}</h5>
|
||||||
|
<#if errors??>
|
||||||
|
<#list errors as error>
|
||||||
|
<h5 style="color:darkred">${error.category?upper_case}: ${error.message}</h5>
|
||||||
|
<#else>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
</#if>
|
||||||
<form name="sentM" id="contactFo" action="/register" method="POST">
|
<form name="sentM" id="contactFo" action="/register" method="POST">
|
||||||
<div class="row control-group">
|
<div class="row control-group">
|
||||||
<div class="form-group col-xs-12 floating-label-form-group controls">
|
<div class="form-group col-xs-12 floating-label-form-group controls">
|
||||||
|
@ -73,6 +73,13 @@
|
|||||||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||||
<p>Compose the Next Viral Message!</p>
|
<p>Compose the Next Viral Message!</p>
|
||||||
<h5 style="color:darkred;">${error!""} ${status!""}</h5>
|
<h5 style="color:darkred;">${error!""} ${status!""}</h5>
|
||||||
|
<#if errors??>
|
||||||
|
<#list errors as error>
|
||||||
|
<h5 style="color:darkred">${error.category?upper_case}: ${error.message}</h5>
|
||||||
|
<#else>
|
||||||
|
</#list>
|
||||||
|
<#else>
|
||||||
|
</#if>
|
||||||
<form action="/post/add" method="POST">
|
<form action="/post/add" method="POST">
|
||||||
<div class="row control-group">
|
<div class="row control-group">
|
||||||
<div class="form-group col-xs-12 floating-label-form-group controls">
|
<div class="form-group col-xs-12 floating-label-form-group controls">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user