我已经创建了一个用于创建博客文章的应用。现在,我正在尝试更新应用程序以执行CRUD功能。原始应用程序仍在工作,其中包括一个屏幕截图。
通过他们我可以登录和注销,或者可以创建新帖子。当我选择一个给定的用户名时,应按用户名列出所有帖子。见下文:
从这里我可以选择一个特定的博客文章,该博客文章将按用户名及其名称拉出该博客文章。另外,在此屏幕中,我修改了百里香叶以允许我编辑博客文章或删除博客文章。见下文:
当我单击“编辑”时,这是该操作的结果。我想做的是将信息从单个帖子页面传递到新的百里香模板中,并允许对信息进行编辑。完成编辑的信息后,继续并重新保存信息。见下文:
Blog posts edit by username and uid
我想知道我做错了什么。我将包含我的postcontroller.java,editpost.html,post.html,postdao.java。
package org.launchcode.blogz.controllers;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.launchcode.blogz.models.Post;
import org.launchcode.blogz.models.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class PostController extends AbstractController {
@RequestMapping(value = "/blog/newpost", method = RequestMethod.GET)
public String newPostForm() {
return "newpost";
}
@RequestMapping(value = "/blog/newpost", method = RequestMethod.POST)
public String newPost(HttpServletRequest request, Model model) {
// TODO - implement newPost
// get request parameters
String title = request.getParameter("title");
String body = request.getParameter("body");
// get user from their session
HttpSession newSession = request.getSession();
User author = getUserFromSession(newSession);
// validate parameters
// if valid, create new Post
// if not valid, send back to the form, with an error message
if (title == null || title.isEmpty()) {
model.addAttribute("error", "The title is missing from this post. Please enter a title.");
return "newpost";
}
if (body == null || body.isEmpty()) {
model.addAttribute("title", title);
model.addAttribute("error", "The body of the post needs some content. Please enter some content for this post.");
return "newpost";
}
// save the user's post
Post post = new Post(title, body, author);
postDao.save(post);
// TODO - this redirect should go to the new post's page
return "redirect:/blog/" + author.getUsername() + "/" + post.getUid();
}
@RequestMapping(value = "/blog/{username}/{uid}", method = RequestMethod.GET)
public String singlePost(@PathVariable String username, @PathVariable int uid, Model model) {
// TODO - implement singlePost
// get the given parameters
Post post = postDao.findByUid(uid);
// get the given post
// pass the post into the template
model.addAttribute("error", "");
model.addAttribute("post", post);
return "post";
}
@RequestMapping(value = "/blog/{username}", method = RequestMethod.GET)
public String userPosts(@PathVariable String username, Model model) {
// TODO - implement userPosts
// get all of the user's posts
User user = userDao.findByUsername(username);
List<Post> posts = user.getPosts();
// pass the posts into the template
model.addAttribute("posts", posts);
return "blog";
}
@RequestMapping(value = "/blog/editpost/{username}/{uid}", method = RequestMethod.GET)
public String editPostForm(@PathVariable String username, @PathVariable int uid, Model model){
return "editPost";
}
@RequestMapping(value = "/blog/editpost/{username}/{uid}", method = RequestMethod.POST)
public String editPost(HttpServletRequest request, @PathVariable String username, @PathVariable int id, Model model) {
// TODO - implement newPost
// get request parameters
String title = request.getParameter("title");
String body = request.getParameter("body");
// get user from their session
HttpSession newSession = request.getSession();
User author = getUserFromSession(newSession);
// validate parameters
// if valid, create new Post
// if not valid, send back to the form, with an error message
if (title == null || title.isEmpty()) {
model.addAttribute("error", "The title is missing from this post. Please enter a title.");
return "editPost";
}
if (body == null || body.isEmpty()) {
model.addAttribute("title", title);
model.addAttribute("error", "The body of the post needs some content. Please enter some content for this post.");
return "editPost";
}
// save the user's post
Post post = new Post(title, body, author);
postDao.save(post);
// TODO - this redirect should go to the new post's page
return "redirect:/blog/" + author.getUsername() + "/" + post.getUid();
}
// @RequestMapping(value = "/blog/deletepost")
// public String deleteByUid(@PathVariable("uid") long id, Model model) {
// String deleteSQL = "DELETE post WHERE uid = ${post.uid}";
// PreparedStatement preparedStatement = dbConnection.prepareStatement(deleteSQL);
// // execute delete SQL stetement
// preparedStatement.executeUpdate();
// return "index";
// }
}
上面的代码是我的控制器
下一个是我的editpost.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head th:replace="template :: dochead">
</head>
<body>
<div th:replace="template :: navigation"></div>
<h2>Edit Post</h2>
<form method="post" th:object="$[post}">
<label>
<div>title</div>
<input type="text" name="title" class="post-title" th:text="${post.title}"></input>"post-title" th:text="${post.title}"
</label>
<label>
<div>post</div>
<textarea name="body" class="post-body" th:text="${post.body}"></textarea>
</label>
<div class="error" th:text="${error}"></div>
<button type="submit">Save</button>
</form>
</body>
</html>
下一个代码段将是我的post.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head th:replace="template :: dochead">
</head>
<body>
<div th:replace="template :: navigation"></div>
<h2>A Single Post</h2>
<!-- TODO - display a single blog post -->
<h3 class="post-title" th:text="${post.title}"></h3>
<p class="post-body" th:text="${post.body}"></p>
<div class="error" th:text="${error}"></div>
<a href="@{/blog/editpost/${username}/${uid}">Edit</a>
<a href="/blog/deletepost">Delete</a>
<p class="footer">This post was created by <a th:href="'/blog/' + ${post.author.username}" th:text="${post.author.username}"></a> on the date <em><span th:text="${#dates.format(post.modified, 'yyyy-MM-dd hh:mm:ss')}"></span></em></p>
</body>
</html>
最后的代码段将是我的postdao.java
package org.launchcode.blogz.models.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.launchcode.blogz.models.Post;
import org.launchcode.blogz.models.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Transactional
@Repository
public interface PostDao extends CrudRepository<Post, Integer> {
List<Post> findByAuthor(User authorId);
// TODO - add method signatures as needed
Post findByUid(int uid);
List<Post> findAll();
void deleteByUid(int uid);
}
我希望有人能帮助我,对此我将不胜感激。
答案 0 :(得分:0)
您的editpost.html出现错误。按下“编辑”按钮后,请注意您的浏览器URL,它是
localhost:8080 / blog / jfreynolds / @ {/ blog / editpost / $ {username} / $ {uid}
代替
localhost:8080 / blog / editpost / jreynolds / 2
只需在下面的href前面添加“ :”,即可使用您的表达式。
<a th:href="@{/blog/editpost/${username}/${uid}">Edit</a>