【问题标题】:Spring MVC/Hibernate/MySQL 400 Bad Request ErrorSpring MVC/Hibernate/MySQL 400 错误请求错误
【发布时间】:2017-11-16 00:24:35
【问题描述】:

我正在使用 Spring 和 Hibernate 用 Ja​​va 构建博客。我似乎无法弄清楚发生了什么,但是当我尝试添加(保存)帖子时,我一直遇到错误请求错误,并且我无法弄清楚我在映射中的错误所在。

错误信息:

控制器:

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

表格sn-p:

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

到目前为止,所有其他页面都可以正常工作,只是无法添加实际的博客文章。任何帮助是极大的赞赏。

【问题讨论】:

    标签: java hibernate jsp spring-mvc


    【解决方案1】:

    我发现了这一点,这与我过去遇到的另一个春季问题相似。

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

    首先,我从“savePost”方法中删除了@ModelAttribute 标签。然后我将@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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2017-12-20
      • 2016-04-20
      • 1970-01-01
      相关资源
      最近更新 更多