我们在Web应用程序中大量使用Velocity.虽然很容易调试Java方面并确保正确填充Velocity Context,但是能够在合并步骤中逐步解析VTL,设置断点等是非常有价值的.是否有任何工具或者IDE/IDE插件可以通过VTL(Velocity Template Language)实现这种功能吗?
有没有办法在Velocity中做三元运算符?这就是我想做的事情:
#set ($name = ($args.get(0) == "") ? "default" : $args.get(0))
Run Code Online (Sandbox Code Playgroud)
而不是粗糙的if-else
#if ($args.get(0) == "")
#set ($name = "default")
#else
#set ($name = $args.get(0))
#end
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我尝试包括在以下graphql模式中定义的嵌套类型:
type User {
id: String!
posts: [Post]
}
type Post {
id: String!
}
type Query {
getUser(id: String!): User
getPost(id: String!): Post
}
Run Code Online (Sandbox Code Playgroud)
如您所见,一个用户有多个帖子。我将AppSync与相邻列表动态表(包含用户和与Post有关的行)一起用作数据源。在AppSync中,我必须使用请求映射模板,但是在阅读文档后,我还不了解如何解析嵌套类型?
我可以想象在查询getUser后解析器时应使用User_id进行调用。如果是这样,我如何在后解析器中访问父ID?这是${context.source}地方吗?
由于getPost查询解析器与getUser Post子级调用的Post解析器相同,我是否必须将一些逻辑与解析器的请求模板集成在一起以处理两种情况?
一个例子真的很有帮助!
假设您有一个包含(已解析)标题,正文,页脚模板的标准模板.
在body模板中定义了一个像$ subject这样的变量,你希望它也显示在头模板中.
在其他一些模板语言中,例如HTML :: Mason(基于perl),你首先要评估body模板以获取$ subject变量,但是将它的输出暂时存储在一个变量中,这样你的最终输出就会以正确的顺序结束(header,身体,页脚)
在速度上它看起来像
set($ body = #parse("body.vm"))
解析( "header.vm")
$ {机构}
解析( "footer.vm")
然而,这似乎不起作用,任何关于如何做到这一点的想法?
我不确定是否有办法在Velocity中执行此操作:
我有一个用户POJO,其中一个名为Status的属性,看起来像一个枚举(但它不是,因为我被困在Java 1.4上),定义看起来像这样:
public class User {
// default status to User
private Status status = Status.USER;
public void setStatus(Status status) {
this.status = status;
}
public Status getStatus() {
return status;
}
Run Code Online (Sandbox Code Playgroud)
而Status是一个静态的内部类:
public static final class Status {
private String statusString;
private Status(String statusString) {
this.statusString = statusString;
}
public final static Status USER = new Status("user");
public final static Status ADMIN = new Status("admin");
public final static Status STATUS_X = new Status("blah");
//.equals() and .hashCode() implemented as …Run Code Online (Sandbox Code Playgroud) 所有.我需要在速度模板中使用java 5枚举,以便我可以编写类似的东西
public enum Level{
INFO, ERROR;
}
Velocity template:
#if($var == Level.INFO)
...
#else
...
#end
怎么做到呢?提前致谢.
对于apache的速度,使用$与$!{}访问变量之间是否存在差异.如果是这样,它是什么?
我试图在生成的标记中抽象出一个常见的场景,我需要一些标记来"包装"任意内容.所以不要写这个
<div class="container">
<p class="someClass">Some header</p>
<div id="foo">
<!-- The real content that changes -->
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以写一些"喜欢"的东西
#????
<!-- The real content that changes
#end
Run Code Online (Sandbox Code Playgroud)
哪里显然我不知道#???? 将会.
据我所知,不可能用宏做这个,不能为块的开始定义一个宏,而为块的结尾定义一个宏.
#macro(startContained)
<div class="container">
<p class="someClass">Some header</p>
<div id="foo">
#end
#macro(endContained)
</div>
</div>
#end
#startContained
<!-- The real content -->
#endContained
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?
我有一些问题,我使用键值映射到Velocity.
有人有这个功能的例子吗?
$myMap ={}
$myMap.put("mykey1", "myvalue")
$myMap.delete("mykey1")
$myMap.getValue("mykey1")
Run Code Online (Sandbox Code Playgroud) 我们在应用程序中使用Spring Boot以及AngularJS和HTML.我们仅将Velocity用于电子邮件模板,但不用于视图解析器.
@Bean(name = "velocityEngine")
public VelocityEngineFactoryBean velocityEngineFactoryBean() {
VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
Properties p = new Properties();
p.put("resource.loader", "class");
p.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
vefb.setVelocityProperties(p);
return vefb;
}
Run Code Online (Sandbox Code Playgroud)
即使我们不使用Velocity视图解析器,我们也会因自动配置而出现以下错误:
错误org.apache.velocity - ResourceManager:无法在任何资源加载器中找到资源"LoadList".错误org.apache.velocity - ResourceManager:无法在任何资源加载器中找到资源"索引".
我试图禁用Velocity自动配置:
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,VelocityAutoConfiguration.class })
@SuppressWarnings("PMD")
@EnableAsync
public class Application {
Run Code Online (Sandbox Code Playgroud)
还在application.properties文件中添加了以下内容:
spring.velocity.check-template-location=false
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到上述错误.反正有没有单独禁用Velocity视图解析器?
velocity ×10
java ×3
apache ×2
templates ×2
aws-appsync ×1
debugging ×1
enums ×1
graphql ×1
html ×1
spring ×1
spring-boot ×1
spring-mvc ×1
vtl ×1