我正在将Velocity与Jersey集成,我正面临着资源工具i18n的问题。
整合完成如下:
web.xml (条纹,与相关部分保持一致):
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfiguration</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<context-param>
<filter>
<filter-name>velocity-filter</filter-name>
<filter-class>org.apache.velocity.tools.view.VelocityViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>velocity-filter</filter-name>
<servlet-name>rest-servlet</servlet-name>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>rest-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package.demo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
现在,我的 tools.xml (在WEB-INF文件夹中):
<?xml version="1.0" encoding="UTF-8"?>
<tools>
<data type="boolean" key="STRUTS_TOOLS_AVAILABLE" value="false"/>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.generic.ResourceTool"
bundles="my.package.demo.Resource" />
</toolbox>
</tools>
然后我创建了一个基本的 VelocityTemplateProcessor
package my.package.demo;
import static com.google.common.base.Strings.isNullOrEmpty;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import org.apache.velocity.Template;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.ToolContext;
import org.apache.velocity.tools.view.ServletUtils;
import org.apache.velocity.tools.view.VelocityView;
import com.sun.jersey.api.core.HttpContext;
import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.spi.template.ViewProcessor;
@Provider
public class VelocityTemplateProcessor implements ViewProcessor<Template> {
private static Map<MediaType, String> fileExtension;
static {
fileExtension = new HashMap<>();
fileExtension.put(MediaType.TEXT_HTML_TYPE, ".html.vm");
fileExtension.put(MediaType.APPLICATION_JSON_TYPE, ".json.vm");
fileExtension.put(MediaType.APPLICATION_XML_TYPE, ".xml.vm");
}
@Context
private HttpContext httpContext;
private final VelocityView velocityView;
public VelocityTemplateProcessor(@Context final ServletContext servletContext) {
velocityView = ServletUtils.getVelocityView(servletContext);
}
@Override
public Template resolve(final String name) {
if (isNullOrEmpty(name)) {
return null;
}
final MediaType mediaType = httpContext.getResponse().getMediaType();
final String extension = fileExtension.get(mediaType);
if (isNullOrEmpty(extension)) {
return null;
}
try {
return velocityView.getTemplate(name + extension);
} catch (final ResourceNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
public void writeTo(final Template t, final Viewable viewable, final OutputStream out) throws IOException {
final ToolContext context = velocityView.createContext();
context.put("data", viewable.getModel());
final Writer writer = new StringWriter();
t.merge(context, writer);
out.write(writer.toString().getBytes());
}
}
除了ResourceTool之外,这很有效。它似乎正确加载了所请求的配置(至少,它在ToolContext中的显示方式),但是,当我尝试在模板中调用$text.my.property
时,似乎它使用具有默认配置的ResourceTool(使用名为“resources”的包,然后找不到我的密钥
它在我明确提到$text.bundle('my.package.demo.Resource').my.property
时有效,但我想让管理模板的人们更加透明。
答案 0 :(得分:1)
没关系。我找到了。我必须使用ToolContext
和HttpServletRequest
创建HttpServletResponse
。两者都注入了@Context
。现在就像一个魅力。