Spring c:url的Thymeleaf等价物
我试图让一个thymeleaf按钮在点击时转到mvc控制器,类似于c url的工作方式Spring c:url的Thymeleaf等价物,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,我试图让一个thymeleaf按钮在点击时转到mvc控制器,类似于c url的工作方式 <td><a class="btn btn-success" href="<c:url value="/displayWikis" />">  Show Wiki List  </a></td> 目前,我的代码如下所示 <td><button type="b
<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>
Get All Post
这是百里香广告页面
<!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>
MVC主页
link href=“../static/css/bootstrap theme.css”
th:href=“@{css/bootstrap theme.css}”rel=“stylesheet”media=“screen”/
形式
用户名:
城市:
内容:
投递
发帖
主题
日期
创建
张贴数据
看府绸
函数getAllByUser(){
$.ajax({
键入:“获取”,
url:“https://localhost:8443/api/getAllForUser",
cache:false,
数据:“user=”+$(“#userAcctName”).val(),
成功:功能(响应){
警报(响应);
//如何将此响应数据传递给${postsList}
$('#test').html(“+response[0].subject”);
$(“.card”).children().removeClass('hidden');
$('[data toggle=“popover”]')。popover();
},
错误:函数(){
警报(“请求时出错…”;
}
});
}
$(文档).ready(函数(){
$('[data toggle=“popover”]')。popover();
//$(“.bg success”).children().addClass('hidden');
});
这是控制器代码
@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;
}
}
@控制器
公共类后置控制器{
私人最终邮政服务;
@注入
公共邮政控制器(最终邮政服务){
这个.postService=postService;
}
@RequestMapping(value=“/”,method=RequestMethod.GET)
公共字符串home(){
返回“家”;
}
@RequestMapping(value=“/mvchome”,method=RequestMethod.GET)
公共字符串mvchome(模型){
Post Post=新Post();
model.addAttribute(“post”,post);
返回“mvchome”;
}
@RequestMapping(value=“/mvchome”,method=RequestMethod.POST)
//创建公共字符串(@Valid Post,BindingResult,Model Model){
公共模型和视图创建(@Valid Post){
//if(bindingResult.hasErrors()){
//返回“mvchome”;
// }
//Post Post=新发布(0,1,0,内容,内容,新日期(),用户帐户名,城市,空,空,空);
post.setParentId(0);
post.setDisplayOrder(1);
职位设置级别(0);
setSubject(post.getContent());
post.setCreated(新日期());
post.setLatitude(空);
post.setlongtude(空);
设置后温度(空);
postService.savePost(post);
ModelAndView mav=新的ModelAndView(“mvchome”);
mav.addObject(“postsList”,post);
Post newpost=新Post();
mav.addObject(“post”,newpost);
返回mav;
}
@请求映射(value=“/getAll”)
//创建公共字符串(@Valid Post,BindingResult,Model Model){
公共模型和视图getAll(){
//if(bindingResult.hasErrors()){
//返回“mvchome”;
// }
List postList=postService.findAllPosts();
ModelAndView mav=新的ModelAndView(“mvchome”);
mav.addObject(“postsList”,postList);
返回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]
23:50:25.592[http-nio-8443-exec-10]ERROR org.thymeleaf.TemplateEngine-[thymeleaf][http-nio-8443-exec-10]异常处理模板“mvchome”:处理器“org.thymeleaf.spring4.processor.attr.springInputGeneralFieldDattrProcessor”(mvchome:44)执行期间出错
23:50:25.593[http-nio-8443-exec-10]ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost]./.[dispatcherServlet].[dispatcherServlet]-Servlet.service()在路径为[]的上下文中为Servlet[dispatcherServlet]抛出异常[请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring4.processor.attr.springInputGeneralFieldDattrProcessor'(mvchome:44)]时出错,原因为根本原因
java.lang.IllegalStateException:bean名称“post”的BindingResult和普通目标对象都不能作为请求属性使用
在org.springframework.web.servlet.support.BindStatus.(BindStatus.java:144)~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
对于我的错误“BindingResult或bean name的普通目标对象都不是”,问题是我的表单需要一个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;
}
@RequestMapping(value=“/getAll”)
//创建公共字符串(@Valid Post,BindingResult,Model
//模型){
公共模型和视图getAll(){
//if(bindingResult.hasErrors()){
//返回“mvchome”;
// }
List postList=postService.findAllPosts();
ModelAndView mav=新的ModelAndView(“mvchome”);
mav.addObject(“postsList”,postList);
Post newpost=新Post();
mav.addObject(“post”,newpost);
返回mav;
}
您的代码就是您问题的答案:)
我以前尝试过,但我遇到了一个错误。它告诉您需要添加什么BindingResult
。因此,将控制器方法更改为公共模型和视图创建(@Valid Post,BindingResult)