jsp和thymeleaf jsp哪个好

Spring MVC : Java模板引擎 Thymeleaf (一)
在Java世界的MVC框架里,使用的视图技术不少,最基本的是JSP,还有知名的FreeMarker和Velocity等模板引擎。Thymeleaf也是一款优秀的模板引擎,它在HTML5/XHTML的视图层表现的很好,也能在离线情况下处理任何XML文件。它是完全可以替代JSP+JSTL的。下面是来自于Thymeleaf官方的Q&A:Q: 和FreeMarker,Velocity相比,Thymeleaf表现得怎样呢?A:FreeMarker和Velocity都是软件领域杰出的作品,但它们在解决模板问题上的处理哲学和Thymeleaf不一样。Thymeleaf强调的是自然模板,也就是允许模板作为产品原型使用(笔者注:因为其后缀名就是.html,可以直接在浏览器打开),而FreeMarker和Velocity不行。并且Thymeleaf的语法更简洁、更和目前Web开发的趋势相一致。其次,从架构的角度看,FreeMarker和Velocity更像个文本处理器,所以它们能够处理很多类型的内容,而Thymeleaf是基于XML的,只能处理XML格式的数据。因此这样看,FreeMarker和Velocity更通用些,但恰恰如此,Thymeleaf更能利用XML的特性,尤其是在Web应用中。下面就以Spring官方的例子为基础,给出一个使用Thymeleaf的基本MVC实现:(使用Maven构建)首先是pom.xml的清单:&?xml version=&1.0& encoding=&UTF-8&?&&project xmlns=&http://maven.apache.org/POM/4.0.0& xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance& xsi:schemaLocation=&http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&& &modelVersion&4.0.0&/modelVersion& &groupId&org.springframework&/groupId& &artifactId&gs-serving-web-content&/artifactId& &version&0.1.0&/version& &parent&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-parent&/artifactId&
&version&1.1.5.RELEASE&/version& &/parent& &dependencies&
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-thymeleaf&/artifactId&
&/dependency& &/dependencies& &properties&
&start-class&hello.Application&/start-class& &/properties& &build&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-maven-plugin&/artifactId&
&/plugins& &/build& &repositories&
&repository&
&id&spring-milestone&/id&
&url&http://repo.spring.io/libs-release&/url&
&/repository& &/repositories& &pluginRepositories&
&pluginRepository&
&id&spring-milestone&/id&
&url&http://repo.spring.io/libs-release&/url&
&/pluginRepository& &/pluginRepositories&&/project&按照maven规范,新建目录:└── src└── main└── java└── hello新建控制器,import org.springframework.stereotype.Cimport org.springframework.ui.Mimport org.springframework.web.bind.annotation.RequestMimport org.springframework.web.bind.annotation.RequestP@Controllerpublic class TestController { @RequestMapping(&/greeting&) public String greeting(@RequestParam(value=&name&, required=false, defaultValue=&World&) String name, Model model) {
model.addAttribute(&name&, name);
return &greeting&; }}Main函数,import org.springframework.boot.autoconfigure.EnableAutoCimport org.springframework.boot.SpringAimport org.springframework.ponentS@ComponentScan@EnableAutoConfigurationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}视图模板在└── main└── resources└── regreeting.html&!DOCTYPE HTML&&html xmlns:th=&http://www.thymeleaf.org&&&head& &title&Getting Started: Spring MVC&/title& &meta http-equiv=&Content-Type& content=&text/ charset=UTF-8& /&&/head&&body& &p th:text=&'Hello, ' + ${name} + ' spring mvc !'& /&&/body&&/html&这里的一些注解的含义这里不再介绍(之前笔者翻译的Spring文档有过说明)。执行 mcn clean package再执行 target下的可执行jar文件。接着就能在浏览器访问了。下一篇详细介绍thymeleaf的用法。
最新教程周点击榜
微信扫一扫Thymeleaf是现代化服务器端的Java模板引擎,不同与JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性。详细资料可以浏览。本文主要介绍Thymeleaf模板的使用说明。
模板(template fragments)
定义和引用模板
日常开发中,我们经常会将导航栏,页尾,菜单等部分提取成模板供其它页面使用。
在Thymeleaf 中,我们可以使用th:fragment属性来定义一个模板。
我们可以新建一个简单的页尾模板,如:/WEB-INF/templates/footer.html,内容如下:
&!DOCTYPE html SYSTEM &http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd&&
&html xmlns=&http://www.w3.org/1999/xhtml&
xmlns:th=&http://www.thymeleaf.org&&
&div th:fragment=&copyright&&
(C) 2016 xxx
上面定义了一个叫做copyright的片段,接着我们可以使用th:include或者th:replace属性来使用它:
&div th:include=&footer :: copyright&&&/div&
其中th:include中的参数格式为templatename::[domselector],
其中templatename是模板名(如footer),domselector是可选的dom选择器。如果只写templatename,不写domselector,则会加载整个模板。
当然,这里我们也可以写表达式:
&div th:include=&footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})&&&/div&
模板片段可以被包含在任意th:*属性中,并且可以使用目标页面中的上下文变量。
不通过th:fragment引用模板
通过强大的dom选择器,我们可以在不添加任何Thymeleaf属性的情况下定义模板:
&div id=&copy-section&&
通过dom选择器来加载模板,如id为copy-section
&div th:include=&footer :: #copy-section&&
th:include 和 th:replace区别
th:include 是加载模板的内容,而th:replace则会替换当前标签为模板中的标签
例如如下模板:
&footer th:fragment=&copy&&
我们通过th:include 和 th:replace来加载模板
&div th:include=&footer :: copy&&&/div&
&div th:replace=&footer :: copy&&&/div&
返回的HTML如下:
&div& & 2016 &/div&
&footer&& 2016 &/footer&
模板参数配置
th:fragment定义模板的时候可以定义参数:
&div th:fragment=&frag (onevar,twovar)&&
&p th:text=&${onevar} + ' - ' + ${twovar}&&...&/p&
在 th:include 和 th:replace中我们可以这样传参:
&div th:include=&::frag (${value1},${value2})&&...&/div&
&div th:include=&::frag (onevar=${value1},twovar=${value2})&&...&/div&
此外,定义模板的时候签名也可以不包括参数:&div th:fragment=&frag&&,我们任然可以通过&div th:include=&::frag (onevar=${value1},twovar=${value2})&&...&/div&这种方式调用模板。这其实和&div th:include=&::frag& th:with=&onevar=${value1},twovar=${value2}&&起到一样的效果
th:assert 断言
我们可以通过th:assert来方便的验证模板参数&header th:fragment=&contentheader(title)& th:assert=&${!#strings.isEmpty(title)}&&...&/header&
th:remove 删除代码
假设我们有一个产品列表模板:
&th&NAME&/th&
&th&PRICE&/th&
&th&IN STOCK&/th&
&th&COMMENTS&/th&
&tr th:each=&prod : ${prods}& th:class=&${prodStat.odd}? 'odd'&&
&td th:text=&${prod.name}&&Onions&/td&
&td th:text=&${prod.price}&&2.41&/td&
&td th:text=&${prod.inStock}? #{true} : #{false}&&yes&/td&
&span th:text=&${#lists.ments)}&&2&/span& comment/s
&a href=&comments.html&
th:href=&@{/product/comments(prodId=${prod.id})}&
th:unless=&${#lists.ments)}&&view&/a&
这时一个很常规的模板,但是,当我们直接在浏览器里面打开它(不(不使用Thymeleaf ),它实在不是一个很好的原型。因为它的表格中只有一行,而我们的原型需要更饱满的表格。
如果我们直接添加了多行,原型是没有问题了,但通过Thymeleaf输出的HTML又包含了这些事例行。
这时通过th:remove属性,可以帮我们解决这个两难的处境,
&th&NAME&/th&
&th&PRICE&/th&
&th&IN STOCK&/th&
&th&COMMENTS&/th&
&tr th:each=&prod : ${prods}& th:class=&${prodStat.odd}? 'odd'&&
&td th:text=&${prod.name}&&Onions&/td&
&td th:text=&${prod.price}&&2.41&/td&
&td th:text=&${prod.inStock}? #{true} : #{false}&&yes&/td&
&span th:text=&${#lists.ments)}&&2&/span& comment/s
&a href=&comments.html&
th:href=&@{/product/comments(prodId=${prod.id})}&
th:unless=&${#lists.ments)}&&view&/a&
&tr class=&odd& th:remove=&all&&
&td&Blue Lettuce&/td&
&td&9.55&/td&
&td&no&/td&
&span&0&/span& comment/s
&tr th:remove=&all&&
&td&Mild Cinnamon&/td&
&td&1.99&/td&
&td&yes&/td&
&span&3&/span& comment/s
&a href=&comments.html&&view&/a&
其中th:remove的参数有如下几种:
all 删除当前标签和其内容和子标签
body 不删除当前标签,但是删除其内容和子标签
tag 删除当前标签,但不删除子标签
all-but-first 删除除第一个子标签外的其他子标签
none 啥也不干
当然,我们也可以通过表达式来传参,
&a href=&/something& th:remove=&${condition}? tag : none&&Link text not to be removed&/a&
以上为Thymeleaf中模板的一些用法,各位看官请点赞。
阅读(...) 评论()thymeleaf有人用么?_java吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:532,586贴子:
thymeleaf有人用么?收藏
前段时间看spring的一些文档的时候被强行安利了,看了一下这玩意的特性好像很不错,直接在html上扩展属性,可以静态预览页面什么的。不过要把以前的项目和经验从jsp迁移过来的话还是比较费事的,现在在考虑要不要入坑
java,就业薪资高,发展前景好,零基础入学,120天入门到精通.线上线下学习任你选,先就业后付款,学员尊享-名企入职通道.先就业后付款!
你在说什么,我都完全不知道
性能差 比freemarker还差 网上有测评 他牛逼的地方是像可以直接当html显示(自定义标签在属性里…没经过模板引擎转换也可以显示默认html)
大神再说什么~
很不好用,我们的项目里面,领导用这个框架搭的,总结下来有N多缺点,首先和后台的耦合度太强,举例:表单提交对应的后台实体类,该实体类有多少属性,前端就要有多少th:name,少一个就报错(领导说这样好,这样写代码更严格,一有错就会马上知道),那么我们就且先不说这点,说说其他不好的地方,所有判断都是写在html标签内部,举例:&div th:if=&${pojo.type == '1'}& th:text=&asfsaf&&&/div&,这样最大的缺点就是如果有并列同级的标签,你th:if就要写两次,如果要外面再套一层div就更麻烦,还有可能引起样式上的问题,最终我还遇到问题,让我决定以后绝对不用这个框架的问题,“&”,页面里只要用到这个符号,页面就会无条件报错,比如a标签里面的href需要get请求用&来拼2个或2个以上参数时,页面直接报错,只能写脚本,而且这个框架的a标签不支持onclick,换句话说就必须写绑定事件,页面的性能进一步下降,这还只是有了解决方案的情况,说说另一种没有我还没有找到解决方案的情况,&div th:if=${pojo.type !='1' && pojo.type != '2'}&&/div&这样并列条件判断直接报错,根本就不能用,都想不出办法,所以我的观点是,要用一个框架不是先看他牛逼的地方,还是先看他常用的地方是不是都支持,基本的都不支持,再强大有P用
投奔angularjs吧
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或JSP过时了?
[问题点数:40分]
JSP过时了?
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年12月 Web 开发大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 thymeleaf jsp 性能 的文章

 

随机推荐