mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Add update/delete to the web-ui sample
Closes gh-4735 See gh-4733
This commit is contained in:
parent
ffbdfc586b
commit
0de4b6ce63
5
spring-boot-samples/spring-boot-sample-web-ui/src/main/java/sample/web/ui/InMemoryMessageRepository.java
Normal file → Executable file
5
spring-boot-samples/spring-boot-sample-web-ui/src/main/java/sample/web/ui/InMemoryMessageRepository.java
Normal file → Executable file
@ -50,4 +50,9 @@ public class InMemoryMessageRepository implements MessageRepository {
|
||||
return this.messages.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteMessage(Long id) {
|
||||
this.messages.remove(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
2
spring-boot-samples/spring-boot-sample-web-ui/src/main/java/sample/web/ui/MessageRepository.java
Normal file → Executable file
2
spring-boot-samples/spring-boot-sample-web-ui/src/main/java/sample/web/ui/MessageRepository.java
Normal file → Executable file
@ -24,4 +24,6 @@ public interface MessageRepository {
|
||||
|
||||
Message findMessage(Long id);
|
||||
|
||||
void deleteMessage(Long id);
|
||||
|
||||
}
|
||||
|
13
spring-boot-samples/spring-boot-sample-web-ui/src/main/java/sample/web/ui/mvc/MessageController.java
Normal file → Executable file
13
spring-boot-samples/spring-boot-sample-web-ui/src/main/java/sample/web/ui/mvc/MessageController.java
Normal file → Executable file
@ -30,6 +30,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @author Doo-Hwan Kwak
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
@ -73,4 +74,16 @@ public class MessageController {
|
||||
throw new RuntimeException("Expected exception in controller");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "delete/{id}")
|
||||
public ModelAndView delete(@PathVariable("id") Long id) {
|
||||
this.messageRepository.deleteMessage(id);
|
||||
Iterable<Message> messages = this.messageRepository.findAll();
|
||||
return new ModelAndView("messages/list", "messages", messages);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "modify/{id}", method = RequestMethod.GET)
|
||||
public ModelAndView modifyForm(@PathVariable("id") Message message) {
|
||||
return new ModelAndView("messages/form", "message", message);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,10 @@
|
||||
Messages
|
||||
</a>
|
||||
</div>
|
||||
<input type="hidden"
|
||||
th:field="*{id}"
|
||||
th:class="${#fields.hasErrors('id')} ? 'field-error'"
|
||||
/>
|
||||
<label for="summary">Summary</label>
|
||||
<input type="text"
|
||||
th:field="*{summary}"
|
||||
@ -35,7 +39,7 @@
|
||||
th:field="*{text}"
|
||||
th:class="${#fields.hasErrors('text')} ? 'field-error'"></textarea>
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Create"/>
|
||||
<input type="submit" value="Save"/>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
@ -37,6 +37,17 @@
|
||||
A detailed message that is longer than the summary.
|
||||
</dd>
|
||||
</dl>
|
||||
<div class="pull-left">
|
||||
<a href="messages"
|
||||
th:href="@{'/delete/' + ${message.id}}">
|
||||
delete
|
||||
</a>
|
||||
|
|
||||
<a href="form.html"
|
||||
th:href="@{'/modify/' + ${message.id}}">
|
||||
modify
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user