提交添加新邮件时出现错误 403



我正在学习弹簧引导并编写一个小项目表单。我以想法运行它并提交表单,注册有效,但是当我要添加新消息时,浏览器发生了

There was an unexpected error (type=Forbidden, status=403).

禁止

我试图添加新的功能,我希望它也会显示用户的名称,但不起作用

这是一个消息类

@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String text;
private String tag;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User author;

public Message() {
}
public Message(String text, String tag, User user) {
this.author=user;
this.text = text;
this.tag = tag;
}
public String getAuthorName(){
return author !=null ? author.getUsername() : "<nine>";
}
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}

控制器

@Controller
public class MainController {
@Autowired
private MessasgeRepo messasgeRepo;
@GetMapping("/")
public String greeting(Model model) {
return "greeting";
}
@GetMapping("/main")
public String main(Model model){
Iterable<Message> messages = messasgeRepo.findAll();
model.addAttribute("mess",messages);
return "main";
}
@PostMapping("/main")
public String add (@AuthenticationPrincipal User user,
@RequestParam String text,
@RequestParam String tag, Model model){
Message message =new Message(text, tag, user);
messasgeRepo.save(message);
Iterable<Message> messages = messasgeRepo.findAll();
model.addAttribute("mess",messages);
return "main";
}
@PostMapping("filter")
public String filter(@RequestParam String text, Model model){
Iterable<Message> messages;
if(text!=null && !text.isEmpty()) {
messages = messasgeRepo.findByTag(text);
}else {
messages = messasgeRepo.findAll();
}
model.addAttribute("mess", messages);
return "main";
}
}

和 HTML 文档

<div>
<form method="post">
<input type="text"  name="text" placeholder="Enter first name"  >
<input type="text" name="tag" placeholder="Enter first name" >
<button type="submit" class="btn btn-success"> Add order</button>
</form>
</div>
<form method="post" action="/filter">
<input type="text" name="text">
<button type="submit">Search</button>
</form>
<p>Message</p>
<div th:each="me : ${mess}" >
<b th:text="${me.id}"/>
<span th:text="${me.text}"/>
<i th:text="${me.tag}"/>
<strong th:text="${me.authorName}"/>
</div>

我认为这里缺少操作action="/main",请尝试以下代码

<form method="post" action="/main">
<input type="text"  name="text" placeholder="Enter first name">
<input type="text" name="tag" placeholder="Enter first name">
<button type="submit" class="btn btn-success"> Add order </button>
</form>

最新更新