以上就是给各位分享如何使用Spring和Thymeleaf在下拉列表中显示所有可能的枚举值?,其中也会对下拉列表java进行解释,同时本文还将给你拓展java–SpringBoot和Thymeleaf
以上就是给各位分享如何使用Spring和Thymeleaf在下拉列表中显示所有可能的枚举值?,其中也会对下拉列表java进行解释,同时本文还将给你拓展java – Spring Boot和Thymeleaf 3.0.0.RELEASE集成、Spring Boot (4) 静态页面和Thymeleaf模板、spring boot使用thymeleaf跳转页面实例代码my thymeleaf indexpage、Spring MVC 4和Thymeleaf-防止页面刷新等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- 如何使用Spring和Thymeleaf在下拉列表中显示所有可能的枚举值?(下拉列表java)
- java – Spring Boot和Thymeleaf 3.0.0.RELEASE集成
- Spring Boot (4) 静态页面和Thymeleaf模板
- spring boot使用thymeleaf跳转页面实例代码my thymeleaf indexpage
- Spring MVC 4和Thymeleaf-防止页面刷新
如何使用Spring和Thymeleaf在下拉列表中显示所有可能的枚举值?(下拉列表java)
我有一个具有枚举属性的域对象,并且我想以该对象的形式显示一个包含所有可能的枚举值的下拉列表。想象以下对象:
public class Ticket { private Long id; private String title; private State state; // Getters & setters public static enum State { OPEN, IN_WORK, FINISHED }}
在我的控制器中,我有一个方法呈现此对象的形式:
@RequestMapping("/tickets/new")public String showNewTicketForm(@ModelAttribute Ticket ticket) { return "tickets/new";}
模板如下所示:
<form th:action="@{/tickets}" method="post" th:object="${ticket}"> <input type="text" th:field="*{title}" /> <select></select></form>
稍后,应将其转换为如下形式:
<form action="/tickets" method="post"> <input type="text" name="title" /> <select name="state"> <option>OPEN</option> <option>IN_WORK</option> <option>FINISHED</option> </select></form>
如何创建选择标签?所选值还应该自动映射到票证,以便我可以在控制器中执行以下操作:
@RequestMapping(value = "/tickets", method = RequestMethod.POST)public String createTicket(@Valid Ticket ticket) { service.createTicket(ticket); return "redirect:/tickets";}
答案1
小编典典您可以这样做:
<select> <option th:each="state : ${T(com.mypackage.Ticket.State).values()}" th:value="${state}" th:text="${state}"> </option></select>
java – Spring Boot和Thymeleaf 3.0.0.RELEASE集成
@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
并添加我自己的SpringWebConfig配置.
不幸的是收到这样的错误:
java.lang.classNotFoundException: org.thymeleaf.resourceresolver.IResourceResolver at java.net.urlclassloader.findClass(urlclassloader.java:381) ~[na:1.8.0_66] at java.lang.classLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_66] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_66] at java.lang.classLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_66] ... 37 common frames omitted Wrapped by: java.lang.NoClassDefFoundError: org/thymeleaf/resourceresolver/IResourceResolver wrapped by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration due to org/thymeleaf/resourceresolver/IResourceResolver not found. M ake sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
解决方法
http://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#howto-use-thymeleaf-3
<properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version> </properties>
Spring Boot (4) 静态页面和Thymeleaf模板
静态页面
spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下
/static
/public
/resources
/META-INF/resources
在resources建立一个static目录和index.html静态文件,访问地址就是 http://localhost:8080/index.html
动态页面
动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问jsp,spring boot建议不要使用jsp,默认使用Thymeleaf来做动态页面。
在pom.xml中添加Thymeleaf组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
TemplatesController.java
@Controller //返回的是页面所以用简单的controller
public class TemplatesController {
@GetMapping("/templates")
String test(HttpServletRequest request) {
request.setAttribute("key", "hello world");
//跳转到templates/index.html动态页面,templates目录是spring boot 默认配置的动态页面路径
return "index";
}
}
templates/index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${key}"></span>
</body>
</html>
访问http://localhost:8088/dev/templates
这是一个最基本的像页面传递参数,templates标签和jsp标签一样,也可以实现条件判断,循环等各种功能,建议使用静态html+rest代替动态页面。
遍历数据:
@GetMapping("/template")
String template(HttpServletRequest request){
request.setAttribute("key",bookName + bookAuthor);
request.setAttribute("list", Arrays.asList("a","b","c","d","e"));
return "index";
}
遍历
<table>
<tr th:each="item : ${list}">
<td th:text="${item}"></td>
</tr>
</table>
数据判断:
<tr th:each="item : ${list}">
<td th:text="${item}" th:if="${item == ''a''}"></td>
</tr>
等于a才显示出来
<table th:if="${list != null && list.size() > 0}">
有数据才遍历
在javascript中访问model数据
<script>
var a = ''[[${key}]]'';
alert(a);
</script>
spring boot使用thymeleaf跳转页面实例代码my thymeleaf indexpage
本篇文章主要介绍了spring boot使用thymeleaf跳转页面,实例介绍了thymeleaf的原理和介绍,有兴趣的可以了解一下。
前言
在学习springboot 之后想结合着html做个小demo,无奈一直没掌握窍门,在多番的搜索和尝试下终于找到了配置的方法,使用thymeleaf做事前端页面模板,不能使用纯html.
thymeleaf介绍
Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎。
Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏览器中正确显示HTML,还可以作为静态原型工作,从而在开发团队中进行更强大的协作。
使用Spring Framework的模块,与您最喜爱的工具进行大量集成,以及插入自己的功能的能力,Thymeleaf是现代HTML5 JVM Web开发的理想选择,尽管它可以做的更多。
实战
项目结构
thymeleaf pom依赖
org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-thymeleaf
模板页面
注意使用模板作为页面时候必须要把模板页面放在templates文件夹下
index.html
demo
my thymeleaf indexpage
更多详情controller
@Controller public class PageController { @RequestMapping("/page") public String page3(Model model){ model.addAttribute("userName","张三"); return "hello"; } @RequestMapping("info/more") public String page2(){ return "hello2"; } @RequestMapping("sys/index") public String page(){ return "sys/index"; } }
测试
点击更多详情
项目源码: github地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小编。
Spring MVC 4和Thymeleaf-防止页面刷新
我保证,我已经彻底解决了这个问题。
我有一个使用Thymeleaf收集表单数据并将其放入数据库的Spring MVC
4应用程序。效果很好,除了我希望我的应用程序在用户单击“提交”按钮之后将其留在表单页面上,以便他们可以继续进行编辑。
每次单击提交时,都会调用Controller并返回视图,这将刷新页面。我想消除刷新,并添加警报以确认保存。
因此,我开始着手将表单提交转换为AJAX,并意识到这将破坏使用Thymeleaf的目的。Thymeleaf已经将表单字段映射到支持bean,并且效果很好,那么为什么要替换该机制呢?
那么,有没有办法让Spring MVC控制器处理提交请求但不返回视图的新实例?
我尝试不返回视图,但这会导致错误,因为未将任何内容映射到model属性。我尝试使用@ResponseBody并返回数据,但这导致JSON在网页中呈现,而且我不知道如何获取该数据并对其进行处理。
我也试图找到一种挂接到提交按钮的方法,以尝试调用javascript preventDefault(),但是没有运气。
非常感谢您的建议…
这是我的控制器代码,非常简单:
@SessionAttributes("adminFormAjax")@Controllerpublic class TestController { @Autowired private AdminDataRepository rep; @RequestMapping(value="/admin_ajax", method=RequestMethod.GET) public String adminFormAjax(Model model) { AdminData ad = rep.findById(1L); // If there is no configuration record, create one and assign the primary key as 1. if(ad == null) { ad = new AdminData(); ad.setId(1L); } model.addAttribute("adminFormAjax", ad); return "adminFormAjax"; } @RequestMapping(value="/admin_ajax", method=RequestMethod.POST) public @ResponseBody AdminData adminSubmit(@ModelAttribute("adminFormAjax") AdminData ad, Model model) { // rep.save(ad); model.addAttribute("adminFormAjax", ad); return ad; }}
答案1
小编典典因此,如果您希望能够在不更改页面的情况下将表单数据提交给控制器,则thymeleaf会将页面呈现为HTML并将其发送给客户端以进行显示,则需要合并Javascript。
您绝对需要使用javascript / ajax发布表单内容并保持页面原样。
例如,如果您只想更新包含表单的部分,则可以调用控制器方法,该方法返回一个片段并显示包含相关表单信息的片段。
关于如何使用Spring和Thymeleaf在下拉列表中显示所有可能的枚举值?和下拉列表java的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于java – Spring Boot和Thymeleaf 3.0.0.RELEASE集成、Spring Boot (4) 静态页面和Thymeleaf模板、spring boot使用thymeleaf跳转页面实例代码my thymeleaf indexpage、Spring MVC 4和Thymeleaf-防止页面刷新等相关内容,可以在本站寻找。
本文标签: