有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC/Hibernate/MySQL 400错误请求错误

我正在使用Spring和Hibernate用Java构建一个博客。我似乎不知道发生了什么,但当我试图添加(保存)一篇文章时,我一直遇到一个错误的请求错误,我无法找出我在映射中的错误之处

错误消息: enter image description here

控制器:

@Controller
@RequestMapping("/blog")
public class IndexController {

@Autowired
private PostService postService;

@RequestMapping("/list")
public String showPage (Model theModel) {

    // get posts from DAO

    List<Post> thePosts = postService.getAllPosts();

    // add the posts to the model

    theModel.addAttribute("allPosts", thePosts);
    return "allPosts";
}

@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {

    //create model attribute to bind form data
    Post thePost = new Post();

    theModel.addAttribute("post", thePost);

    return "postSuccess";
}

@PostMapping("/savePost")
public String savePost(@ModelAttribute("post") Post thePost) {
    // save the post using our service

    postService.savePost(thePost);
    return "allPosts";
}

表单片段:

 <div class="table" id="container">

            <form:form action="savePost" modelAttribute="post" 
 method="POST">

                <table>
                    <tbody>
                    <tr>
                        <td><label>Title:</label></td>
                        <td><form:input path="title" /></td>
                    </tr>

                    <tr>
                        <td><label>Author:</label></td>
                        <td><form:input path="author" /></td>
                    </tr>

                    <tr>
                        <td><label>Date:</label></td>
                        <td><form:input path="date" /></td>
                    </tr>

                    <tr>
                        <td><label>Post:</label></td>
                        <td><form:input path="post" /></td>
                    </tr>

                    <tr>
                        <td><label></label></td>
                        <td><input type="submit" value="Save"></td>
                    </tr>

                    </tbody>
                </table>
            </form:form>
            <div style="clear: both;"></div>
            <p>
                <a href="${pageContext.request.contextPath}/">Back to Home Page</a>
            </p>
    </div>

到目前为止,所有其他页面都正常工作,只是无法添加实际的博客文章。非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    我发现了这一点,这与我过去发行的另一期春季杂志很相似

    我不认为这真的遵循了很多传统的功能/设计理论,但我在控制器中添加了一些代码,现在它可以工作了。我可以很容易地添加帖子

    首先,我从“savePost”方法中删除了@modeldattribute标记。然后我将@RequestParam添加到我的方法参数中。添加了一点逻辑,现在它保存到数据库中,然后出现在博客上。好东西

    代码:

    @PostMapping("/savePost")
    public String savePost(@RequestParam("author") String author,
                           @RequestParam("title") String title, @RequestParam("date") String date,
                           @RequestParam("post") String post) throws ParseException {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date theDate = sdf.parse(date);
    
        // save the customer using our service
        Post thePost = new Post();
        thePost.setAuthor(author);
        thePost.setDate(theDate);
        thePost.setTitle(title);
        thePost.setPost(post);
    
        postService.addPost(thePost);
    
        System.out.println(thePost.toString()); //testing
        return "success";
    }
    

    jsp:

    <form:form action="savePost" modelAttribute="post" method="POST">
    
                    <table>
                        <tbody>
                        <tr>
                            <td><label>Title:</label></td>
                            <td><input id="title" type="text" name="title"></td>
                        </tr>
    
                        <tr>
                            <td><label>Author:</label></td>
                            <td><input id="author" type="text" name="author"></td>
                        </tr>
    
                        <tr>
                            <td><label>Date:</label></td>
                            <td><input id="date" type="text" name="date"></td>
                        </tr>
    
                        <tr>
                            <td><label>Post:</label></td>
                            <td><textarea id="post" type="text" 
      name="post"></textarea></td>
                        </tr>
    
                        <tr>
                            <td><label></label></td>
                            <td><input type="submit"  value="Save"></td>
                        </tr>
    
                        </tbody>
                    </table>
                </form:form>