简体   繁体   English

我应该将html和java文件放在Apache Wicket的同一个包(文件夹)中吗?

[英]Should I put html and java files in the same package (folder) in Apache Wicket?

我想知道是否有一个例子,html文件和java文件驻留在不同的文件夹中。

I don't recommend using a separate page directory unless you are quite comfortable with how resource streams work, which I am not. 我不建议使用单独的页面目录,除非您对资源流的工作方式非常熟悉,我不是。

The vast majority of wicket projects I have seen keep class and html files in the source directory. 我见过的绝大多数wicket项目都将class和html文件保存在源目录中。 I tried separating them myself but then found that getting my hands on other resources, like images and such, was a hassle; 我试着把它们自己分开,然后发现把手放在其他资源上,比如图像等等,这很麻烦; so, I wound up putting those resources in the package directory - in the end I had resources in several different places and it was more of a mess than putting everything in package directory would have been. 所以,我最终把这些资源放在了包目录中 - 最后我在几个不同的地方有了资源,而且把它放在包目录中就好了。

That said, here's the code I used to put my html templates in a separate folder. 也就是说,这是我用来将我的html模板放在一个单独的文件夹中的代码。 It should be added to Init() in your application class. 它应该添加到应用程序类中的Init()。

IResourceSettings resourceSettings = getResourceSettings();
resourceSettings.addResourceFolder("pages"); //the full path to your folder, relative to the context root
resourceSettings.setResourceStreamLocator((IResourceStreamLocator) new PathStripperLocator());

https://cwiki.apache.org/confluence/display/WICKET/Control+where+HTML+files+are+loaded+from has a tutorial on this. https://cwiki.apache.org/confluence/display/WICKET/Control+where+HTML+files+are+loaded+from有一个关于此的教程。

I use Maven (as the user above points out) and typically put all my .html pages in src/main/resources/same/package/name/as/corresponding/java/file 我使用Maven(作为上面的用户指出)并且通常将我所有的.html页面放在src/main/resources/same/package/name/as/corresponding/java/file

I find this works nicely as my designers can checkout the base package from the resources folder and they don't get confused by all the .java files (and more importantly they don't accidentally change them!) 我发现这很好用,因为我的设计师可以从资源文件夹中检出基础包,并且不会被所有.java文件混淆(更重要的是,它们不会意外地改变它们!)

I made a post to the mailing list if you are interested. 如果您有兴趣,我会在邮件列表上发帖子

I'm not a fan of having the HTML files residing inside the src/main/resources folder. 我不喜欢让HTML文件驻留在src/main/resources文件夹中。 I think the simplest way to handle this and obtain a clean separation in version control and within your project is to use the last set up in the Wicket article linked by perilandmishap: 我认为处理这个并在版本控制和项目中获得清晰分离的最简单方法是使用perilandmishap链接的Wicket文章中的最后一个设置:

<project>
[...]
 <build>
   <resources>
     <resource>
       <filtering>false</filtering>
       <directory>src/main/html</directory>
     </resource>
   </resources>
[...]
 </build>
[...]
</project>

So, now all your HTML files will be in the new, separate HTML folder but still merged with your Java classes. 因此,现在所有HTML文件都将位于新的单独HTML文件夹中,但仍与Java类合并。 Just remember you would still use the package naming scheme within the HTML folder, ie src/main/html/com/your/package 记住你仍然会在HTML文件夹中使用包命名方案,即src/main/html/com/your/package

I use seperate folders for my java and html wicket source files, but my ant build process then copies the html to the classes folder of my web app so i avoid issues of having to setup wicket resource settings. 我为我的java和html wicket源文件使用单独的文件夹,但我的ant构建过程然后将html复制到我的web应用程序的classes文件夹中,这样我就避免了必须设置wicket资源设置的问题。

My basic build.properties has 我的基本build.properties有


web.root            = ${project.home}/web
web.java.dir            = ${web.root}/java
web.html.dir            = ${web.root}/html

war.root            = ${project.home}/war
web.inf.dir         = ${war.root}/WEB-INF
war.lib.dir         = ${web.inf.dir}/lib
war.classes.dir         = ${web.inf.dir}/classes

wicket.version          = 1.3.5
wicket.jar          = ${war.lib.dir}/wicket-${wicket.version}.jar
wicket-extend.jar       = ${war.lib.dir}/wicket-extensions-${wicket.version}.jar
wicket-spring.jar       = ${war.lib.dir}/wicket-spring-${wicket.version}.jar

and the compile / assemble ant targets look like 并且编译/汇编ant目标看起来像

<target name="compile-web">
    <path id="wicket.build.classpath">
        <filelist>
            <file name="${wicket.jar}"/>
            <file name="${wicket-extend.jar}"/>
            <file name="${wicket-spring.jar}"/>
            <file name="${externaljars.path}/spring/2.5.6/spring.jar"/>
        </filelist>
    </path>

    <javac destdir="${war.classes.dir}" classpathref="wicket.build.classpath" 
        debug="on" srcdir="${web.java.dir}">
        <include name="**/*.java"/>
    </javac>
</target>

<target name="assemble-war" depends="compile-web">
    <copy todir="${war.classes.dir}" overwrite="true">
        <fileset dir="${web.html.dir}"/>
    </copy>
    <delete file="${dist.dir}/validationservice.war"/>
    <war destfile="${dist.dir}/validationservice.war" webxml="${web.inf.dir}/web.xml" basedir="${war.dir}">
    </war>
</target>    

This is one area where using Maven to manage your project is very nice. 这是使用Maven管理项目的一个领域非常好。 Since Maven has two locations that are included on the classpath, you can logically separate the Java source and the HTML files and still have them maintain the same package structure. 由于Maven有两个包含在类路径中的位置,因此您可以在逻辑上将Java源文件和HTML文件分开,并使它们保持相同的包结构。 The Java code goes into src/main/java and the HTML goes into src/main/resources. Java代码进入src / main / java,HTML进入src / main / resources。 When building or running the code both of these locations are added to the classpath. 构建或运行代码时,这两个位置都会添加到类路径中。

If Maven isn't for you perhaps this idea could be applied to whatever environment you are using. 如果Maven不适合您,也许这个想法可以适用于您正在使用的任何环境。

I would put Java and HTML files into the same folder. 我会将Java和HTML文件放在同一个文件夹中。 That would make it much more comfortable to pick the HTML file corresponding to a Java class. 这样可以更轻松地选择与Java类相对应的HTML文件。 Think of a components HTML file as a sort of UI description that would otherwise be written in Java (like with Swing). 将组件HTML文件视为一种UI描述,否则将用Java编写(就像使用Swing一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将包含java文件的文件夹放入eclipse的包浏览器中 - How do I put a folder with java files into eclipse's package explorer 在Java中,应该创建新的Package,Folder还是Source Folder? - In Java, should I be creating a new Package, Folder, or Source Folder? 我可以在Tomcat上将相同Java包的类文件拆分为jar文件和classes文件夹吗? - Can I split class files of same java package into jar file and classes folder on Tomcat? 什么应该放入项目,文件夹和包 - What should be put into the project, folder and package 如果它们属于同一个文件夹,是否需要在java文件中声明程序包名称? - Is it necessary to declare package name in java files if they belong to same folder? 我应该将Angular文件放在同一个JEE项目中吗? - Should i put my Angular Files in same JEE Project? 我可以将Java文件放在pydev项目的文件夹中吗? - Can i put java files in a folder in a pydev project? 我应该将 SQL 文件放在我的 Java 项目中的什么位置? - Where should I put the SQL files in my Java project? 编译/运行Java文件时应在哪里放置-cp - Where should I put -cp when compiling/running java files 如何使用Apache Wicket将多个复选框放入表单? - How to put multiple checkboxes into a form with Apache Wicket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM