我一直在熟悉FreeMarker,它是Java的模板引擎.
我到了能够通过哈希映射将对象传递给模板引擎的地步.这没关系.但是一旦我尝试将任何类型的多个对象集合传递给FreeMarker,它就会给我一个freemarker.template.TemplateException并抱怨它"预期的集合或序列.作业被评估为freemarker.template.SimpleHash".
根据我在各种资源中阅读的理解,这是可以预料的.
现在,我做了大量的腿部工作,发现很多人评论如何解决这个问题.但是,坦率地说,(a)对于许多例子,我不清楚他们的建议在我的案例中是如何适用的 - 尽管我已经知道Java基础知识很长一段时间我对一些架构很新关于Java Web应用程序和(b)我对哪种方法是最好的方法感到困惑.
在最简化的层面上,我想要做的就是基本上:
我有一个简单的Servlet.
我有一个简单的类(对于这个名为Invoice的例子),它有一些方法和属性.
我想让我的servlet(以某种方式)通过FreeMarker的处理方法呈现这些对象(或这些对象的视图)的实例的列表/数组/序列/散列图.
我想让我的.ftl模板循环遍历list/array/sequence/hashmap并显示方法结果,如下所示:
< # list invoices as invoice>
Invoice note: ${invoice.getNote()}, Invoice Amount:${invoice.getAmount()}
< / # list>
Run Code Online (Sandbox Code Playgroud)
现在,我不一定在寻找快速而肮脏的解决方案.我是FreeMarker的新手,但我希望以优雅和优秀的设计正确的方式做到这一点.所以我愿意完全重新思考这种方法.有人可以帮助我看看我需要做些什么才能让这样的事情发挥作用?
我最近在我的Django模型中遇到了一个ForeignKey冲突.我需要有两个外键(owner,assigned_to)最终指向同一个模型(用户).
根据我的理解,我需要一个related_name论据来解决这个问题.所以我这样做了:
assigned_to = models.ForeignKey(TaskUser, blank=True, null=True, related_name='user_assignment')
Run Code Online (Sandbox Code Playgroud)
和
owner = models.ForeignKey(TaskUser, related_name="user_ownership"
Run Code Online (Sandbox Code Playgroud)
但我仍然收到一个错误:
tasks.task: Accessor for field 'owner' clashes with related field 'TaskUser.user
_ownership'. Add a related_name argument to the definition for 'owner'.
tasks.task: Reverse query name for field 'owner' clashes with related field 'TaskUser.user_ownership'. Add a related_name argument to the definition for 'owner'.
Run Code Online (Sandbox Code Playgroud)
为什么我仍然会收到此错误?
有一个catch,owner在超类(BaseWidget)中并且assigned_to在子类(Task)中.related_name在继承关系中使用是否存在问题?我是否需要覆盖所有者的继承并related_name在子类中重新定义?我很感激任何帮助!
我有一个jsp包含一个看起来像的CSS链接
<link type="text/css" href="/css/login-min.css" rel="stylesheet" />
Run Code Online (Sandbox Code Playgroud)
为了防止浏览器缓存css文件,我们用css login-min.css的名称和时间戳或版本号替换
login-min.css?t=432432423423...
Run Code Online (Sandbox Code Playgroud)
在蚂蚁我会做类似的事情
<tstamp>
<format property="current.time" pattern="MMddyyyyhhmmssaa" offset="-5" unit="hour" />
</tstamp>
<replace dir="${deploy.path}/${name}/WEB-INF/jsp" value="login-min.css?t=${current.time}">
<include name="includes/login_css_include.jsp" />
<replacetoken>login-min.css</replacetoken>
</replace>
Run Code Online (Sandbox Code Playgroud)
对于gradle我已经更新了jsp页面
<link type="text/css" href="/css/@loginCSS@" rel="stylesheet" />
Run Code Online (Sandbox Code Playgroud)
在build.gradle中
import org.apache.tools.ant.filters.ReplaceTokens
war {
webInf {
from ("${webAppDir}/WEB-INF/jsp") {
include: "/includes/login_css_include.jsp"
filter(ReplaceTokens, tokens: [loginCSS: 'login-min.css?v=1'])
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
这个有效,但它改变了源...我只想修改战争中的文件.
import org.apache.tools.ant.filters.ReplaceTokens
war {
webInf {
from ("${webAppDir}/WEB-INF/jsp/includes/login_css_include.jsp") {
it.eachFile {
ant.replace(file: it.file, token: "@loginCSS@", value: "login-min.css?v=1")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我是个全新的朋友,我完全不正确吗?以前有人需要这样做吗?使用gradle 1.0-milestone-1.
谢谢
我在我的一个Django模型中有一个DateTimeField.
completed_date = models.DateTimeField('date completed', blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
我已将其定义为允许空值和空值.但是,当我尝试创建模型的实例时,我收到以下错误:
/ admin/tasks/project/add /中的IntegrityError
tasks_project.completed_date可能不是NULL
我正在使用Django 1.25和Python 2.7.任何人都知道为什么会这样吗?有什么办法可以解决这个问题吗?
我有一个用IntelliJ构建的Maven/Spring Boot项目.据我所知,一切似乎都在IntelliJ中很好地运行.
我已将其设置为打包到嵌入式Tomcat的独立jar:
<packaging>jar</packaging>
Run Code Online (Sandbox Code Playgroud)
我在用:
spring-boot-starter-parent
Run Code Online (Sandbox Code Playgroud)
所以,我的理解是我不需要在我的pom文件中的执行标记下"重新包装".
我想开始尝试部署项目,然后尝试进行构建以获取JAR.但是当我右键单击"Maven Projects"下的项目时,"Run Maven Build"箭头显示为灰色.关于可能是什么原因或我应该仔细检查的任何想法?
我有一个 Spring boot 应用程序,当从命令行运行时,它可以完美地记录到文件系统(通过logging.level/logging.file属性):
java -jar jarfilename.jar
Run Code Online (Sandbox Code Playgroud)
但是,当我将以下内容放入 pom.xml 中以创建可执行 JAR,然后尝试将其作为 Ubuntu 中的 init 脚本运行时,日志记录根本不会发生。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
以下是我在 Linux 上运行的命令:
ln -s /path/to/executablejar.jar /etc/init.d/myapp
chmod a+x /etc/myapp
/etc/init.d/myapp start
Run Code Online (Sandbox Code Playgroud)
我想这一定是日志记录配置的某种问题,没有考虑可执行 jar 的启动方式,但我很困惑并且希望得到任何指导。
这是我在 application.properties 中的日志记录设置:
logging.level.org.springframework=error
logging.level.com.myapp=debug
logging.level.org.hibernate=error
logging.file=/path/to/a.log
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的代码片段,用于填充向量,遍历它,然后清除它.这基本上就是我原则上的尝试:
vector v = new Vector();
v.add(1);
v.add(2);
v.add(3);
ListIterator iter = v.listIterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
v.clear()
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个ConcurrentModificationException.
从阅读中看,显然以某种方式使用"同步"是解决方案.但我看到了几种不同的方法,我想知道在我的情况下解决这个问题的最佳,最简单的方法是什么(没有明确的线程参与)?
另一个列表字典问题.
我有一个dict如下,列表中的单位名称和测试名称:
dictA = {('unit1', 'test1'): 10, ('unit2', 'test1'): 78, ('unit2', 'test2'): 2, ('unit1', 'test2'): 45}
units = ['unit1', 'unit2']
testnames = ['test1','test2']
Run Code Online (Sandbox Code Playgroud)
我们如何在测试名中找到每个测试的最大值:
我尝试如下:
def max(dict, testnames_array):
maxdict = {}
maxlist = []
temp = []
for testname in testnames_array:
for (xunit, xtestname), value in dict.items():
if xtestname == testname:
if not isinstance(value, str):
temp.append(value)
temp = filter(None, temp)
stats = corestats.Stats(temp)
k = stats.max() #finds the max of a list using another module
maxdict[testname] = k
maxlist.append(maxdict)
maxlist.insert(0,{'Type':'MAX'}) …Run Code Online (Sandbox Code Playgroud) 当我尝试使用createQuery()运行HQL查询时,我正在努力找到空指针异常的底部.
运行查询的代码非常简单.最初我有一个我正在调用的命名查询,但只是为了使事情变得更简单并消除我正在做的任何复杂情况(springwildlife是包,而Species是类/实体):
Query q = session.createQuery("SELECT s FROM springwildlife.Species s");
Run Code Online (Sandbox Code Playgroud)
(顺便说一下:我也曾尝试使用和不使用包名.我还尝试过更简单的"FROM springwildlife.Species"查询)
我已经创建了一个XML映射(species.hbm.xml),如下所示:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="springwildlife.Species" table="species">
<id name="id" type="java.lang.Long" column="id" >
<generator class="native">
<param name="sequence">species_id_seq</param>
</generator>
</id>
<!-- in the actual file I have one these for each item I want to be mapped -->
<property name="propertyFromClass" type="java.lang.String">
<column name="sql_table_column_name" />
</property>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
在我的hibernate.cfg.xml文件中,我像这样拉入映射:
我的类/实体看起来像这样:
package springwildlife;
public class Species
{
// properties here
public Species()
{
}
// …Run Code Online (Sandbox Code Playgroud)