首页 > 代码库 > SpringMvc 与 freemarker整合

SpringMvc 与 freemarker整合

1:在pom.xml文件中引入freemarker的jar包

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --><dependency>    <groupId>org.freemarker</groupId>    <artifactId>freemarker</artifactId>    <version>2.3.23</version></dependency>

2:在servlet.xml(applicationContext.xml)配置文件中配置freemarker。

!-- framemarker配置,將framemarker配置放在InternalResourceViewResolver的前面,先訪問freemarker,在訪問jsp --><bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/freemarker/"/></bean><bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="order" value="9"></property><property name="cache" value="true"/><property name="prefix" value=""/><property name="suffix" value=".ftl"/><property name="contentType" value="text/html; charset=utf-8"/>  </bean>

3:控制层的写法,model向前台传输数据。

/**     * 前段首页     * @return     */    @RequestMapping("/index")    public Object frontIndex(Model model){        logger.info("登录前端页面展示");        try{//新闻列表 显示第一页            List<News> newsList = newsService.findAllNews(PageConstants.FRONT_LIST_SIZE);            //通知公告            List<Notice> noticeList = noticeService.findAllNotice(PageConstants.FRONT_LIST_SIZE);            //健康导航            List<Pehealth> pehealthList = pehealthService.findAllPehealth(PageConstants.FRONT_LIST_SIZE);            //下载专区            List<Attachment> attachmentList = attachmentService.findAllattachment(PageConstants.FRONT_LIST_SIZE);            model.addAttribute("newsList", newsList);            model.addAttribute("noticeList", noticeList);            model.addAttribute("pehealthList", pehealthList);            model.addAttribute("attachmentList", attachmentList);        }catch(Exception e){            e.getStackTrace();            logger.debug("/index出错!");        }        return "index";    }

 

SpringMvc 与 freemarker整合