Play Framework Compile Error:'(' expect but')'发现

时间:2017-03-29 03:53:12

标签: java html scala intellij-idea playframework

当我尝试编译我的Play Framework 2.项目时,我收到此错误:'('预期,但')'找到。 我尝试做的是:'登录尝试失败后,用户应该获得反馈。如果登录尝试在服务器端失败,请在失败的请求后显示页面上的错误消息。有人可以帮帮我。

我在登录尝试失败后添加了要在表单上显示的字符串,如下所示:

if (response.hasErrors()) {
return ok(create.render(createViewModelForm, "invalid username or password"));

create.scala.html

@if (!@errorMessage.isEmpty()) {
  <div>@errorMessage</div>
  }

This is the error that I get when I compile

这是 SessionsController.java &gt; UI \控制器\ SessionsController

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());
    }
}

这是ui \ views \ sessions

中的createForm
@(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>
            @if (!@errorMessage.isEmpty()) {
                <div>@errorMessage</div>
            }
            <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>
    }
}

HomeController.java

package femr.ui.controllers;

import com.google.inject.Inject;
import femr.common.dtos.CurrentUser;
import femr.business.services.core.ISessionService;
import femr.ui.views.html.home.index;
import femr.ui.views.html.sessions.create;
import play.mvc.Controller;
import play.mvc.Result;

public class HomeController extends Controller {

    private ISessionService sessionService;

    @Inject
    public HomeController(ISessionService sessionService) {
        this.sessionService = sessionService;
    }

    public Result index() {
        CurrentUser currentUser = sessionService.retrieveCurrentUserSession();

        if (null != currentUser) {
            return ok(index.render(currentUser));
        }
        return ok(create.render(null));
    }

}

2 个答案:

答案 0 :(得分:1)

尝试使用:@if(!errorMessage.isEmpty()) {

相应的旋转文档here

答案 1 :(得分:0)

1 - 当你有这样的表达式时,你不需要在变量上添加@,因为它已经在scala代码块中了。

2 - errorMessage: java.lang.String,将您的变量类型更改为String我想如果您使用Scala字符串会更好可能是模板引擎的一些差异。< / p>

@if (!errorMessage.isEmpty()) { <div>@errorMessage</div> }