update source code for VRaptor article
This commit is contained in:
parent
6b3ba33119
commit
70d58b370f
@ -1,8 +1,6 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import br.com.caelum.vraptor.Controller;
|
||||
import br.com.caelum.vraptor.Path;
|
||||
import br.com.caelum.vraptor.Result;
|
||||
import br.com.caelum.vraptor.*;
|
||||
import br.com.caelum.vraptor.freemarker.FreemarkerView;
|
||||
import br.com.caelum.vraptor.validator.Validator;
|
||||
import com.baeldung.config.UserInfo;
|
||||
@ -36,60 +34,61 @@ public class AuthController {
|
||||
this.userInfo = userInfo;
|
||||
}
|
||||
|
||||
@Path("/register")
|
||||
@Get("/register")
|
||||
public void registrationForm() {
|
||||
result.use(FreemarkerView.class).withTemplate("auth/register");
|
||||
}
|
||||
|
||||
@Post("/register")
|
||||
public void register(User user, HttpServletRequest request) {
|
||||
|
||||
if(request.getMethod().equals("GET")) {
|
||||
result.use(FreemarkerView.class).withTemplate("auth/register");
|
||||
}
|
||||
else if(request.getMethod().equals("POST")) {
|
||||
validator.validate(user);
|
||||
validator.onErrorRedirectTo(this).register(user, request);
|
||||
if(!user.getPassword().equals(request.getParameter("password_confirmation"))) {
|
||||
result.include("error", "Passwords Do Not Match");
|
||||
result.redirectTo(this).register(null, request);
|
||||
}
|
||||
user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
|
||||
Object resp = userDao.add(user);
|
||||
if(resp != null) {
|
||||
result.include("status", "Registration Successful! Now Login");
|
||||
result.redirectTo(this).login(request);
|
||||
} else {
|
||||
result.include("error", "There was an error during registration");
|
||||
result.redirectTo(this).register(user, request);
|
||||
}
|
||||
validator.validate(user);
|
||||
|
||||
if(validator.hasErrors()) {
|
||||
result.include("errors", validator.getErrors());
|
||||
}
|
||||
|
||||
validator.onErrorRedirectTo(this).registrationForm();
|
||||
|
||||
if(!user.getPassword().equals(request.getParameter("password_confirmation"))) {
|
||||
result.include("error", "Passwords Do Not Match");
|
||||
result.redirectTo(this).registrationForm();
|
||||
}
|
||||
|
||||
user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
|
||||
|
||||
Object resp = userDao.add(user);
|
||||
|
||||
if(resp != null) {
|
||||
result.include("status", "Registration Successful! Now Login");
|
||||
result.redirectTo(this).loginForm();
|
||||
} else {
|
||||
result.notFound();
|
||||
result.include("error", "There was an error during registration");
|
||||
result.redirectTo(this).registrationForm();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/login")
|
||||
@Get("/login")
|
||||
public void loginForm() {
|
||||
result.use(FreemarkerView.class).withTemplate("auth/login");
|
||||
}
|
||||
|
||||
@Post("/login")
|
||||
public void login(HttpServletRequest request) {
|
||||
|
||||
if(request.getMethod().equals("GET")) {
|
||||
result.use(FreemarkerView.class).withTemplate("auth/login");
|
||||
if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) {
|
||||
result.include("error", "Email/Password is Required!");
|
||||
result.redirectTo(AuthController.class).loginForm();
|
||||
}
|
||||
else if(request.getMethod().equals("POST")) {
|
||||
|
||||
if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) {
|
||||
result.include("error", "Email/Password is Required!");
|
||||
result.redirectTo(AuthController.class).login(request);
|
||||
}
|
||||
|
||||
User user = userDao.findByEmail(request.getParameter("user.email"));
|
||||
if (Objects.nonNull(user) && BCrypt.checkpw(request.getParameter("user.password"), user.getPassword())) {
|
||||
userInfo.setUser(user);
|
||||
result.include("status", "Login Successful!");
|
||||
result.redirectTo(IndexController.class).index();
|
||||
} else {
|
||||
result.include("error", "Email/Password Does Not Match!");
|
||||
result.redirectTo(AuthController.class).login(request);
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.include("error", "Invalid Request");
|
||||
result.notFound();
|
||||
User user = userDao.findByEmail(request.getParameter("user.email"));
|
||||
if (Objects.nonNull(user) && BCrypt.checkpw(request.getParameter("user.password"), user.getPassword())) {
|
||||
userInfo.setUser(user);
|
||||
result.include("status", "Login Successful!");
|
||||
result.redirectTo(IndexController.class).index();
|
||||
} else {
|
||||
result.include("error", "Email/Password Does Not Match!");
|
||||
result.redirectTo(AuthController.class).loginForm();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,42 +34,35 @@ public class PostController {
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
|
||||
@Path(value = "/post/add")
|
||||
public void add(Post post, HttpServletRequest request) {
|
||||
|
||||
if(request.getMethod().equals("GET")) {
|
||||
@Get("/post/add")
|
||||
public void addForm() {
|
||||
|
||||
if(Objects.isNull(userInfo.getUser())) {
|
||||
result.include("error", "Please Login to Proceed");
|
||||
result.redirectTo(AuthController.class).login(request);
|
||||
return;
|
||||
result.include("error", "Please Login to Proceed");
|
||||
result.redirectTo(AuthController.class).loginForm();
|
||||
return;
|
||||
}
|
||||
|
||||
result.use(FreemarkerView.class).withTemplate("posts/add");
|
||||
}
|
||||
else if(request.getMethod().equals("POST")) {
|
||||
}
|
||||
|
||||
post.setAuthor(userInfo.getUser());
|
||||
@br.com.caelum.vraptor.Post("/post/add")
|
||||
public void add(Post post) {
|
||||
|
||||
validator.validate(post);
|
||||
if(validator.hasErrors())
|
||||
result.include("error", "Post's Title (min of 5 chars) and Body (min of 10 chars) is Required");
|
||||
validator.onErrorRedirectTo(this).add(post, request);
|
||||
post.setAuthor(userInfo.getUser());
|
||||
validator.validate(post);
|
||||
if(validator.hasErrors())
|
||||
result.include("errors", validator.getErrors());
|
||||
validator.onErrorRedirectTo(this).addForm();
|
||||
|
||||
Object id = postDao.add(post);
|
||||
Object id = postDao.add(post);
|
||||
|
||||
if(Objects.nonNull(id)) {
|
||||
result.include("status", "Post Added Successfully");
|
||||
result.redirectTo(IndexController.class).index();
|
||||
} else {
|
||||
result.include("error", "There was an error creating the post. Try Again");
|
||||
result.redirectTo(this).add(post, request);
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.include("error", "Invalid Request Type");
|
||||
if(Objects.nonNull(id)) {
|
||||
result.include("status", "Post Added Successfully");
|
||||
result.redirectTo(IndexController.class).index();
|
||||
} else {
|
||||
result.include("error", "There was an error creating the post. Try Again");
|
||||
result.redirectTo(this).addForm();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.baeldung.models;
|
||||
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
@ -20,17 +22,7 @@ public class User {
|
||||
private String name;
|
||||
|
||||
@Column(unique = true)
|
||||
@NotNull @Pattern(regexp = "" +
|
||||
"(?:[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")
|
||||
@NotNull @Email
|
||||
private String email;
|
||||
|
||||
@NotNull
|
||||
|
@ -87,6 +87,13 @@
|
||||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||
<p>Register</p>
|
||||
<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">
|
||||
<div class="row control-group">
|
||||
<div class="form-group col-xs-12 floating-label-form-group controls">
|
||||
|
@ -73,7 +73,14 @@
|
||||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||
<p>Compose the Next Viral Message!</p>
|
||||
<h5 style="color:darkred;">${error!""} ${status!""}</h5>
|
||||
<form action="/post/add" method="POST">
|
||||
<#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">
|
||||
<div class="row control-group">
|
||||
<div class="form-group col-xs-12 floating-label-form-group controls">
|
||||
<label>Title</label>
|
||||
@ -95,7 +102,7 @@
|
||||
<button type="submit" class="btn btn-default">Send</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user