0
我試圖讓thymeleaf按鈕轉到一個MVC控制器點擊類似Thymeleaf當量C:網址
<td><a class="btn btn-success" href="<c:url value="/displayWikis" />">  Show Wiki List  </a></td>
怎樣的C網址工作,目前我的代碼是在我的thymeleaf頁如下時
<td><button type="button" th:href="@{/getAll}">Get All Post </button></td>
這是thymelead頁
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>MVC Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="../static/css/bootstrap.css" th:href="@{css/bootstrap.css}"
rel="stylesheet" media="screen" />
link href="../static/css/bootstrap-theme.css"
th:href="@{css/bootstrap-theme.css}" rel="stylesheet" media="screen" />
<script type="text/javascript" src="../static/js/jquery-2.2.2.js"
th:src="@{js/jquery-2.2.2.js}"></script>
<script type="text/javascript" src="../static/js/tether.js"
th:src="@{js/tether.js}"></script>
<script type="text/javascript" src="../static/js/bootstrap.js"
th:src="@{js/bootstrap.js}"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Form</h1>
<form action="#" th:action="@{/mvchome}" th:object="${post}"
method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" th:field="*{userAcctName}" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" th:field="*{city}" /></td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" th:field="*{content}" /></td>
</tr>
<tr>
<td><button type="submit" name="action" value="save">Submit post</button></td>
<td><button type="button" th:href="@{/getAll}">Get All Post </button></td>
</tr>
</table>
</form>
</div>
<div>
<!-- <button type="button" th:href="@{/getAll}"> Get All </button> -->
</div>
<!-- Results Block -->
<th:block th:each="post : ${postsList}">
<div class="card ">
<div class="card-block">
<h4 class="card-title" id="test" th:text="${post.subject}">Subject</h4>
<h6 class="card-subtitle text-muted" th:text="${post.created}">Date
Created</h6>
</div>
<div class="card-block">
<p class="card-text" th:text="${post.content}">Post Data</p>
<a href="#" class="card-link"></a>
<button type="button" class="btn btn-sm btn-danger"
data-toggle="popover" title="Popover title"
data-content="th:text='${post.city}'">See Popover</button>
</div>
<div class="card-footer text-muted" th:text="${post.temperature}"></div>
</div>
</th:block>
</div>
<script>
function getAllByUser() {
$.ajax({
type : "get",
url : "https://localhost:8443/api/getAllForUser",
cache : false,
data : 'user=' + $("#userAcctName").val(),
success : function(response) {
alert(response);
//some how pass this response data to ${postsList}
$('#test').html("" + response[0].subject);
$(".card").children().removeClass('hidden');
$('[data-toggle="popover"]').popover();
},
error : function() {
alert('Error while request..');
}
});
}
$(document).ready(function() {
$('[data-toggle="popover"]').popover();
// $(".bg-success").children().addClass('hidden');
});
</script>
</body>
</html>
這裏的控制器代碼
@Controller
public class PostController {
private final PostService postService;
@Inject
public PostController(final PostService postService){
this.postService=postService;
}
@RequestMapping(value="/", method = RequestMethod.GET)
public String home() {
return "home";
}
@RequestMapping(value="/mvchome", method = RequestMethod.GET)
public String mvchome(Model model) {
Post post = new Post();
model.addAttribute("post", post);
return "mvchome";
}
@RequestMapping(value = "/mvchome", method=RequestMethod.POST)
//public String create(@Valid Post post, BindingResult bindingResult, Model model) {
public ModelAndView create(@Valid Post post) {
// if (bindingResult.hasErrors()) {
// return "mvchome";
// }
//Post post = new Post(0, 1, 0, content,content, new Date(),userAcctName,city, null, null, null);
post.setParentId(0);
post.setDisplayOrder(1);
post.setIndentLevel(0);
post.setSubject(post.getContent());
post.setCreated(new Date());
post.setLatitude(null);
post.setLongtitude(null);
post.setTemperature(null);
postService.savePost(post);
ModelAndView mav = new ModelAndView("mvchome");
mav.addObject("postsList", post);
Post newpost = new Post();
mav.addObject("post", newpost);
return mav;
}
@RequestMapping(value = "/getAll")
//public String create(@Valid Post post, BindingResult bindingResult, Model model) {
public ModelAndView getAll() {
// if (bindingResult.hasErrors()) {
// return "mvchome";
// }
List<Post> postList=postService.findAllPosts();
ModelAndView mav = new ModelAndView("mvchome");
mav.addObject("postsList", postList);
return mav;
}
}
我已經四處搜索,到目前爲止我找不到類似的功能。 任何一個人都可以指向正確的方向嗎?然而
感謝
****緊隨其後的建議,現在我得到這個錯誤
23:50:25.592 [http-nio-8443-exec-10] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8443-exec-10] Exception processing template "mvchome": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (mvchome:44)
23:50:25.593 [http-nio-8443-exec-10] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (mvchome:44)] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'post' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
你的代碼是你的問題:)'的答案Show Wiki List' – sanluck
我試過以前,但我得到一個錯誤 – user2184653
那麼,它會告訴你你需要什麼添加'BindingResult'。所以改變你的控制器方法到'公共ModelAndView創建(@Valid Post帖子,BindingResult結果)' – sanluck