views:

57

answers:

2

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

}
+1  A: 

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.

James Earl Douglas
+1  A: 

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.

JacobM