将jsp页面转为html页面 您所在的位置:网站首页 jsp转换html 将jsp页面转为html页面

将jsp页面转为html页面

2023-09-24 01:14| 来源: 网络整理| 查看: 265

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1.修改配置文件servlet,添加thymleaf解析器

  

2.jsp页面相关内容转为html可识别代码,如

${user.userId} ${user.userName} Delete

转为

delete

3.将thymleaf相关包导入(thymeleaf-2.1.4.RELEASE, thymeleaf-spring4-2.1.4.RELEASE. unbescape-1.1.0.RELEASE)

注意:  html中的EL表达式无法识别,需要通过th:方法

    

4.通过网络连接他人的数据库

新建sql用户(name随便,Hostname-IP地址)

更改工程中数据库连接的相关代码

5.html页面的顶部为(第二句为thymleaf的相关代码)

6、关于thymleaf解析器的配置的补充

对于返回的字符串,我们会先对通过配置的thymeleaf来先判断是否可以解析为一个HTML页面,然后不可以的时候就抛出错误,但是在我们的项目中,我们并没有抛出异常,因为我们在第一次解析中加入我们自定义的一个类在cn.agriculture.common.component这个包中,这个类的代码如下:

public class ThymeleafViewResolverEx extends ThymeleafViewResolver { @Override public View resolveViewName(String viewName, Locale locale) throws Exception { ServletContextTemplateResolver servletContextTemplateResolver =         (ServletContextTemplateResolver)this.getWebApplicationContext().getBean("templateResolver"); servletContextTemplateResolver.initialize(); String prefix = servletContextTemplateResolver.getPrefix().substring(1); String suffix = servletContextTemplateResolver.getSuffix(); //String str = getClass().getResource("/").toString().replace("file:/", "") .replace("/WEB-INF/classes/", ""); //log.info("*****************************" + str); log.info("-----------------------------" + this.getServletContext().getRealPath("/")); File file = new File(this.getServletContext().getRealPath("/") + prefix + viewName + suffix); if (!file.exists()){ return null; } return super.resolveViewName(viewName, locale); }}

     在这个类的作用下,我们可以先对controller返回的一个字符串做HTML页面的解析,在没有相应的HTML页面再进行Jsp的解析,即调用SpringMVC的内部视图解析器。若我们不需要这样的帮助时,我们可以将第一段代码中的最后一个bean的class属性值选择为ThymeleafViewResolver的包名。

     当我们因为类找不到时,我们可以通过复制类,然后按住 ctrl+shfit+t 来快捷提示对应的类的位置,然后将对应的包找到复制到我们工程WebContent的WEB-INF文件夹下的lib中,这样我们可以实现HTML的解析。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有