2017-04-01 125 views
0

我在Intellij中使用的Play Framework 2.5.x,我不太熟悉,我需要獲取頁面才能在失敗後顯示錯誤消息請求。 當前,當我輸入不正確的憑據時,頁面將重新加載而不顯示錯誤。我只需要一個簡單的方法來顯示錯誤。如何在登錄頁面顯示一條消息,如果登錄失敗

如果用戶輸入他們的憑證,則if語句會評估是否存在表單錯誤。如果是這樣,再次顯示錶單,否則處理提交。

if (response.hasErrors()) { 
     return ok(create.render(createViewModelForm)); 
     } 

這是createForm.html在UI \意見\會議

@(gcreateForm: Form[femr.ui.models.sessions.CreateViewModel], errorMessage: java.lang.String) 

@import femr.ui.views.html.layouts.main 
@import femr.ui.controllers.routes.SessionsController 
    @styles = { 
     <link rel="stylesheet" href="@routes.Assets.versioned("css/login.css")"> 
    } 
@main("Login", styles = styles) { 

    @helper.form(action = SessionsController.createPost(), 'class -> "form-signin") { 
     <div id="login"> 
      <h1>Please sign in</h1> 
      <input type="text" name="email" placeholder="Email" /> 
      <input type="password" name="password" placeholder="Password" /> 
      <input type="submit" value="Log in" /> 
      <img src="@routes.Assets.versioned("img/logo_color_sm.png")" /> 
     </div> 
    } 
} 

這是在UI上的CreateForm \意見\會議

package femr.ui.controllers; 

import com.google.inject.Inject; 
import femr.business.services.core.ISessionService; 
import femr.business.services.core.IUserService; 
import femr.common.dtos.CurrentUser; 
import femr.common.dtos.ServiceResponse; 
import femr.data.models.core.IUser; 
import femr.ui.models.sessions.CreateViewModel; 
import femr.ui.views.html.sessions.create; 
import femr.ui.views.html.sessions.editPassword; 
import femr.util.calculations.dateUtils; 
import femr.util.stringhelpers.StringUtils; 
import org.joda.time.DateTime; 
import org.joda.time.Days; 
import play.data.Form; 
import play.data.FormFactory; 
import play.mvc.Controller; 
import play.mvc.Result; 

import java.util.ArrayList; 
import java.util.regex.Pattern; 

public class SessionsController extends Controller { 

    private final FormFactory formFactory; 
    private final ISessionService sessionsService; 
    private final IUserService userService; 

    @Inject 
    public SessionsController(FormFactory formFactory, ISessionService sessionsService, IUserService userService) { 

     this.formFactory = formFactory; 
     this.sessionsService = sessionsService; 
     this.userService = userService; 
    } 

    public Result createGet() { 
     CurrentUser currentUser = sessionsService.retrieveCurrentUserSession(); 

     final Form<CreateViewModel> createViewModelForm = formFactory.form(CreateViewModel.class); 

     if (currentUser != null) { 
      return redirect(routes.HomeController.index()); 
     } 

     return ok(create.render(createViewModelForm)); 
    } 

    public Result createPost() { 

     final Form<CreateViewModel> createViewModelForm = formFactory.form(CreateViewModel.class); 
     CreateViewModel viewModel = createViewModelForm.bindFromRequest().get(); 
     ServiceResponse<CurrentUser> response = sessionsService.createSession(viewModel.getEmail(), viewModel.getPassword(), request().remoteAddress()); 

     if (response.hasErrors()) { 
      return ok(create.render(createViewModelForm)); 
     }else{ 
      IUser user = userService.retrieveById(response.getResponseObject().getId()); 
      user.setLastLogin(dateUtils.getCurrentDateTime()); 
      ServiceResponse<IUser> userResponse = userService.update(user, false); 
      if (userResponse.hasErrors()){ 
       throw new RuntimeException(); 
      } 

      DateTime start = new DateTime(user.getPasswordCreatedDate()); 
      DateTime stop = new DateTime(DateTime.now()); 
      int daysBetween = Days.daysBetween(start, stop).getDays(); 

      if(daysBetween > 60){ 
       user.setPasswordReset(true); 
      } 

      if (user.getPasswordReset() == true){ 
       return editPasswordGet(user); 
      } 
     } 

     return redirect(routes.HomeController.index()); 

    } 

    public Result editPasswordGet(IUser user){ 

     final Form<CreateViewModel> createViewModelForm = formFactory.form(CreateViewModel.class); 

     return ok(editPassword.render(user.getFirstName(), user.getLastName(), createViewModelForm, new ArrayList<String>())); 
    } 

    public Result editPasswordPost(){ 

     final Form<CreateViewModel> createViewModelForm = formFactory.form(CreateViewModel.class); 
     CreateViewModel viewModel = createViewModelForm.bindFromRequest().get(); 
     CurrentUser currentUser = sessionsService.retrieveCurrentUserSession(); 
     IUser user = userService.retrieveById(currentUser.getId()); 
     Boolean isNewPassword = false; 

     Pattern hasUppercase = Pattern.compile("[A-Z]"); 
     Pattern hasNumber = Pattern.compile("\\d"); 
     ArrayList<String> messages = new ArrayList<>(); 
     if (StringUtils.isNullOrWhiteSpace(viewModel.getNewPassword())) 
      messages.add("password is a required field"); 
     else 
     { 
      if(viewModel.getNewPassword().length() < 6)  //AJ Saclayan Password Constraints 
       messages.add("password is less than 6 characters"); 
      if (!hasUppercase.matcher(viewModel.getNewPassword()).find()) 
        messages.add("password must have an uppercase"); 
      if (!hasNumber.matcher(viewModel.getNewPassword()).find()) 
        messages.add("password must have a number"); 
      if(!viewModel.getNewPassword().equals(viewModel.getNewPasswordVerify())) 
       messages.add("passwords do not match"); 
      //check if new password is equal to the old password 
      if(userService.checkOldPassword(viewModel.getNewPassword(),userService.retrieveById(currentUser.getId()).getPassword())) 
       messages.add("password must not be the same one used before reset"); 


     } 

     if(!messages.isEmpty()) 
      return ok(editPassword.render(user.getFirstName(), user.getLastName(), createViewModelForm, messages)); 
     else 
     { 
      user.setPassword(viewModel.getNewPassword()); 
      user.setPasswordCreatedDate(DateTime.now()); 
      user.setPasswordReset(false); 
      isNewPassword = true; 
     } 

     ServiceResponse<IUser> userResponse = userService.update(user, isNewPassword); 
     if (userResponse.hasErrors()){ 
      throw new RuntimeException(); 
     } 
     return redirect(routes.HomeController.index()); 
    } 

    public Result delete() { 
     sessionsService.invalidateCurrentUserSession(); 

     return redirect(routes.HomeController.index()); 
    } 
} 

回答

1

您可以輕鬆地通過使用Flash scope來顯示用戶反饋。如documentation所述,數據僅保留一個請求,這符合您在發生錯誤時呈現錯誤消息的要求。

例如:

public static Result yourAction() { 
    // Oh snap! Error occurred... 
    flash("error", "Invalid credentials."); 
    return redirect("/your-action"); 
} 

然後在您的捻模板:

@(request: RequestHeader) 

@request.flash.get("error").map { msg => 
<div class="alert alert-danger alert-error"> 
    <strong>Error!</strong> @msg 
</div> 
} 
相關問題