2121import org .springframework .context .support .ReloadableResourceBundleMessageSource ;
2222import org .springframework .stereotype .Controller ;
2323import org .springframework .ui .Model ;
24+ import org .springframework .validation .BindingResult ;
2425import org .springframework .validation .Errors ;
2526import org .springframework .web .bind .annotation .GetMapping ;
2627import org .springframework .web .bind .annotation .ModelAttribute ;
2728import org .springframework .web .bind .annotation .PathVariable ;
2829import org .springframework .web .bind .annotation .PostMapping ;
2930import org .springframework .web .bind .annotation .RequestMapping ;
3031import org .springframework .web .bind .annotation .RequestPart ;
32+ import org .springframework .web .bind .annotation .SessionAttributes ;
3133import org .springframework .web .multipart .MultipartFile ;
3234import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
3335import services .PostService ;
4042 */
4143@ Controller ("AdminPostController" )
4244@ RequestMapping ("/admin/posts" )
45+ @ SessionAttributes ({PostController .ATTRIBUTE_NAME })
4346public class PostController {
4447
4548 private static Logger logger = LoggerFactory .getLogger (PostController .class );
4649
50+ public static final String ATTRIBUTE_NAME = "post" ;
51+ public static final String BINDING_RESULT_NAME = "org.springframework.validation.BindingResult." + ATTRIBUTE_NAME ;
52+
4753 @ Autowired
4854 private PostService postService ;
4955 @ Autowired
@@ -59,11 +65,14 @@ public String all(@CurrentUser User activeUser, Model model){
5965
6066 @ GetMapping ("/edit/{postId}" )
6167 public String showUpdatePostForm (@ PathVariable Long postId , Model model ) {
62- Post post = postService .findById (postId );
63- if (post == null ) {
64- throw new PostNotFoundException ();
68+ /* if "fresh" GET (ie, not redirect w validation errors): */
69+ if (!model .containsAttribute (BINDING_RESULT_NAME )) {
70+ Post post = postService .findById (postId );
71+ if (post == null ) {
72+ throw new PostNotFoundException ();
73+ }
74+ model .addAttribute (ATTRIBUTE_NAME , post );
6575 }
66- model .addAttribute ("post" , post );
6776 return "admin/post/edit" ;
6877 }
6978
@@ -88,19 +97,26 @@ public String processDelete(@ModelAttribute Post post, RedirectAttributes model)
8897
8998 @ GetMapping ("/create" )
9099 public String showCreatePostForm (Model model ){
91- model .addAttribute ("post" , new Post ());
100+ model .addAttribute (ATTRIBUTE_NAME , new Post ());
92101 return "admin/post/create" ;
93102 }
94103
95104 @ PostMapping ("/save" )
96- public String processPost (@ RequestPart ("postImage" ) MultipartFile postImage , @ ModelAttribute @ Valid Post post , Errors errors ,
97- @ CurrentUserAttached User activeUser , RedirectAttributes model ) throws IOException , SQLException {
105+ public String processPost (
106+ @ RequestPart ("postImage" ) MultipartFile postImage ,
107+ @ ModelAttribute (ATTRIBUTE_NAME ) @ Valid Post post ,
108+ BindingResult bindingResult ,
109+ @ CurrentUserAttached User activeUser ,
110+ RedirectAttributes model ) throws IOException , SQLException {
98111
99- if (errors .hasErrors ()){
100- return "admin/post/create" ;
112+ String url = "redirect:/admin/posts/all" ;
113+
114+ if (bindingResult .hasErrors ()) {
115+ model .addFlashAttribute (BINDING_RESULT_NAME , bindingResult );
116+ return url ;
101117 }
102-
103- if (postImage != null && !postImage .isEmpty ()){
118+
119+ if (postImage != null && !postImage .isEmpty ()) {
104120 logger .info ("Añadiendo información de la imagen" );
105121 FileImage image = new FileImage ();
106122 image .setName (postImage .getName ());
@@ -109,19 +125,17 @@ public String processPost(@RequestPart("postImage") MultipartFile postImage, @Mo
109125 image .setContent (postImage .getBytes ());
110126 post .setImage (image );
111127 }
112-
128+
113129 post .setAuthor (activeUser );
114- if (post .getId () == null ){
115- logger .info ("Creando Nuevo Post" );
130+ if (post .getId () == null ) {
116131 postService .create (post );
117- }else {
118- logger .info ("Editando Post" );
132+ } else {
119133 postService .edit (post );
120134 }
121-
135+
122136 List <String > successMessages = new ArrayList ();
123- successMessages .add (messageSource .getMessage ("message.post.save.success" , new Object [] {post .getId ()}, Locale .getDefault ()));
124- model .addFlashAttribute ("successFlashMessages" , successMessages );
125- return "redirect:/admin/posts/all" ;
137+ successMessages .add (messageSource .getMessage ("message.post.save.success" , new Object []{post .getId ()}, Locale .getDefault ()));
138+ model .addFlashAttribute ("successFlashMessages" , successMessages );
139+ return url ;
126140 }
127141}
0 commit comments