开发者

Is request.getMethod().equalsIgnoreCase("post") a bad idea in a (pre-Spring-3.0) Spring blog app?

开发者 https://www.devze.com 2023-01-12 20:02 出处:网络
In Spring 3.0 annotations allow you to specify the blog post method as a url. But prior to that version of Spring, is this the best way to achieve it:

In Spring 3.0 annotations allow you to specify the blog post method as a url. But prior to that version of Spring, is this the best way to achieve it:

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

}


Actually, pre-3.0 had this as well. The distinction between this and your example is not the Spring version, it's the use of annotation MVC configuration.

Your solution seems functional, but would you consider migrating to annotation-based configuration? Here is an example of how simple it can be.


If you know that a given page may contain a POST (i.e. a form submission), one approach is have your controller inherit from AbstractFormController, or SimpleFormController, and use or override the built-in isFormSubmission() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消