码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMvc 与 freemarker整合

时间:2017-07-01 15:28:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:XML   har   exce   get   ftl   osi   通知   tty   pat   

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整合

标签:XML   har   exce   get   ftl   osi   通知   tty   pat   

原文地址:http://www.cnblogs.com/duoqingxiaohao/p/7102373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!