Java 在jsp spring数据jpa中添加follow按钮

Java 在jsp spring数据jpa中添加follow按钮,java,mysql,jsp,spring-mvc,spring-data-jpa,Java,Mysql,Jsp,Spring Mvc,Spring Data Jpa,我有一个用户和跟随者,我对jsp不是很熟悉,我想在前端添加一个跟随按钮,将当前用户添加到followers表,并在用户配置文件上显示followers,这已经完成了 我该怎么办 追随者: @Entity public class Follower { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @ManyToOne @JoinColumn(name =

我有一个用户和跟随者,我对jsp不是很熟悉,我想在前端添加一个跟随按钮,将当前用户添加到followers表,并在用户配置文件上显示followers,这已经完成了

我该怎么办

追随者:

@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;
    }
}
@实体
@表(name=“usr”,Index={@Index(columnList=“email”,unique=true)})
//使用usr,因为in可能与类的名称冲突
公共类用户{
公共静态最终int电子邮件_MAX=250;
公共静态最终整数名_MAX=50;
/*
*公共静态枚举角色{
* 
*未验证、阻止、管理员
* 
* }
*/
//主键长,需要用@Id注释
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
//添加列
@列(nullable=false,length=EMAIL_MAX)
私人字符串电子邮件;
@列(nullable=false,length=NAME\u MAX)
私有字符串名称;
//无长度,密码将被加密为比
//用户输入
@列(nullable=false)
私有字符串密码;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“user”)
私人列表推文;
@OneToMany(mappedBy=“用户”)
私人名单追随者;
公共列表getFollowers(){
回归追随者;
}
public void setFollowers(列出followers){
this.followers=追随者;
}
公共列表getTweets(){
返回推文;
}
公共无效设置tweets(列出tweets){
收藏。反向(推特);
this.tweets=tweets;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
@列(nullable=false)
私有字符串用户名;
/*
*//电子邮件验证码
* 
*@Column(长度=16)私有字符串验证代码;
* 
*公共字符串getVerificationCode(){return verificationCode;}
* 
*public void setVerificationCode(字符串验证代码){
*this.verificationCode=verificationCode;}
* 
* 
*@ElementCollection(fetch=FetchType.EAGER)私有集角色=新建
*HashSet();
* 
* 
* 
*public Set getRoles(){return roles;}
* 
*public void setRoles(Set roles){this.roles=roles;}
*/
公共长getId(){
返回id;
}
/*
公共无效集合id(长id){
this.id=id;
}
*/
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共布尔值可编辑(){
用户loggedIn=MyTools.getSessionUser();
if(loggedIn==null){
返回false;
}
返回loggedIn.getId()==id;
}
公共字符串getUsername(){
返回用户名;
}
}
在JSP中添加“跟随”按钮

<button id="follow_me">Follow</button>
并在成功调用中使用以下格式将JSON返回给用户,并使用JavaScript DOM操纵将其呈现回JSP {userFollowFlag:true,//当前用户跟随标志 totalFollowers:34//追随者总数 }

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