gpt4 book ai didi

java - 尝试为当前用户设置新的详细信息

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:51 24 4
gpt4 key购买 nike

我对编程很陌生,而且我对java的了解很有限。我正在修改一个类似 Facebook 的网站模板,这样我就可以学习和添加功能。我试图允许当前用户更改他们的详细信息,例如姓名、电子邮件、密码等,但无法弄清楚 java 是否可以这样做。当我尝试时,所发生的只是在当前用户之上创建一个全新的用户:/这是我到目前为止所拥有的:

public static void changeDetails(String firstName, String lastName, String email, String password, String age, String nationality)
{
Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

User user = new User(firstName, lastName, email, password, age, nationality);
user.save();
Home.index();
}

这是用户类别:

package models;

import java.util.List;
import java.util.ArrayList;

import javax.persistence.OneToMany;
import javax.persistence.Entity;

import controllers.Home;
import play.db.jpa.Model;
import play.db.jpa.Blob;

@Entity
public class User extends Model
{
public String firstName;
public String lastName;
public String email;
public String password;
public String age;
public String nationality;
public String statusText;
public Blob profilePicture;

@OneToMany(mappedBy = "sourceUser")
public List<Friendship> friendships = new ArrayList<Friendship>();

public User(String firstName, String lastName, String email, String password, String age, String nationality)
{
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.age = age;
this.nationality = nationality;
}


public static User findByEmail(String email)
{
return find("email", email).first();
}

public boolean checkPassword(String password)
{
return this.password.equals(password);
}

public void befriend(User friend)
{
Friendship friendship = new Friendship(this, friend);
friendships.add(friendship);
friendship.save();
save();
}

public void unfriend(User friend)
{
Friendship thisFriendship = null;

for (Friendship friendship:friendships)
{
if (friendship.targetUser== friend)
{
thisFriendship = friendship;
}
}
friendships.remove(thisFriendship);
thisFriendship.delete();
save();
}

@OneToMany(mappedBy = "to")
public List<Message> inbox = new ArrayList<Message>();

@OneToMany(mappedBy = "from")
public List<Message> outbox = new ArrayList<Message>();

public void sendMessage (User to, String messageText)
{
Message message = new Message (this, to, messageText);
outbox.add(message);
to.inbox.add(message);
message.save();
}



}

我可以看到我的问题可能是“新用户和 user.save”,但我不知道我应该有什么。感谢您的帮助:)

最佳答案

您必须将 User 类的实例传递给changeDetails 方法,并使用setter 方法更改它的属性。

编辑:

向您的代码添加注释以查看问题所在:

public static void changeDetails(String firstName, String lastName, String email, String password, String age, String nationality)
{
Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

//here you are creating new user object and setting it`s properties, wrong
User user = new User(firstName, lastName, email, password, age, nationality);
user.save();
Home.index();
}

建议的修复(假设您在 User 类中有 setter 方法):

public static void changeDetails(User user, String firstName, String lastName, String email, String password, String age, String nationality)
{
Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

user.setfirstName(firstName);
user.setlastName(lastName);
user.setEmail(email);
user.setPassword(password);
user.setAge(age);
user.setNationality(nationality);

Home.index();
}

或者在看到您的编辑和更多没有设置方法的代码后,请执行以下操作:

public static void changeDetails(User user, String firstName, String lastName, String email, String password, String age, String nationality)
{
Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.password = password;
user.age = age;
user.nationality = nationality;

Home.index();
}

关于java - 尝试为当前用户设置新的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com