Skip to content

Commit 28d6ad4

Browse files
committed
fix(post): solve problems with update partials
1 parent 7f3f1d2 commit 28d6ad4

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/main/java/controllers/admin/PostController.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
2222
import org.springframework.stereotype.Controller;
2323
import org.springframework.ui.Model;
24+
import org.springframework.validation.BindingResult;
2425
import org.springframework.validation.Errors;
2526
import org.springframework.web.bind.annotation.GetMapping;
2627
import org.springframework.web.bind.annotation.ModelAttribute;
2728
import org.springframework.web.bind.annotation.PathVariable;
2829
import org.springframework.web.bind.annotation.PostMapping;
2930
import org.springframework.web.bind.annotation.RequestMapping;
3031
import org.springframework.web.bind.annotation.RequestPart;
32+
import org.springframework.web.bind.annotation.SessionAttributes;
3133
import org.springframework.web.multipart.MultipartFile;
3234
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
3335
import services.PostService;
@@ -40,10 +42,14 @@
4042
*/
4143
@Controller("AdminPostController")
4244
@RequestMapping("/admin/posts")
45+
@SessionAttributes({PostController.ATTRIBUTE_NAME})
4346
public 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
}

src/main/webapp/WEB-INF/templates/admin/fragment/alerts.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<div th:fragment="form-errors" class="alert alert-danger" th:if="${#fields.hasErrors('*')}">
1010
<ul>
11-
<li th:each="err: ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
11+
<li class="text-left" th:each="err: ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
1212
</ul>
1313
</div>
1414
<div th:fragment="flash-message" th:if="${not #lists.isEmpty(successFlashMessages)}" class="alert alert-success">

src/main/webapp/WEB-INF/templates/admin/fragment/post.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
<textarea id="bootstrap-editor" th:readonly="${delete}" th:field="*{body}" placeholder="Enter text ..." style="width:98%;height:200px;"></textarea>
6060
</div>
6161
</div>
62-
<input type="hidden" th:field="*{id}"/>
6362
<br />
6463
<div class="row">
6564
<div class="form-group col-xs-12">

0 commit comments

Comments
 (0)