ITPub博客

首页 > IT职业 > IT职场 > springMvc与Freemarker整合

springMvc与Freemarker整合

IT职场 作者:freefancy 时间:2021-09-09 09:06:07 0 删除 编辑

项目路径
图片描述
引jar包
(1)图片描述
(2)配置web.xml文件

contextConfigLocationclasspath:applicationContext.xmlindex.htmlorg.springframework.web.context.ContextLoaderListener字符集过滤器encodingFilterorg.springframework.web.filter.CharacterEncodingFilter字符集编码encodingUTF-8encodingFilter*springMvcorg.springframework.web.servlet.DispatcherServletspring mvc 配置文件contextConfigLocationclasspath:spring-mvc.xml1springMvc/*

(3)配置applicationContext.xml文件

<!-- 自动扫描dao和service包(自动注入) --><!-- freemarker的配置 -->10zh_CNyyyy-MM-dd HH:mm:ssyyyy-MM-dd#.##<!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 --><!-- 添加注解驱动 -->

(4)配置spring-mvc.xml文件

<!-- 
     --><!-- 开启controller注解支持 --><!-- 注意事项请参考:http://jinnianshilongnian.iteye.com/blog/1762632 --><!-- 当在web.xml 中   DispatcherServlet使用 / 映射时,能映射静态资源 --><!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->text/html;charset=UTF-8<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --><!-- json转换器-->

(5)RichFreeMarkerView.java中的代码

package com.lmy.freemarker.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

public class RichFreeMarkerView extends FreeMarkerView {

    public static final String CONTEXT_PATH = "base";

    @SuppressWarnings("unchecked")
    protected void exposeHelpers(Map model, HttpServletRequest request)
            throws Exception {
        super.exposeHelpers(model, request);
        model.put(CONTEXT_PATH, request.getContextPath());
    }
}

(6)controller层(简单写了点逻辑)

package com.lmy.freemarker.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/from")
public class TestFreemarkerController {

    @RequestMapping("/query")
    public String TestFreemarker(ModelMap modelMap){
        String mes="你好!";
        String mes1="你好啊!";
        int a=2;
        modelMap.put("a",a);
        modelMap.put("mes", mes);
        modelMap.put("mes1", mes1);
        return "MyJsp";

    }

}

(7)页面



  
index.html



点击
  




  
MyHtml.html


 
  ${mes!}
${mes1!} #if>

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

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

注册时间:2019-04-15

  • 博文量
    118
  • 访问量
    798