Dropwizard 从 POST 方法更新视图
Dropwizard update View from POST method
我正在尝试验证我的 POST 注册新用户的方法中的表单。如果出现错误,例如已使用的用户名,我希望我的视图(从我的 GET 方法中检索到)更新错误。我正在使用 Dropwizard Views 和 Freemarker。到目前为止我的代码:
public class SignUpView extends View {
List<String> errors;
public SignUpView() {
super("signup.ftl");
errors = new ArrayList<>();
}
public SignUpView(List<String> errors) {
super("signup.ftl");
this.errors = errors;
}
}
signup.ftl:
<#-- @ftlvariable name="" type="com.tm.resources.views.SignUpView" -->
<#include "include/head.html">
<#include "include/header.html">
<br><br><br>
<form action="/signup" method="post">
<div class="container">
<h1>Sign Up</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<#if errors?has_content>
<ul class="errorMessages">
<#list errors as error>
<li>${error}</li>
</#list>
</ul>
</#if>
<label><b>Full name</b></label>
<input type="text" placeholder="full name" name="full_name" required>
<label><b>Email</b></label>
<input type="email" placeholder="Enter Email" name="email" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<label><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<label>
<input type="checkbox" checked="checked" style="margin-bottom:15px"> Remember me
</label>
<p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>
<div class="clearfix">
<button type="button" class="cancelbtn btn-form">Cancel</button>
<button type="submit" class="signupbtn btn-form">Sign Up</button>
</div>
</div>
</form>
<#include "include/footer.html">
还有我的资源class:
@Path("/signup")
@Produces(MediaType.TEXT_HTML)
public class SignUpResource {
// UserService
private UserService userService;
public SignUpResource(UserService userService) {
this.userService = userService;
}
@GET
public SignUpView getView() {
return new SignUpView();
}
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Timed
@UnitOfWork
public Response registerUserInfo(@FormParam("full_name") String name,
@FormParam("email") String email,
@FormParam("psw") String pwd,
@FormParam("psw-repeat") String pwdRepeat){
String responseString = "Successfully added user details, name: "+
name+" and pwd: "+pwd;
try {
userService.addUser(name,email, pwd);
} catch (ShowAbleException e) {
// if adding triggered an error it must be displayed on the page
List<String> errors = new ArrayList<String>();
errors.add(e.getMessage());
// HOW to go back to GET method with errors?? Or just display them?
}
try {
//return Response.created(new URI("/")).entity(response).build();
return Response.seeOther(new URI("/")).build(); // redirect back to homepage...
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
}
我看到 this 页面提供了一些指导,但我未能复制它,因为源未完全提供并且它不使用 Dropwizard 的视图功能。我还考虑过向我的 GET 方法添加参数并从 POST 方法重定向,但这对我来说似乎有点矫枉过正?
任何帮助将不胜感激。
我设法通过遇到 this thread 解决了这个问题,这表明可以发送一个新的 View
作为响应。因此,如果遇到错误,则会在 Response
中创建一个新的 View
并更新错误。代码如下:
try {
userService.addUser(name,email, pwd);
} catch (ShowAbleException e) {
// if adding triggered an error it must be displayed on the page
List<String> errors = new ArrayList<String>();
errors.add(e.getMessage());
return Response.ok(new SignUpView(errors)).build();
}
try {
//return Response.created(new URI("/")).entity(response).build();
return Response.seeOther(new URI("/")).build(); // redirect back to homepage...
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
我还发现我的 SignUpView
class 需要一个 getErrors 方法,以便 Freemarker 文件可以访问错误列表。所以我补充说:
public List<String> getErrors() {
return errors;
}
我正在尝试验证我的 POST 注册新用户的方法中的表单。如果出现错误,例如已使用的用户名,我希望我的视图(从我的 GET 方法中检索到)更新错误。我正在使用 Dropwizard Views 和 Freemarker。到目前为止我的代码:
public class SignUpView extends View {
List<String> errors;
public SignUpView() {
super("signup.ftl");
errors = new ArrayList<>();
}
public SignUpView(List<String> errors) {
super("signup.ftl");
this.errors = errors;
}
}
signup.ftl:
<#-- @ftlvariable name="" type="com.tm.resources.views.SignUpView" -->
<#include "include/head.html">
<#include "include/header.html">
<br><br><br>
<form action="/signup" method="post">
<div class="container">
<h1>Sign Up</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<#if errors?has_content>
<ul class="errorMessages">
<#list errors as error>
<li>${error}</li>
</#list>
</ul>
</#if>
<label><b>Full name</b></label>
<input type="text" placeholder="full name" name="full_name" required>
<label><b>Email</b></label>
<input type="email" placeholder="Enter Email" name="email" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<label><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<label>
<input type="checkbox" checked="checked" style="margin-bottom:15px"> Remember me
</label>
<p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>
<div class="clearfix">
<button type="button" class="cancelbtn btn-form">Cancel</button>
<button type="submit" class="signupbtn btn-form">Sign Up</button>
</div>
</div>
</form>
<#include "include/footer.html">
还有我的资源class:
@Path("/signup")
@Produces(MediaType.TEXT_HTML)
public class SignUpResource {
// UserService
private UserService userService;
public SignUpResource(UserService userService) {
this.userService = userService;
}
@GET
public SignUpView getView() {
return new SignUpView();
}
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Timed
@UnitOfWork
public Response registerUserInfo(@FormParam("full_name") String name,
@FormParam("email") String email,
@FormParam("psw") String pwd,
@FormParam("psw-repeat") String pwdRepeat){
String responseString = "Successfully added user details, name: "+
name+" and pwd: "+pwd;
try {
userService.addUser(name,email, pwd);
} catch (ShowAbleException e) {
// if adding triggered an error it must be displayed on the page
List<String> errors = new ArrayList<String>();
errors.add(e.getMessage());
// HOW to go back to GET method with errors?? Or just display them?
}
try {
//return Response.created(new URI("/")).entity(response).build();
return Response.seeOther(new URI("/")).build(); // redirect back to homepage...
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
}
我看到 this 页面提供了一些指导,但我未能复制它,因为源未完全提供并且它不使用 Dropwizard 的视图功能。我还考虑过向我的 GET 方法添加参数并从 POST 方法重定向,但这对我来说似乎有点矫枉过正?
任何帮助将不胜感激。
我设法通过遇到 this thread 解决了这个问题,这表明可以发送一个新的 View
作为响应。因此,如果遇到错误,则会在 Response
中创建一个新的 View
并更新错误。代码如下:
try {
userService.addUser(name,email, pwd);
} catch (ShowAbleException e) {
// if adding triggered an error it must be displayed on the page
List<String> errors = new ArrayList<String>();
errors.add(e.getMessage());
return Response.ok(new SignUpView(errors)).build();
}
try {
//return Response.created(new URI("/")).entity(response).build();
return Response.seeOther(new URI("/")).build(); // redirect back to homepage...
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
我还发现我的 SignUpView
class 需要一个 getErrors 方法,以便 Freemarker 文件可以访问错误列表。所以我补充说:
public List<String> getErrors() {
return errors;
}