在 jsp spring 数据 jpa 中添加关注按钮
Add follow button in jsp spring data jpa
我有一个用户和关注者,我对jsp不是很熟悉,想在前端添加一个关注按钮并将当前用户添加到关注者table并显示已完成的用户个人资料上的关注者。
我该怎么做?
关注者:
@Entity
public class Follower {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用户:
@Entity
@Table(name = "usr", indexes = { @Index(columnList = "email", unique = true) })
// using usr because in may conflict with the name of the class
public class User {
public static final int EMAIL_MAX = 250;
public static final int NAME_MAX = 50;
/*
* public static enum Role {
*
* UNVERIFIED, BLOCKED, ADMINISTRATOR
*
* }
*/
// primary key long, needs to be annotated with @Id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
// add columns
@Column(nullable = false, length = EMAIL_MAX)
private String email;
@Column(nullable = false, length = NAME_MAX)
private String name;
// no length, the password will be encrypted to some longer value than the
// user enters
@Column(nullable = false)
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
private List<Tweets> tweets;
@OneToMany(mappedBy = "user")
private List<Follower> followers;
public List<Follower> getFollowers() {
return followers;
}
public void setFollowers(List<Follower> followers) {
this.followers = followers;
}
public List<Tweets> getTweets() {
return tweets;
}
public void setTweets(List<Tweets> tweets) {
Collections.reverse(tweets);
this.tweets = tweets;
}
public void setUsername(String username) {
this.username = username;
}
@Column(nullable = false)
private String username;
/*
* //email verification code
*
* @Column(length = 16) private String verificationCode;
*
* public String getVerificationCode() { return verificationCode; }
*
* public void setVerificationCode(String verificationCode) {
* this.verificationCode = verificationCode; }
*
*
* @ElementCollection(fetch = FetchType.EAGER) private Set<Role> roles = new
* HashSet<Role>();
*
*
*
* public Set<Role> getRoles() { return roles; }
*
* public void setRoles(Set<Role> roles) { this.roles = roles; }
*/
public long getId() {
return id;
}
/*
public void setId(long id) {
this.id = id;
}
*/
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEditable() {
User loggedIn = MyTools.getSessionUser();
if (loggedIn == null) {
return false;
}
return loggedIn.getId() == id;
}
public String getUsername() {
return username;
}
}
在 JSP
中添加一个 "Follow" 按钮
<button id="follow_me">Follow</button>
使用 JavaScript 发送 ajax 调用并将用户详细信息发送到控制器,然后从控制器将其映射到 POJO(Follower.java)。
$('#follow_me').on('click',function(){
$.ajax(
url : url, // Controller URL
data : user_id, // Current User ID
follow_Flag : true,
success: function(result){
//Code for changing the view(JSP)
}});
);
});
和 return JSON 在成功调用中使用以下格式向用户发送,并使用 JavaScript DOM 操作将其呈现回 JSP
{ userFollowFlag : true, // 当前用户关注标志
totalFollowers : 34 // 粉丝总数
}
我有一个用户和关注者,我对jsp不是很熟悉,想在前端添加一个关注按钮并将当前用户添加到关注者table并显示已完成的用户个人资料上的关注者。
我该怎么做?
关注者:
@Entity
public class Follower {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用户:
@Entity
@Table(name = "usr", indexes = { @Index(columnList = "email", unique = true) })
// using usr because in may conflict with the name of the class
public class User {
public static final int EMAIL_MAX = 250;
public static final int NAME_MAX = 50;
/*
* public static enum Role {
*
* UNVERIFIED, BLOCKED, ADMINISTRATOR
*
* }
*/
// primary key long, needs to be annotated with @Id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
// add columns
@Column(nullable = false, length = EMAIL_MAX)
private String email;
@Column(nullable = false, length = NAME_MAX)
private String name;
// no length, the password will be encrypted to some longer value than the
// user enters
@Column(nullable = false)
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
private List<Tweets> tweets;
@OneToMany(mappedBy = "user")
private List<Follower> followers;
public List<Follower> getFollowers() {
return followers;
}
public void setFollowers(List<Follower> followers) {
this.followers = followers;
}
public List<Tweets> getTweets() {
return tweets;
}
public void setTweets(List<Tweets> tweets) {
Collections.reverse(tweets);
this.tweets = tweets;
}
public void setUsername(String username) {
this.username = username;
}
@Column(nullable = false)
private String username;
/*
* //email verification code
*
* @Column(length = 16) private String verificationCode;
*
* public String getVerificationCode() { return verificationCode; }
*
* public void setVerificationCode(String verificationCode) {
* this.verificationCode = verificationCode; }
*
*
* @ElementCollection(fetch = FetchType.EAGER) private Set<Role> roles = new
* HashSet<Role>();
*
*
*
* public Set<Role> getRoles() { return roles; }
*
* public void setRoles(Set<Role> roles) { this.roles = roles; }
*/
public long getId() {
return id;
}
/*
public void setId(long id) {
this.id = id;
}
*/
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEditable() {
User loggedIn = MyTools.getSessionUser();
if (loggedIn == null) {
return false;
}
return loggedIn.getId() == id;
}
public String getUsername() {
return username;
}
}
在 JSP
中添加一个 "Follow" 按钮<button id="follow_me">Follow</button>
使用 JavaScript 发送 ajax 调用并将用户详细信息发送到控制器,然后从控制器将其映射到 POJO(Follower.java)。
$('#follow_me').on('click',function(){
$.ajax(
url : url, // Controller URL
data : user_id, // Current User ID
follow_Flag : true,
success: function(result){
//Code for changing the view(JSP)
}});
);
});
和 return JSON 在成功调用中使用以下格式向用户发送,并使用 JavaScript DOM 操作将其呈现回 JSP { userFollowFlag : true, // 当前用户关注标志 totalFollowers : 34 // 粉丝总数 }