发布一个简单表单不起作用

9gm1akwq  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(367)

我是java和spring的新手,我正在尝试创建一个简单的post表单。模板如下所示:

<form action="#" th:action="@{/notes}" method="POST">
  <input type="hidden" name="noteId" id="note-id">
  <div class="form-group">
    <label for="note-title" class="col-form-label">Title</label>
    <input type="text" name= "noteTitle" class="form-control" id="note-title" maxlength="20" required>
  </div>
  <div class="form-group">
    <label for="note-description" class="col-form-label">Description</label>
    <textarea class="form-control" name="noteDescription" id="note-description" rows="5" maxlength="1000" required></textarea>
  </div>
  <button id="noteSubmit" type="submit" class="d-none"></button>
</form>

这是此post端点的控制器:

@PostMapping("/notes")
    public String postNote(@ModelAttribute Note note, Authentication authentication, Model model) throws IOException {
        User user = this.userService.getUser(authentication.getName());
        Integer userid = user.getUserId();
        noteService.createNote(note, userid);
        model.addAttribute("success",true);
        model.addAttribute("message","New note added successfully!");
        return "result";
    }

noteservice createnote方法如下所示:

public int createNote(Note note, Integer userid) {
    return noteMapper.insert(new Note(null, note.getNotetitle(), note.getNotedescription(), userid));
}

Map绘制程序如下所示:

@Insert("INSERT INTO CREDENTIALS (notetitle, notedescription, userid) VALUES(#{filename}, #{notetitle}, #{notedescription}, #{userid})")
@Options(useGeneratedKeys = true, keyProperty = "noteid")
int insert(Note note);

这是模型:

public class Note {
    private Integer noteid;
    private String notetitle;
    private String notedescription;
    private Integer userid;

    public Note(Integer noteid, String notetitle, String notedescription, Integer userid) {
        this.noteid = noteid;
        this.notetitle = notetitle;
        this.notedescription = notedescription;
        this.userid = userid;
    }

    public Integer getNoteid() {
        return noteid;
    }

    public void setNoteid(Integer fileId) {
        this.noteid = noteid;
    }

    public String getNotetitle() {
        return notetitle;
    }

    public void setNotetitle(String filename) {
        this.notetitle = notetitle;
    }

    public String getNotedescription() {
        return notedescription;
    }

    public void setNotedescription(String notedescription) {
        this.notedescription = notedescription;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) { this.userid = userid; }
}

这是sql模式:

CREATE TABLE IF NOT EXISTS NOTES (
    noteid INT PRIMARY KEY auto_increment,
    notetitle VARCHAR(20),
    notedescription VARCHAR (1000),
    userid INT,
    foreign key (userid) references USERS(userid)
);

当我试图发布便条时,出现了一个错误:
出现意外错误(类型=未找到,状态=404)。无可用消息
当我在调试模式下运行它时,它甚至不能到达控制器。我做错什么了?这是回购。

gv8xihay

gv8xihay1#

错误404通常意味着你有错误的网址(https://httpstatuses.com/404)
这就解释了为什么“它连控制器都到不了”
仔细检查是否指向正确的api url

8mmmxcuj

8mmmxcuj2#

当@leff发现自己的时候,他只是失踪了

@RestController
public class NoteController {

…比他的notecontroller级别高。 @RestController 告诉spring底层类是rest端点bean。
@restcontroller继承自@controller,请参阅spring boot doc:
示例类的第一个注解是@restcontroller。这被称为原型注解。它为阅读代码的人和spring提供了类扮演特定角色的提示。在本例中,我们的类是一个web@controller,因此spring在处理传入的web请求时会考虑它。
@requestmapping注解提供“路由”信息。它告诉spring,任何带有/path的http请求都应该Map到home方法。@restcontroller注解告诉spring将结果字符串直接呈现回调用者。

nbnkbykc

nbnkbykc3#

然后api甚至没有暴露出来。
如果您运行这个项目,您应该在日志中获得公开api的列表。例如
debug 9736---[main]s.w.s.m.m.a.requestmappinghandlermapping:requestmappinghandlermapping中的19个Map
如果您在intellij上使用spring boot actuator:您可以在endpoints选项卡中检查api列表,这将非常有用:

相关问题