Spring MVC整合FreeMarker的示例

网友投稿 255 2023-02-10

Spring MVC整合FreeMarker的示例

什么是Freemarker?

FreeMarker是一个用java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,jsP或Java 等。

    目前企业中:主要用Freemarker做静态页面或是页面展示

一.工程结构

二.web.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

SpringMVC

contextConfigLocation

/WEB-INF/springMVC-servlet.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

springMVC

org.springframework.web.servlet.DispatcherServlet

1

springMVC

/

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

SpringMVC

contextConfigLocation

/WEB-INF/springMVC-servlet.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

springMVC

org.springframework.web.servlet.DispatcherServlet

1

springMVC

/

三.springMVC-servlet.xml

xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

">

3600

zh_CN

yyyy-MM-dd HH:mm:ss

yyyy-MM-dd

#.##

xmlns:context="http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

">

3600

zh_CN

yyyy-MM-dd HH:mm:ss

yyyy-MM-dd

#.##

在JSP和Freemarker的配置项中都有一个order property,上面例子是把freemarker的order设置为0,jsp为1,意思是找view时,先找ftl文件,再找jsp文件做为视图。这样Freemarker视图解析器就能与JSP视图解析器并存。

四.FreeMarkerController.java

package com.bijian.study.controller;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import com.bijian.study.utils.JsonUtil;

import com.bijian.study.vo.User;

@Controller

public class FreeMarkerController {

@RequestMapping("/get/usersInfo")

public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) {

User user = new User();

user.setUsername("zhangsan");

user.setPassword("1234");

User user2 = new User();

user2.setUsername("lisi");

user2.setPassword("123");

List users = new ArrayList();

users.add(user);

users.add(user2);

return new ModelAndView("usersInfo", "users", users);

}

@RequestMapping("/get/allUsers")

public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {

List users = new ArrayList();

User u1 = new User();

u1.setUsername("王五");

u1.setPassword("123");

users.add(u1);

User u2 = new User();

u2.setUsername("张三");

u2.setPassword("2345");

users.add(u2);

User u3 = new User();

u3.setPassword("fgh");

u3.setUsername("李四");

users.add(u3);

Map rootMap = new HashMap();

rootMap.put("userList", users);

Map product = new HashMap();

rootMap.put("lastProduct", product);

product.put("url", "http://baidu.com");

product.put("name", "green hose");

String result = JSON.toJSONString(rootMap);

Map resultMap = new HashMap();

resultMap = JsonUtil.getMapFromJson(result);

return new ModelAndView("allUsers", "resultMap", resultMap);

}

}

五.JsonUtil.java

package com.bijian.study.utils;

import java.util.Map;

import com.alibaba.fastjson.JSON;

public class JsonUtil {

public static Map getMapFromJson(String jsonString) {

if (checkStringIsEmpty(jsonString)) {

return null;

}

return JSON.parseObject(jsonString);

}

/**

* 检查字符串是否为空

* @param str

* @return

*/

private static boolean checkStringIsEmpty(String str) {

if (str == null || str.trim().equals("") || str.equalsIgnoreCase("null")) {

return true;

}

return false;

}

}

六.User.java

ackage com.bijian.study.vo;

public class User {

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

七.usersInfo.ftl

<#list users as user>

username : ${user.username},

password : ${user.password}

#list>

八.allUsers.ftl

<#list resultMap.userList as user>

Welcome ${user.username}!  id:${user.password}

#list>

Our latest product:

${resultMap.lastProduct.name} !

九.运行效果

再输入http://http://localhost:8088/SpringMVC/greeting?name=zhangshan,JSP视图解析器运行依然正常。

至此,就结束完成整合了!

以上就是Spring MVC整合FreeMarker的示例的详细内容,更多关于Spring MVC整合FreeMarker的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:招聘信息聚合网站(招聘信息资源网)
下一篇:企业工商数据api(企业工商数据采集)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~