在 Spring 3.0 注释中,您可以将博客发布方法指定为 url。但是在那个版本的 Spring 之前,这是实现它的最佳方法吗:
@SuppressWarnings("unchecked")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse arg1) throws Exception {
ModelAndView mav = handleRequestInternal(request, arg1);
if (!Utils.loggedIn(request, arg1)) {
return new ModelAndView(new RedirectView("login.html"));
}
String id = Utils.getLoggedInUserId(request);
Key objectkey = KeyFactory.createKey(Admin.class.getSimpleName(), id);
//Admin user = userService.getAdmin(objectkey);
//System.out.println("admin key "+user.getId());
if (request.getMethod().equalsIgnoreCase("post")) {
String title = request.getParameter("title");
String content = request.getParameter("content");
if (!title.isEmpty()&&!content.isEmpty()) {
BlogPost post = new BlogPost();
post.setTitle(title);
post.setContent(new Text(content));
post.setDate(new Date());
post.setUser(objectkey);
postService.storePost(post);
}
}
return new ModelAndView(new RedirectView("news.html"));
}
最佳答案
实际上,pre-3.0 had this as well .这个和你的例子的区别不是Spring版本,而是使用注解MVC配置。
您的解决方案看起来很实用,但您会考虑迁移到基于注解的配置吗? Here is an example它可以多么简单。
关于java - request.getMethod().equalsIgnoreCase ("post") 在 (pre-Spring-3.0) Spring 博客应用程序中是个坏主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3524077/