JSP是否提供了比FreeMarker更多的灵活性,还是以其他方式?我的后端涉及Servlets.在什么情况下使用哪一个?
我有一个使用FreeMarker的大型Web应用程序.当我最近更新到Spring 3.2.4并通过jetty或tomcat运行web应用程序时,我得到以下异常:java.lang.ClassNotFoundException:org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.
我知道Spring-webmvc中有FreeMarkerConfigurationFactory类,所以我的POM中包含依赖项.我不知道为什么我得到例外.我已经包含了我的POM和我的弹簧servlet.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>WhiteSwan-Web</artifactId>
<groupId>com.millad.whiteswan.web</groupId>
<version>1.5</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.12.v20130726</version>
</plugin>
</plugins>
</build>
<properties>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
<org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
<junit.version>4.11</junit.version>
<joda.time.version>2.2</joda.time.version>
<mockito.version>1.9.5</mockito.version>
<freemarker.version>2.3.20</freemarker.version>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-beta8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-beta8</version>
</dependency>
<dependency>
<groupId>com.foursquare</groupId>
<artifactId>fongo</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.5</version>
</dependency>
<!-- …Run Code Online (Sandbox Code Playgroud) 我认为切换枚举将是FreeMarker中非常基本的东西,所以我可以编写如下内容:
<!-- Doesn't work -->
Dear
<#switch gender>
<#case MALE>
sir
<#break>
<#case FEMALE>
madam
<#break>
<#default>
sir/madam
<#/switch>
Run Code Online (Sandbox Code Playgroud)
但似乎访问枚举是一个丑陋而复杂的问题.是否有一种正确,干净的方式来打开枚举值?我应该在模特中以不同方式存储它吗?
我使用ModelAttribute绑定Spring Web应用程序中的对象.
一旦我注意到,如果一个对象有一个布尔值A为真,如果我们取消选中A的复选框,它的值将不会更新.
例如,我有一个Lesson对象,其属性为"active"= true.在"编辑课程"视图中,我创建一个绑定到"活动"的复选框.如果选中复选框(绑定对象反映更改),则工作正常,但如果取消选中复选框,则对象课程不会更改.
进一步的研究告诉我,这是因为浏览器可能无法提交复选框值(这是HTML的设计).所以我必须使用丑陋request.getParameter来检查是否设置了值.
我刚刚提出这个问题,我看到asp.net mvc提供了一种更优雅地解决它的方法.我认为Spring必须提供类似的东西.有谁知道这是怎么做到的吗?
以下是我的代码:
控制器代码:
@RequestMapping(value="/test", method = RequestMethod.POST)
public String processEditLesson(@Valid Lesson lesson, BindingResult bindingResult, Model model) {
System.out.println("Lesson is active: " + lesson.isActive()); // still "true" even if the checkbox is unset
// Current work-around
String isActive = request.getParameter("active");
if (StringUtils.isNotNullOrEmpty(isActive)) {
lesson.setActive(true);
} else {
lesson.setActive(false);
}
...
}
Run Code Online (Sandbox Code Playgroud)
查看代码:
<form id="lesson" class="EditorForm" action="${rc.getContextUrl('/test.html')}" method="post" >
<fieldset>
<legend><@spring.message code="lesson.edit"/></legend>
<@spring.formHiddenInput "lesson.id" />
<@spring.formHiddenInput "lesson.studio.id" …Run Code Online (Sandbox Code Playgroud) 你如何将Freemarker(或替代)作为模板代码生成器构建到Maven项目中?我是Maven的新手,非常感谢一些帮助.
我想从我的项目中的模板生成一些代码.[一个]
而不是写我自己的,谷歌搜索发现了一个似乎被Spring使用的freemarker,这对我来说是一个很好的参考,虽然我还没有开始使用它,任何其他适合Maven的建议也会受到赞赏.
这个网站告诉我如何将它作为依赖项添加到我的pom.xml中. 这个问题告诉我生成的源应该去哪里.我无法解决的问题是如何将它们组合在一起,因此我从模板生成生成的源代码,然后生成的源代码使用常规源代码进行编译,测试,jar,javadoc等.还有其他人使用过maven中的java模板代码生成器可以帮忙吗?
[a]我知道泛型将是通常的解决方案,事实上我正在使用它们,但我必须使用模板来处理原始情况,而不会引入复制/粘贴错误.请相信我:-)
以下代码不起作用,因为Freemarker似乎将[]中的表达式的值转换为String,然后将其用作键,这不是实际预期的.
准备模板模型:
Map<MyEnum, Object> myMap;
myMap.put(MyEnum.FOO, "Foo");
myMap.put(MyEnum.BAR, "Bar");
templateModel.put("myMap", myMap);
Run Code Online (Sandbox Code Playgroud)
my.ftl:
<#list myMap?keys as key>
<#assign value = myMap[key]>
<li>${key} = ${value}</li>
</#list>
Run Code Online (Sandbox Code Playgroud)
在Freemarker文档中,它描述了如何访问Enum本身,但我没有找到任何关于如何使用Enum作为键从哈希值中获取值的信息.
谢谢.
是否可以在freemarker模板中引用包含点的变量?(或' - '字符)
例如:
Main.java:
public class Main {
public static void main(String[] args) throws IOException, TemplateException {
Configuration freemarkerConfig = new Configuration();
freemarkerConfig.setClassForTemplateLoading(Main.class, "");
Template template = freemarkerConfig.getTemplate("template.ftl");
Map<String, String> data = new HashMap<String, String>();
data.put("variable.name", "value");
Writer out = new StringWriter();
template.process(data, out);
System.out.println(out.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
template.ftl:
${variable.name}
Run Code Online (Sandbox Code Playgroud)
此代码抛出异常:
The following has evaluated to null or missing:
==> variable [in template "template.ftl" at line 1, column 3]
Run Code Online (Sandbox Code Playgroud)
是否可以在tempalte文件中引用变量"variable.name"?
我已经使用$ {.now}来获取freemarker模板中的当前时间戳,但我想知道我怎么才能获得这一年?
我在FTL文件中创建了一个DIV,DIV包含表单现在说我有另一个FTL文件,我想在第二个FTL文件中使用第一个FTL的div这是可能的
<div id="filterReportParameters" style="display:none">
<form method="POST" action="${rc.getContextPath()}/leave/generateEmpLeaveReport.json" target="_blank">
<table border="0px" class="gtsjq-master-table">
<tr>
<td>From</td>
<input type="hidden" name="empId" id="empId"/>
<td>
<input type="text" id="fromDate" name="fromDate" class="text ui-widget-content ui-corner-all" style="height:20px;width:145px;"/>
</td>
<td>Order By</td>
<td>
<select name="orderBy" id="orderBy">
<option value="0">- - - - - - - Select- - - - - - - -</option>
<option value="1">Date</option>
<option value="2">Leave Type</option>
<option value="3">Transaction Type</option>
</select>
</td>
</tr>
<tr>
<td>To</td>
<td><input type="text" id="toDate" name="toDate" class="text ui-widget-content ui-corner-all" style="height:20px;width:145px;"/>
</tr>
<tr>
<td>Leave Type</td>
<td>
<select name="leaveType" id="leaveType">
<option …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Freemarker和Java配置创建Spring MVC应用程序.
我习惯使用JSP,但我想我会给Freemarker一个旋转.
我添加了Freemarker作为依赖,并且Maven正在下载jar:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用3.2.3.RELEASE版本的Spring.
我的MVC配置,通过阅读类似问题组装而成:
@ComponentScan(basePackages="yhj.*")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer result = new FreeMarkerConfigurer();
result.setTemplateLoaderPath("/WEB-INF/views/");
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到一个编译错误,我的IDE告诉我无法解析方法'setTemplateLoaderPath(String)'.
那么......看起来像是使用Freemarker配置Spring MVC的正确方法吗?如果这是问题,我需要添加到我的POM的依赖项是什么?
freemarker ×10
java ×8
spring-mvc ×4
spring ×3
enums ×2
date ×1
escaping ×1
html ×1
jetty ×1
jsp ×1
key ×1
map ×1
maven-2 ×1
servlets ×1
templating ×1