提交添加新消息时出现错误403

时间:2020-05-13 16:08:51

标签: java spring spring-boot

我正在学习spring boot并编写一个小型项目表格。我在构思中运行它并提交表单,注册工作正常,但是当我试图添加新消息时,浏览器就会出现

Logging object of Type Hermod2._0.clsCustomAutoCompleteTextbox : Hermod2._0.clsCustomAutoCompleteTextbox, Text: 
Property AutoSize : True
Property CanFocus : False
Property ClientSize : {Width=281, Height=16}
Property Enabled : True
Property Handle : 3279192
Property Height : 20
Property Name : txtPartner
Property Parent : System.Windows.Forms.GroupBox, Text: Dossier détecté
    |=> Logging object of Type System.Windows.Forms.GroupBox : System.Windows.Forms.GroupBox, Text: Dossier détecté
    |=> Property AutoSize : False
    |=> Property CanFocus : True
    |=> Property ClientSize : {Width=458, Height=113}
    |=> Property Enabled : True
    |=> Property Handle : 2952070
    |=> Property Height : 113
    |=> Property Name : groupBox2
    |=> Property Parent : System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None
        |=> Logging object of Type System.Windows.Forms.Panel : System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None
        |=> Property AutoSize : False
        |=> Property CanFocus : True
        |=> Property ClientSize : {Width=778, Height=399}
        |=> Property Enabled : True
        |=> Property Handle : 4130368
        |=> Property Height : 399
        |=> Property Name : panel1
        |=> Property Parent : Hermod2._0.CaseSearch, Text: Recherche de dossier
            |=> Logging object of Type Hermod2._0.CaseSearch : Hermod2._0.CaseSearch, Text: Recherche de dossier
            |=> Property AutoSize : False
            |=> Property ClientSize : {Width=783, Height=405}
            |=> Property Size : {Width=799, Height=444}
            |=> Property CanFocus : True
            |=> Property Enabled : True
            |=> Property Handle : 1379040
            |=> Property Height : 444
            |=> Property Name : CaseSearch
            |=> Property Parent : 
                |=> Can't log object, it's null...
            |=> Property Visible : True
            |=> Property Width : 799
            |=> Property PreferredSize : {Width=798, Height=442}
        |=> Property Size : {Width=778, Height=399}
        |=> Property Visible : True
        |=> Property Width : 778
        |=> Property PreferredSize : {Width=771, Height=358}
    |=> Property Size : {Width=458, Height=113}
    |=> Property Visible : True
    |=> Property Width : 458
    |=> Property PreferredSize : {Width=457, Height=115}
Property Size : {Width=285, Height=20}
Property Visible : True
Property Width : 285
Property PreferredSize : {Width=54, Height=20}

禁止

我试图添加一个新功能,我希望它也显示用户名,但是不起作用

这是一个Message类

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

和html文档

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

1 个答案:

答案 0 :(得分:1)

我认为此处缺少动作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>