我在用户的个人资料上有一个关注按钮,该按钮应该将当前用户添加到数据库表关注者,但是当我点击按钮时我没有发生任何事情我已经设置了所有内容(我知道)< / p>
所以
我有一个关注者服务
@Service
public class FollowerService {
@Autowired
private FollowerRepository followerRepo;
@Autowired
private UserRepository userRepo;
public void save(Follower follower, String name) {
User user = MyTools.getSessionUser();
follower.setUser(user);
System.out.println(name + "=====================================================");
followerRepo.save(follower);
}
public void delete(long id) {
followerRepo.delete(id);
}
}
这是非常标准的,我可能会在某个地方丢失数据,但我完全没有看到它出于某种原因,不确定问题是什么
FollowerRepository
public interface FollowerRepository extends JpaRepository<Follower, Long> {
List<Follower> findByUser(User user);
}
追随者实体:
@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;
}
}
在UserController中我有一张地图不知道如何映射这个,我有一个推文按钮,可以在/ {userId}中工作,如果我把它映射到同一个网址我会收到错误
@RequestMapping(value = "/{userId}/follow", method = RequestMethod.POST)
public String doAddFollower(@ModelAttribute("follow") Follower follower, Principal principal) {
String name = principal.getName();
followerService.save(follower, name);
return "redirect:/users/{userId}";
}
更新:
这是我在按钮视图中的内容,我确定这个有问题:
<form:form modelAttribute="follow" cssClass="form-horizontal">
<button value="Follow Me" type="Submit" class="btn btn-primary btn-lg" style=" margin-left: 10px;" >Follow Me</button>
</form:form>
答案 0 :(得分:1)
你必须添加&#34;动作&#34; <form:form>
标记上的属性。例如:
<form:form method="post" action="follow" modelAttribute="follow" cssClass="form-horizontal">
<button value="Follow Me" type="Submit" class="btn btn-primary btn-lg" style=" margin-left: 10px;" >Follow Me</button>
</form:form>