当前位置:网站首页>Jersey2.25.1 integration freemaker
Jersey2.25.1 integration freemaker
2022-07-24 07:35:00 【kse_ music】
One 、 Add attribute configuration and registration in the configuration class FreemarkerMvcFeature
//register(MvcFeature.class);
property(MvcFeature.TEMPLATE_BASE_PATH+".freemarker", "freemarker");
//property(MvcFeature.ENCODING+".freemarker", "GBK");// If @Produces No output code specified , Then this setting shall prevail , Default UTF-8
property(FreemarkerMvcFeature.TEMPLATE_OBJECT_FACTORY,new FreemarkerConfig(null));
register(FreemarkerMvcFeature.class);// rely on MvcFeature
Two 、 Writing freemarker To configure
import com.hiekn.demo.exception.FtlException;
import com.hiekn.demo.util.FtlMethod;
import freemarker.cache.*;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Version;
import jersey.repackaged.com.google.common.collect.Lists;
import org.glassfish.jersey.server.mvc.freemarker.FreemarkerConfigurationFactory;
import org.jvnet.hk2.annotations.Optional;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import java.io.File;
import java.util.List;
import java.util.Properties;
public class FreemarkerConfig implements FreemarkerConfigurationFactory {
protected final Configuration configuration;
@Inject
public FreemarkerConfig(@Optional final ServletContext servletContext){
Version version = Configuration.VERSION_2_3_23;
configuration = new Configuration(version);
configuration.setSharedVariable("cutStr", new FtlMethod());
configuration.setDefaultEncoding("utf-8");// If .ftl Not in the return view , But through include Imported , Affected by this attribute , Not subject to @Produces influence
configuration.setTemplateExceptionHandler(new FtlException());
configuration.setIncompatibleImprovements(version);
configuration.setObjectWrapper(new DefaultObjectWrapper(version));
final List<TemplateLoader> loaders = Lists.newArrayList();
Properties p = new Properties();
try {
p.load(FreemarkerConfig.class.getClassLoader().getResourceAsStream("freemarker.properties"));
configuration.setSettings(p);
if (servletContext != null) {
loaders.add(new WebappTemplateLoader(servletContext));
}
loaders.add(new ClassTemplateLoader(FreemarkerConfig.class, "/"));
loaders.add(new FileTemplateLoader(new File("/")));
} catch (Exception e) {
e.printStackTrace();
}
configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
}
@Override
public Configuration getConfiguration() {
return configuration;
}
}
3、 ... and 、 Write view controller
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.log4j.Logger;
import org.glassfish.jersey.server.mvc.Template;
import org.glassfish.jersey.server.mvc.Viewable;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;
@Controller
@Path("/ftl")
@Produces(MediaType.TEXT_HTML+";charset=UTF-8")
@Api(" Back to view ")
public class Ftl {
static final Logger log = Logger.getLogger(Ftl.class);
@GET
@Path("/test")
@ApiOperation(" The test returns to the view ")
public Viewable exampleView(@Context HttpServletRequest request) {
Map<String, Object> data = new HashMap<>();
data.put("test", "this is the ViewResource test text");
data.put("request", request);
BeansWrapper wrapper = new BeansWrapperBuilder(Configuration.VERSION_2_3_23).build();
TemplateHashModel staticModels = wrapper.getStaticModels();
data.put("statics", staticModels);
TemplateHashModel enumModels = wrapper.getEnumModels();
data.put("enums",enumModels);
try {
TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get("java.io.File");
data.put("File", fileStatics);
TemplateHashModel roundingModeEnums = (TemplateHashModel) enumModels.get("java.math.RoundingMode");
data.put("RoundingMode", roundingModeEnums);
} catch (TemplateModelException e) {
e.printStackTrace();
}
return new Viewable("/test", data);
}
@Path("/")
@GET
@Template(name = "/index")
@ApiOperation(" The test returns to the view default page ")
public Map<String, Object> indexView() {
Map<String, Object> map = new HashMap<>();
System.out.println(" I'm home page ");
return map;
}
@Path("{path}")
@GET
@ApiOperation(" Return the corresponding page according to the request path ")
public Viewable common(@PathParam("path") String path) {
Map<String, Object> map = new HashMap<>();
return new Viewable("/"+path,map);
}
}
边栏推荐
- MySQL statement
- 【Pytorch】conv2d torchvision.transforms
- [leetcode simple] 20. Valid brackets stack
- Wild pointer, null pointer, invalid pointer
- [steering wheel] code review ability of idea to ensure code quality
- 【信息系统项目管理师】第七章 复盘成本管理知识架构
- Mitre att & CK ultra detailed learning notes-02 (a large number of cases)
- Chapter007 FPGA learning IIC bus EEPROM reading
- C language file operation
- Laplace distribution
猜你喜欢

Introduction to C language II. Functions

爬虫学习-概述

Simple installation of sqli Labs

Advanced part of C language I. data storage
![[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management](/img/a9/7cdab9264902b1e2947a43463f6b32.png)
[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management

mysql查询当前节点的所有父级

DOM operation of JS -- style operation

Vulnhub DC1

R language handwritten numeral recognition

Advanced part of C language IV. detailed explanation of user-defined types
随机推荐
php 转义字符串
Requests crawl page source code data
Vulnhub DC1
Filter filter
Development system selection route
[leetcode] 11. Container with the most water - go language solution
The shortest distance of Y axis of 2D plane polyline
PHP link log scheme
R language handwritten numeral recognition
R语言手写数字识别
爬虫学习-概述
剑指offer专项突击版第8天
Injectfix principle learning (to realize the heat of repair addition)
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled
Induction, generalization, deduction
Game three piece chess
Service Vulnerability & FTP & RDP & SSH & Rsync
B. Also Try Minecraft
Nacos的高级部分
服务漏洞&FTP&RDP&SSH&rsync