2014-07-18 15 views
0

我创建了一个登录页面..如果用户已经注册,登录将起作用,并且它将重定向到配置文件页面。在配置文件中提交值之后,数据将存储在数据库中。同样的用户会再次登录,它会显示空白页面来填充。但它会显示用户填充的细节。为此,我只获取登录的唯一配置文件ID并获取该用户的详细信息。但我不知道如何通过在html中使用“value”属性来将这些细节设置到我的前端表单文本字段中。例如,我获取姓氏并将其设置为我的控制器中的一个变量。如何在播放框架中将我的后端值设置为我的表单文本字段

这是我的控制器:

package controllers; 

import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import models.ForgotPassword; 
import models.Profile; 
import models.Register; 

import org.apache.commons.mail.EmailException; 

import play.Logger; 
import play.data.Form; 
import play.libs.F.Promise; 
import play.libs.OpenID; 
import play.libs.OpenID.UserInfo; 
import play.mvc.Controller; 
import play.mvc.Result; 
import views.html.*; 
import views.html.index; 
import views.html.login; 
import views.html.profile; 

import com.typesafe.plugin.MailerAPI; 
import com.typesafe.plugin.MailerPlugin; 


public class Application extends Controller { 

    public static Result index() { 
     return ok(index.render("Your new application is ready.")); 
    } 

    public static Result signup() { 
     return ok(login.render("fghj")); 

    } 

    public static Result register() { 

     Register register = Form.form(Register.class).bindFromRequest().get(); 
     register.save(); 
     return ok(login.render("ghjk")); 
    } 

    public static Result login() { 
     Register register = Form.form(Register.class).bindFromRequest().get(); 
     String uname = register.getUsername(); 
     String pass = register.getPassword(); 
     List<Register> login = Register.find.where().eq("username", uname) 
       .eq("password", pass).findList(); 
     if (login.isEmpty()) { 
      return unauthorized("Please register and try login"); 
     } else { 
      session().clear(); 
      session("registerId", +login.get(0).getRegisterId() + ""); 
      return ok(app.render("wsdfghjk")); 
      } 
    } 

    public static Result home() { 
     return ok(app.render("fghjk")); 
    } 

    public static Result menu() { 
     String reg=session().get("registerId"); 
     Profile profile1=Profile.find.byId(reg); 
     String last=profile1.getLastname(); 
     return ok(profile.render("" ,profile1)); 
     //return ok(profile.render("")); 


    } 

    public static Result logout() { 
     return ok(index.render("fhjk")); 
    } 

    public static Result profile() { 
     Profile profile = Form.form(Profile.class).bindFromRequest().get(); 
     profile.getRegister().setRegisterId(
       Integer.parseInt(session().get("registerId"))); 

     profile.save(); 
     return ok(); 
    } 

    public static Result forgotPassword() { 

     return ok(forgotPassword.render("eghj")); 
    } 

    public static Result email() throws EmailException { 
     ForgotPassword forgotPass = Form.form(ForgotPassword.class) 
       .bindFromRequest().get(); 
     String emailId = forgotPass.getEmail(); 
     System.out.println(emailId); 
     MailerAPI mail = play.Play.application().plugin(MailerPlugin.class).email(); 
      mail.setSubject("mailer"); 
      mail.setRecipient(emailId); 
      mail.setFrom("[email protected]"); 
      mail.send("text"); 
      return ok("gg"); 


    } 

    @SuppressWarnings("serial") 
    public static final Map<String, String> identifiers = new HashMap<String, String>() { 
     { 
      put("google", "https://www.google.com/accounts/o8/id"); 
     } 
    }; 

    @SuppressWarnings("deprecation") 
    public static Result auth() { 
     Logger.debug("authenticate"); 
     String providerId = "google"; 
     String providerUrl = identifiers.get(providerId); 
     String returnToUrl = "http://localhost:9000/login/verify"; 
     if (providerUrl == null) { 
      return badRequest("Could not find provider " + providerId); 
     } 

     Map<String, String> attributes = new HashMap<String, String>(); 
     attributes.put("Email", "http://schema.openid.net/contact/email"); 
     attributes 
       .put("FirstName", "http://schema.openid.net/namePerson/first"); 
     attributes.put("LastName", "http://schema.openid.net/namePerson/last"); 

     Promise<String> redirectUrl = OpenID.redirectURL(providerUrl, 
       returnToUrl, attributes); 
     return redirect(redirectUrl.get()); 
    } 

    public static Result verify() { 
     Logger.debug("verifyLogin"); 
     System.out.println("verify execuited"); 
     Promise<UserInfo> userInfoPromise = OpenID.verifiedId(); 
     userInfoPromise.get(); 
     return ok(profile.render("ert")); 
    } 

} 

在属性值我犯的错误我是这么认为的人,请更正。我没有使用表单助手模板。 HTML代码:

@(message: String,profile1: Profile) 
@main("") { 
<div class="container test"> 

    <!-- form: --> 
    <section> 
     <div class="col-lg-8 col-lg-offset-2"> 
      <label> <a href="@routes.Application.home()">Home</a> 
      </label> 
      <label> <a href="@routes.Application.logout" class="logout">Logout</a> 
      </label> 

      <div class="page-header"> 
       <h2>Profile</h2> 

      </div> 

      <form class="form-horizontal" method="POST" id="profile" 
       action="@routes.Application.profile()"> 
       <div class="input-group"> 
        <input type="file" class="file"> 
       </div> 

       <div class="sample"> 

        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="firstName">First 
          Name:</label> 
         <div class="col-lg-4"> 
          <input type="text" class="form-control" name="firstname" value="" 
           id="firstName" autocomplete="off" required 
           data-bv-notempty-message="The first name is required" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="middleName">Middle 
          Name:</label> 
         <div class="col-lg-4"> 
          <input type="text" class="form-control" name="middlename" 
           autocomplete="off" id="middleName"> 
         </div> 
        </div> 

        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="lastName">Last 
          Name:</label> 
         <div class="col-lg-4"> 

          <input type="text" class="form-control" name="lastname" value='profile1.getLastname' 
           id="lastName" autocomplete="off" required 
           data-bv-notempty-message="The last name is required" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label">Date of Birth:</label> 
         <div class="col-xs-4"> 
          <input type="date" class="form-control" name="dob" id="dob" 
           autocomplete="off" data-bv-date="false" 
           data-bv-date-message="The birthday is not valid" required 
           data-bv-notempty-message="The date of birth is required" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label">Gender:</label> 
         <div class="col-xs-2"> 
          <label class="radio-inline"> <input type="radio" 
           name="gender" value="male" required 
           data-bv-notempty-message="The gender is required" /> Male 
          </label> 
         </div> 
         <div class="col-xs-2"> 
          <label class="radio-inline"> <input type="radio" 
           name="gender" value="female"> Female 
          </label> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="maritalStatus">Marital 
          Status: </label> 
         <div class="col-lg-4"> 
          <select class="form-control" name="maritalStatus"> 

           <option value="single">Single</option> 
           <option value="married">Married</option> 
           <option value="divorced">Divorced</option> 

          </select> 
         </div> 
        </div> 

        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="permanentAddress">Permanent 
          Address:</label> 
         <div class="col-lg-4"> 
          <textarea rows="3" class="form-control" name="permanentAddress" 
           id="permanentAddress" required 
           data-bv-notempty-message="The permanent address is required"></textarea> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="tempAddress">Temporary 
          Address:</label> 
         <div class="col-lg-4"> 
          <textarea rows="3" class="form-control" name="tempAddress" 
           id="tempAddress" required 
           data-bv-notempty-message="The temporary address is required"></textarea> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="bloodGroup">Blood 
          Group:</label> 
         <div class="col-lg-4"> 
          <input type="text" class="form-control" name="bloodGroup" 
           id="bloodGroup" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="idMark">Identification 
          Mark:</label> 
         <div class="col-lg-4"> 
          <input type="text" class="form-control" name="identificationMark" 
           id="idMark" autocomplete="off" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="personalNo">Personal 
          No:</label> 
         <div class="col-lg-4"> 
          <input type="tel" class="form-control" name="personalNum" 
           id="personalNo" autocomplete="off" required 
           data-bv-notempty-message="The phone no is required"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="emergencyNo">Emergency 
          No:</label> 
         <div class="col-lg-4"> 
          <input type="tel" class="form-control" name="emergencyNum" 
           id="emergencyNo" autocomplete="off" required 
           data-bv-notempty-message="The phone no is required"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="location">Location: 
         </label> 
         <div class="col-lg-4"> 
          <input type="text" class="form-control" name="location" 
           id="location" autocomplete="off"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="state">State: </label> 
         <div class="col-lg-4"> 
          <select class="form-control bfh-select-fix bfh-states" 
           name="state"></select> 

         </div> 
        </div> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label" for="state">Country: 

         </label> 
         <div class="col-lg-4"> 
          <select class="form-control bfh-countries" name="country" 
           data-country="US" id="country-selector"></select> 


         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-xs-offset-3 col-xs-9"> 
          <input type="submit" class="btn btn-primary" value="Submit"> 
         </div> 
        </div> 
       </div> 
      </form> 
     </div> 
    </section> 

</div> 



<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#profile').bootstrapValidator(); 
     var form=document.getElementById("profile") 
     form.reset(); 

    }); 

</script> 
<style type="text/css"> 
.form-horizontal .control-label { 
    text-align: left; 
} 

.test .input-group .btn-default,.form-control.file-caption,.glyphicon,.close 
    { 
    display: none; 
} 

.sample { 
    margin-bottom: 50px; 
} 

.test .btn-file:hover { 
    background: #ffffff; 
    border: none; 
} 
</style> 
} 

Profile.java在型号: 封装模型;

import java.util.Date; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 

import play.db.ebean.Model; 

@Entity 
@Table(name = "profile") 
public class Profile extends Model { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator ="profile_profile_id_seq") 
    private int profileId; 


    public int getProfileId() { 
     return profileId; 
    } 

    public void setProfileId(int profileId) { 
     this.profileId = profileId; 
    } 
    @OneToOne 
    @JoinColumn(table="register", name= "register_id") 
    private Register register; 

    private int registerId; 
    public Register getRegister() { 
     return register; 
    } 

    public void setRegister(Register register) { 
     this.register = register; 
    } 

    public Profile(){ 
     register = new Register(); 
    } 
    private byte[] photo; 
    @Column(name="firstname") 
    private String firstname; 
    private String middlename; 
    private String lastname; 
    private Date dob; 
    private String gender; 
    private String maritalStatus; 
    private String permanentAddress; 
    private String tempAddress; 
    private String bloodGroup; 
    private String identificationMark; 
    private String personalNum; 
    private String emergencyNum; 
    private String location; 
    private String state; 
    private String country; 



    public byte[] getPhoto() { 
     return photo; 
    } 

    public void setPhoto(byte[] photo) { 
     this.photo = photo; 
    } 

    public String getIdentificationMark() { 
     return identificationMark; 
    } 

    public void setIdentificationMark(String identificationMark) { 
     this.identificationMark = identificationMark; 
    } 

    public String getPersonalNum() { 
     return personalNum; 
    } 

    public void setPersonalNum(String personalNum) { 
     this.personalNum = personalNum; 
    } 

    public String getEmergencyNum() { 
     return emergencyNum; 
    } 

    public void setEmergencyNum(String emergencyNum) { 
     this.emergencyNum = emergencyNum; 
    } 

    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 

    public String getMiddlename() { 
     return middlename; 
    } 

    public void setMiddlename(String middlename) { 
     this.middlename = middlename; 
    } 

    public String getLastname() { 
     return lastname; 
    } 

    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 



    public Date getDob() { 
     return dob; 
    } 

    public void setDob(Date dob) { 
     this.dob = dob; 
    } 

    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public String getMaritalStatus() { 
     return maritalStatus; 
    } 

    public void setMaritalStatus(String maritalStatus) { 
     this.maritalStatus = maritalStatus; 
    } 

    public String getPermanentAddress() { 
     return permanentAddress; 
    } 

    public void setPermanentAddress(String permanentAddress) { 
     this.permanentAddress = permanentAddress; 
    } 

    public String getTempAddress() { 
     return tempAddress; 
    } 

    public void setTempAddress(String tempAddress) { 
     this.tempAddress = tempAddress; 
    } 

    public String getBloodGroup() { 
     return bloodGroup; 
    } 

    public void setBloodGroup(String bloodGroup) { 
     this.bloodGroup = bloodGroup; 
    } 


    public String getLocation() { 
     return location; 
    } 

    public void setLocation(String location) { 
     this.location = location; 
    } 

    public String getState() { 
     return state; 
    } 

    public void setState(String state) { 
     this.state = state; 
    } 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String toString() { 
     StringBuilder stringBuilder = new StringBuilder(); 


     stringBuilder.append("Profile[Photo="); 
     stringBuilder.append(photo); 
     stringBuilder.append(",ProfileId="); 
     stringBuilder.append(profileId); 
     stringBuilder.append(",FirstName="); 
     stringBuilder.append(firstname); 
     stringBuilder.append(",MiddleName="); 
     stringBuilder.append(middlename); 
     stringBuilder.append(",LastName="); 
     stringBuilder.append(lastname); 
     stringBuilder.append(",Dob="); 
     stringBuilder.append(dob); 
     stringBuilder.append(",Gender="); 
     stringBuilder.append(gender); 
     stringBuilder.append(",MaritalStatus="); 
     stringBuilder.append(maritalStatus); 
     stringBuilder.append(",PermanentAddress="); 
     stringBuilder.append(permanentAddress); 
     stringBuilder.append(",TempAddress="); 
     stringBuilder.append(tempAddress); 
     stringBuilder.append(",BloodGroup="); 
     stringBuilder.append(bloodGroup); 
     stringBuilder.append(",IdentificationMark="); 
     stringBuilder.append(identificationMark); 
     stringBuilder.append(",PersonalNum="); 
     stringBuilder.append(personalNum); 
     stringBuilder.append(",EmergencyNum="); 
     stringBuilder.append(emergencyNum); 
     stringBuilder.append(",Location="); 
     stringBuilder.append(location); 
     stringBuilder.append(",Country="); 
     stringBuilder.append(country); 
     stringBuilder.append(",State="); 
     stringBuilder.append(state); 
     stringBuilder.append(",Register Id="); 
     stringBuilder.append(register != null ? register.getRegisterId() : ""); 

     stringBuilder.append("]"); 
     return stringBuilder.toString(); 

    } 
    public int getRegisterId() { 
     return registerId; 
    } 

    public void setRegisterId(int registerId) { 
     this.registerId = registerId; 
    } 
    public static Finder<String, Profile> find = new Finder<String, Profile>(
      String.class, Profile.class); 


} 

这就是我得到的控制台:

  [error] /home/e100085/Software/play-2.2.3/myApps/app/controllers/Application.java:142: error: method render in class profile cannot be applied to given types; 
[error]     return ok(profile.render("ert")); 
[error]        ^
[error] required: String,Profile 
[error] found: String 
[error] reason: actual and formal argument lists differ in length 
[error] Note: /home/e100085/Software/play-2.2.3/myApps/app/controllers/Application.java uses or overrides a deprecated API. 
[error] Note: Recompile with -Xlint:deprecation for details. 
[error] 1 error 
[error] (compile:compile) javac returned nonzero exit code 
[error] application - 

! @6j0a93m46 - Internal server error, for (GET) [/] -> 

play.PlayExceptions$CompilationException: Compilation error[error: method render in class profile cannot be applied to given types;] 
     at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14$$anonfun$apply$16.apply(PlayReloader.scala:304) ~[na:na] 
     at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14$$anonfun$apply$16.apply(PlayReloader.scala:304) ~[na:na] 
     at scala.Option.map(Option.scala:145) ~[scala-library-2.10.4.jar:na] 
     at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14.apply(PlayReloader.scala:304) ~[na:na] 
     at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14.apply(PlayReloader.scala:298) ~[na:na] 
     at scala.Option.map(Option.scala:145) ~[scala-library-2.10.4.jar:na] 
[warn] play - No application found at invoker init 

回答

0

如果你不想使用表单辅助模板,你可以简单地明确地传递你配置文件1对象到您的模板:

@(message: String, profile: Profile) 

@main("") { 
    <div class="form-group"> 
     <label class="col-lg-3 control-label" for="lastName">Last 
      Name:</label> 
     <div class="col-lg-4"> 

      <input type="text" class="form-control" name="lastname" value='@profile.getLastname' 
      id="lastName" autocomplete="off" required 
      data-bv-notempty-message="The last name is required" /> 
     </div> 
    </div> 
} 

和Controller:

public static Result menu() { 
     //rest omitted 
     Profile profile1=Profile.find.byId(reg); 
     //rest omitted 
     return ok(profile.render("", profile1)); 
    } 
+0

我在渲染方法返回错误(profile.render(“”,profile1)); – user3651240

+0

请让我看看您的模板标题 –

+0

您的Profile类别位于何处?您需要在模板中指定路径。例如: @(消息:字符串,简介:my.model.package.Profile) 此外,你错过了之前@ * profile1.getLastname * –

相关问题