我试图获得一个百万美元的按钮,当点击类似于c url的工作方式时,转到mvc控制器
<td><a class="btn btn-success" href="<c:url value="/displayWikis" />">  Show Wiki List  </a></td>
目前我的代码如下在我的百万富翁页面中
<td><button type="button" th:href="@{/getAll}">Get All Post </button></td>
这是百里香的页面
<!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]
答案 0 :(得分:0)
我的错误&#34; BindingResult和bean名称的简单目标对象&#34;问题是我的表单期待一个post对象,但是在getAll方法中我没有提供一个新的post对象。要在这里解决错误
@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);
Post newpost = new Post();
mav.addObject("post", newpost);
return mav;
}