ITPub博客

首页 > IT职业 > IT图书 > SpringMVC集成FreeMarker以及多视图问题解决

SpringMVC集成FreeMarker以及多视图问题解决

IT图书 作者:tris42 时间:2021-09-09 09:00:56 0 删除 编辑

声明:同是新手,哪里不对可以指正,谢谢。

首先我用的maven的web项目。其中引入的jar包
junitjunit4.12test<!-- spring相关依赖 -->org.springframeworkspring-webmvc4.3.7.RELEASEmysqlmysql-connector-java5.1.39org.springframeworkspring-jdbc4.3.7.RELEASEorg.springframeworkspring-test4.3.7.RELEASEorg.springframeworkspring-context-support4.3.7.RELEASE<!-- 日志 -->org.slf4jslf4j-log4j121.7.21<!-- freeMarker -->org.freemarkerfreemarker2.3.23<!-- 工具类,非必需 -->commons-collectionscommons-collections3.2.2

此处需要注意的是:

  1. 暂时不要引入mybatis的jar包,因为该实例没有配置datasource,引入后会直接导致tomcat无法启动。

  2. 不要少了spring-context-support.jar不然在配置freemarderConfig时会有“No setter
    found for property 'templateLoaderPath' ”错误。

 <!--   错误   No setter found for                        property 'templateLoaderPath' in class
'org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer'    -->
第二步:配置web.xml

在这里需要加入spring的核心监听器

org.springframework.web.context.ContextLoaderListener

,以及springMVC的请求分发器

org.springframework.web.servlet.DispatcherServlet

web.xml代码如下:


  FreeMarkerTcontextConfigLocationclasspath:spring/applicationContext.xml<!-- Bootstraps the root web application context before servlet initialization -->org.springframework.web.context.ContextLoaderListener<!-- The front controller of this Spring Web application, responsible for handling all application requests -->springDispatcherServletorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/beans/spring-*.xml1<!-- Map all requests to the DispatcherServlet for handling -->springDispatcherServlet/
第三步:指定spring-MVC的视图解析器和spring解析器

在这里需要注意的就是要让FreeMarker的解析器优先级高一些,这样就会优先解析ftl文件。
这个比较简单,这个也不用多说了,大家看一下,有注释。
!!!要注意!!! :定义模版所在的位置(freeMarkerConfigurer的templateLoaderPath属性)后,此处定义后就不能在freeMarkerviewResolver中定义prifix属性,不然会提示 找不到模版!!!

<!--JSP视图解析器--><!-- 配置freeMarker视图解析器 --><!-- 配置freeMarker的模板路径 -->3600zh_CNyyyy-MM-dd HH:mm:ssyyyy-MM-dd#.##
第四步:定义一个Controller用于不同请求的跳转
/*
     * freeMarker需要返回一个ModelAndView对象,其中可以添加多种数据类型
     */
    @RequestMapping(value="haha",method=RequestMethod.GET)
    public ModelAndView index(){
        System.out.println("进入Index");
        ModelAndView mv = new ModelAndView("index");  
        mv.addObject("message", "Spring MVC And Freemarker");  
        mv.addObject("name", " Hello world , test my first spring mvc ! ");
        String[] s = {"数组1","数组2"};
        List list = Arrays.asList(s);
        mv.addObject("list", list);
        return mv;  
    }
    /*
     * SpringMVC的测试方法
     */
    @RequestMapping(value="home",method=RequestMethod.GET)
    public String home(){
        System.out.println("这里是Home");
        return "home";
    }
第五步:写一个模版ftl文件

注意事项:

  1. 该文件需要放在上面指定的目录下,我上面写的是:(“/WEB-INF/view/”)目录。
  2. 名称与Controller中MovdelAndView相同。我定义的为(”index“),所以模版的名称为:index.ftl
  3. 取集合时,ModelAndView中定义的集合名称和s模版中的保持一致

  4. ${r"代码内容"},这种形式可将代码原样输出到html页面上。

具体内如下 :

       
      
freemarker测试  
        FreeMarker
            

${message},${name}

这里是集合的遍历方法

  1. list是集合名称
  2. a是每个元素的名称
  3. 通过${r'${a}'}形式取出即可显示
${a} #list>
最后,让我把项目打包分享给能看到最后的勇士们!

war和原项目都有,有什么问题及时交流,谢谢。
分享码:pt95
下载地址:FreeMarkerT1

大家看一下结果:
图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1551/viewspace-2807684/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    82
  • 访问量
    782