【问题标题】:Add follow button in jsp spring data jpa在jsp spring data jpa中添加关注按钮
【发布时间】:2015-08-31 18:36:10
【问题描述】:

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

我该怎么办?

关注者:

@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;
    }
}

【问题讨论】:

    标签: java mysql jsp spring-mvc spring-data-jpa


    【解决方案1】:

    在 JSP 中添加“关注”按钮

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

    并使用以下格式在成功调用中将 JSON 返回给用户,并使用 JavaScript DOM 操作将其呈现回 JSP { userFollowFlag : true, // 当前用户关注标志 totalFollowers : 34 // 关注者总数 }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2015-07-14
      • 2021-02-09
      • 2019-01-27
      • 1970-01-01
      • 2021-07-03
      • 2016-04-03
      • 2019-07-15
      相关资源
      最近更新 更多