简体   繁体   中英

Spring MVC/Hibernate/MySQL 400 Bad Request Error

I'm building a blog in Java using Spring and Hibernate. I can't seem to figure out what is going on but I keep running into a Bad Request error when I try to add (save) a post and I can't figure out where I am wrong in my mapping.

Error message: 在此处输入图片说明

Controller:

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

Form snippet:

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

All other pages are working correctly so far, just can't add an actual blog post. Any help is greatly appreciated.

I figured this out and it is similar to another spring issue I had in the past.

I don't think this really follows a lot of conventional function/design theory, but I added some code into the controller and it now works. I can add a post easily.

First thing was, I removed the @ModelAttribute tag from my "savePost" method. Then I added @RequestParam to my method parameters. Added a little bit of logic and now it saves to the database and then appears on the blog. Good stuff.

Code:

@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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM