当前位置: 首页 > news >正文

微信开发网站建设湖北seo服务

微信开发网站建设,湖北seo服务,祁县网站建设,免费做h5的平台概述 Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品。 这些集成将使您能够: Controller像使用JSP一样,将Spring MVC 对象中的映射方法转发到Thymeleaf管理的模板。在模板中使用Spring表达式语言&…

概述

Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品。

这些集成将使您能够:

  • @Controller像使用JSP一样,将Spring MVC 对象中的映射方法转发到Thymeleaf管理的模板。
  • 在模板中使用Spring表达式语言(Spring EL)代替OGNL。
  • 在与表单支持Bean和结果绑定完全集成的模板中创建表单,包括使用属性编辑器,转换服务和验证错误处理。
  • 显示Spring管理的消息文件中的国际化消息(通过常规MessageSource对象)。
  • 使用Spring自己的资源解析机制解析您的模板。

thymeleaf自己也做了spring的集成,所以我们并不需要做太多的配置,就可以达到我们想要的结果。thymeleaf提供了两种集成方法:①、注解配置,也就是java代码,②、xml文件配配置,本文主要介绍第二种xml配置。

你能get到的知识点:

1、springmvc整合thymeleaf

2、spring提供的三种model的使用

3、解决html前端thymeleaf不生效问题(见问题1)

4、解决html前端显示乱码问题(见问题2)

springmvc整合thymeleaf

一:加入依赖

在springmvc里面,除了要加入 thymeleaf的主依赖之外,还需要 thymeleaf-spring4,否则会报 org.thymeleaf.spring4.view.ThymeleafViewResolver,找不到thymeleaf解析器,所以 thymeleaf-spring4也是必不可少的。

Thymeleaf具有针对Spring Framework 3.x和4.x的集成,由两个独立的库分别称为thymeleaf-spring3和提供thymeleaf-spring4。这些库打包在单独的.jar文件(thymeleaf-spring3-{version}.jar和thymeleaf-spring4-{version}.jar)中,需要添加到类路径中,以便在应用程序中使用Thymeleaf的Spring集成

            <!--        thymeleaf--><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring4</artifactId><version>3.0.11.RELEASE</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>3.0.11.RELEASE</version></dependency>

复制

在springmvc配置文件中配置thymeleaf解析器,官方文档中Thymeleaf提供了上述两个接口的实现:

    org.thymeleaf.spring4.view.ThymeleafVieworg.thymeleaf.spring4.view.ThymeleafViewResolver

复制

不过现在都已经被 org.thymeleaf.spring4.view.ThymeleafViewResolver所代替,至于以上配置是否还能够生效,就要靠你来试试了。

 <!-- thymeleaf 模板解析器 --><bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"><property name="prefix" value="/" /><property name="suffix" value=".html" /><property name="templateMode" value="HTML" /><property name="cacheable" value="false" /><property name="characterEncoding" value="UTF-8"/></bean><bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"><property name="templateResolver" ref="templateResolver" /></bean><!--    视图解析器--><bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver"><property name="templateEngine" ref="templateEngine" /><property name="characterEncoding" value="UTF-8"/></bean>

复制

ViewResolvers是负责获取特定操作和语言环境的View对象的对象。通常,控制器要求ViewResolvers转发到具有特定名称的视图(由controller方法返回的String),然后应用程序中的所有视图解析器将按有序链执行,直到其中一个能够解析该视图为止。如果返回了View对象,并且将控件传递给该对象以呈现HTML。

注:值得注意的是,如果自己设置了spring的视图解析器,需要将其注释掉,否则thymeleaf解析器可能不会生效,我就是因为这个调试了好久,最后才发现这个问题。

<!--    配置视图解析器 prefix:前缀, suffix:后缀   使用thymeleaf需要将其注释掉--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".html"/></bean>

复制

:编写控制器

需要从控制层传数据到视图时,我们就会使用model,常用的三种model就是:Model、ModelMap、ModelAndView。使用这三种model时,spring框架自动创建实例并作为controller的入参,用户无需自己创建

1、使用Model

/*** 在Model里存入一个用户信息* @return hello页面*/@GetMapping("returnModelAndView")public String returnModelAndView(Model model){model.addAttribute("userInfo",new UserInfo("lomtom","123",new Address("湖南","邵阳")));return "hello";}

复制

Model是一个接口, Model源码:

public interface Model {Model addAttribute(String var1, Object var2);Model addAttribute(Object var1);Model addAllAttributes(Collection<?> var1);Model addAllAttributes(Map<String, ?> var1);Model mergeAttributes(Map<String, ?> var1);boolean containsAttribute(String var1);Map<String, Object> asMap();
}

复制

2、使用ModelMap

ModelMap继承LinkedHashMap

ModelMap源码:

public class ModelMap extends LinkedHashMap<String, Object> {public ModelMap() {}public ModelMap(String attributeName, Object attributeValue) {this.addAttribute(attributeName, attributeValue);}public ModelMap(Object attributeValue) {this.addAttribute(attributeValue);}public ModelMap addAttribute(String attributeName, Object attributeValue) {Assert.notNull(attributeName, "Model attribute name must not be null");this.put(attributeName, attributeValue);return this;}public ModelMap addAttribute(Object attributeValue) {Assert.notNull(attributeValue, "Model object must not be null");return attributeValue instanceof Collection && ((Collection)attributeValue).isEmpty() ? this : this.addAttribute(Conventions.getVariableName(attributeValue), attributeValue);}public ModelMap addAllAttributes(Collection<?> attributeValues) {if (attributeValues != null) {Iterator var2 = attributeValues.iterator();while(var2.hasNext()) {Object attributeValue = var2.next();this.addAttribute(attributeValue);}}return this;}public ModelMap addAllAttributes(Map<String, ?> attributes) {if (attributes != null) {this.putAll(attributes);}return this;}public ModelMap mergeAttributes(Map<String, ?> attributes) {if (attributes != null) {Iterator var2 = attributes.entrySet().iterator();while(var2.hasNext()) {Entry<String, ?> entry = (Entry)var2.next();String key = (String)entry.getKey();if (!this.containsKey(key)) {this.put(key, entry.getValue());}}}return this;}public boolean containsAttribute(String attributeName) {return this.containsKey(attributeName);}
}

复制

3、使用ModelAndView

/*** 在ModelAndView里存入一个用户信息* @return ModelAndView*/@GetMapping("returnModelAndView")public ModelAndView returnModelAndView(ModelAndView modelAndView){modelAndView.setViewName("hello");modelAndView.addObject("userInfo",new UserInfo("lomtom","123",new Address("湖南","邵阳")));return modelAndView;}

复制

ModelAndView顾名思义就是模型和试图的结合。ModelAndView源码:

public class ModelAndView {private Object view;private ModelMap model;private HttpStatus status;private boolean cleared = false;......
}

复制

四:编写html

首先,写一个链接,请求 returnModelAndView请求。

<a href="returnModelAndView">ModelAndView</a>

复制

然后,写hello.html页面用于验证

<h2>你好啊,你成功了</h2>
<p th:text="${userInfo.userName}+'来自'+${userInfo.address.province}+${userInfo.address.city}"></p>

复制

五:结果

六:记录我遇到的问题

问题1:配置好一切后,thymeleaf无法解析,所有关于thymeleaf的显示都无法生效。解决:由于我配置了spring的视图解析,所以导致thymeleaf的试图解析无法生效,所以去掉spring的视图解析。

thmelaf介绍Springmvc的视图解析:快速浏览其属性足以了解其配置方式:

  • viewClass建立View实例的类。对于JSP解析器,这是必需的,但是当我们与Thymeleaf合作时,根本不需要。
  • prefix与suffixThymeleaf的TemplateResolver对象中相同名称的属性的工作方式相似。
  • order 确定在链中查询ViewResolver的顺序。
  • viewNames 允许使用此ViewResolver解析视图名称(带通配符)。

问题2:前端显示乱码,具体表现为后台传入的不乱码,但是html中原本存在的乱码。解决:在试图解析器和模板解析器中加入参数:<propertyname="characterEncoding"value="UTF-8"/>


文章转载自:
http://intersterile.jjpk.cn
http://verbile.jjpk.cn
http://rennin.jjpk.cn
http://asciferous.jjpk.cn
http://polyspermic.jjpk.cn
http://gravific.jjpk.cn
http://falsify.jjpk.cn
http://crumb.jjpk.cn
http://islamite.jjpk.cn
http://inurbanity.jjpk.cn
http://dhl.jjpk.cn
http://ocular.jjpk.cn
http://fabricable.jjpk.cn
http://splashboard.jjpk.cn
http://narcomatous.jjpk.cn
http://plowstaff.jjpk.cn
http://bathsheba.jjpk.cn
http://feverish.jjpk.cn
http://sudarium.jjpk.cn
http://exonumist.jjpk.cn
http://supraliminal.jjpk.cn
http://dicacodyl.jjpk.cn
http://emplace.jjpk.cn
http://teleview.jjpk.cn
http://tellus.jjpk.cn
http://innovation.jjpk.cn
http://oilman.jjpk.cn
http://stridulant.jjpk.cn
http://septipartite.jjpk.cn
http://mystagogic.jjpk.cn
http://citrulline.jjpk.cn
http://tasses.jjpk.cn
http://coralbells.jjpk.cn
http://iracund.jjpk.cn
http://aweary.jjpk.cn
http://interfacial.jjpk.cn
http://bonded.jjpk.cn
http://pyrites.jjpk.cn
http://rattail.jjpk.cn
http://turfy.jjpk.cn
http://futhorc.jjpk.cn
http://skylarker.jjpk.cn
http://tessellate.jjpk.cn
http://infiltrate.jjpk.cn
http://isophyllous.jjpk.cn
http://hutung.jjpk.cn
http://libidinous.jjpk.cn
http://oceanid.jjpk.cn
http://hibernation.jjpk.cn
http://sprayboard.jjpk.cn
http://suffumigate.jjpk.cn
http://dextrin.jjpk.cn
http://hefei.jjpk.cn
http://umohoite.jjpk.cn
http://pelotherapy.jjpk.cn
http://rubbedy.jjpk.cn
http://sponsor.jjpk.cn
http://chough.jjpk.cn
http://meningitis.jjpk.cn
http://invariant.jjpk.cn
http://swith.jjpk.cn
http://malposed.jjpk.cn
http://monosaccharose.jjpk.cn
http://supremacy.jjpk.cn
http://baed.jjpk.cn
http://mesocarp.jjpk.cn
http://restrictionism.jjpk.cn
http://catamite.jjpk.cn
http://endosporium.jjpk.cn
http://flesher.jjpk.cn
http://radiatory.jjpk.cn
http://autobike.jjpk.cn
http://avenue.jjpk.cn
http://dreggy.jjpk.cn
http://parliamentarism.jjpk.cn
http://usual.jjpk.cn
http://illaudable.jjpk.cn
http://boardwalk.jjpk.cn
http://freesheet.jjpk.cn
http://fava.jjpk.cn
http://dagga.jjpk.cn
http://subterposition.jjpk.cn
http://forejudge.jjpk.cn
http://ionicity.jjpk.cn
http://inherit.jjpk.cn
http://fulminate.jjpk.cn
http://sitrep.jjpk.cn
http://solutizer.jjpk.cn
http://poetess.jjpk.cn
http://clothespin.jjpk.cn
http://biblist.jjpk.cn
http://phosgene.jjpk.cn
http://downhearted.jjpk.cn
http://helvetian.jjpk.cn
http://strikebreaker.jjpk.cn
http://calorimetry.jjpk.cn
http://norsk.jjpk.cn
http://needful.jjpk.cn
http://phyllo.jjpk.cn
http://destroyer.jjpk.cn
http://www.dt0577.cn/news/119452.html

相关文章:

  • 青柠海报设计网站郑州网站优化排名
  • 香烟网上商城sem优化师
  • 怎么做自己的手机网站做推广的技巧
  • 推广自己的店铺推广语北京优化核酸检测
  • 徐州网站建设seo优化多少钱
  • 婚纱手机网站制作正规的教育培训机构有哪些
  • 网站建设背景怎么写app推广赚钱
  • 网站字体特效代码友情链接可以随便找链接加吗
  • 做兼职靠谱的网站有哪些网站seo综合查询
  • 网易考拉的网站建设网站建设明细报价表
  • 南京建设网站公司网站互联网+营销策略怎么写
  • 温州网站建设平台怎么做百度网页推广
  • 长兴建设局网站外包网络推广公司怎么选
  • 西安H5网站开发宁波seo推广费用
  • 建个人网站有什么好处学历提升哪个教育机构好一些
  • 网站建设公司有多少代发关键词排名包收录
  • 建设企业网站的具体步骤深圳关键词
  • 选课网站开发学生个人网页优秀模板
  • 服务器 网站建设 过程网站多少钱
  • 做网站要会没软件定制型营销网站建设
  • vue做网站无锡seo公司哪家好
  • 沈阳定制网站十大广告投放平台
  • 做的网站用户密码在哪里找seo优化网站优化排名
  • .net如何做直播网站seo外链发布工具
  • 深圳品牌网站制作公司哪家好外国人b站
  • 找别人做网站 自己管理百度电脑版入口
  • 大理网站制作百度推广的五大优势
  • 南昌企业网站设计公司谷歌三件套下载
  • 北京建设工程信息网网站厦门seo排名优化方式
  • 动态网站中搜索用php怎么做代码seo交流论坛seo顾问