I am learning java ee related technologies (and I am relatively newbie) and try to write simple web application with Spring and Thymeleaf as frontend, and i have some problems with
modularization.
I found that it is used to pack .html filesinto WEB-INF directory for security purposes, but, as far as i guess, it means I have to put there all of my views, have controllers in this projects. I want to avoid that by having many projects for example:
web-app
admin-module
user-module
core-module
and each of them has its own, module specific controllers and views (htmls) and web-app
combines them all into single, big project.
In Thymelaf i found something like ClassLoaderTemplateResolver and got (maybe stupid idea) how to divide big project into many ones - put htmls into classpath resource directory.
By this I can ask for any html view from any module project. Does this violates some security reasons?
I would like to ask does anyone has similar problem and found proper solution how to elegantly divide html web-app project into many smaller 'context-specific' projects?
Here is my current project structure. Blue arrow idicates which view is connected to which controller:
http://prntscr.com/4tqt9m
This is my first question asked here so I hope you will treat me with some understanding.
Thank you in advance.
You might be able to merge multiple Spring and Thymeleaf modules at runtime using classpath lookup. Just make sure you give different qualified name / folder structure (eg: two files named views/index.html on different packages might cause confusing inconsistencies)
With Spring if you configure the classpath scanning properly it should take into account jar packages on the classpath.
With Thymeleaf, try setup a TemplateResolver with prefix such as classpath:/templates/, then on different modules setup a folder structure such as src/main/resources/templates/package-a/foo.html, src/main/resources/templates/package-b/foo.html. I haven't yet tried this, and not 100% sure the class loader can merge these alright.
Related
I have a situation wherein i have multiple war files with 90% code base same (controllers, html files etc).
They have some custom code for each along with different configurations.
Currently these projects are nothing but a copy of each others (with minor tweaks and config changes).
How should i approach this for creating a single module/jar from where these multiple wars can be created with minim code(their respective changes) in the war modules.
So, the common module will have all the code along with the html files and in each war module i'll just override/provide different implementation.
I am using Gradle for build.
Using Java, Spring.
Say, I have a Project FOO.
It's a web project with java classes, html, properties and configuration (beans, persistence) files and web.xml file.
I have another project BOO.
which is a copy of FOO but there are some property changes, some code changes to existing implementation and some new code of it's own.
I dont want to keep duplicate code in BOO, BOO should ideally have the new code and the changed implementation.
This is what i want to achieve.
I cant post the code as its a big project.
Hope this makes it clear.
This is perfectly a refactoring project. Your main concentration area are all config files and default servlet and default html pages. As your project codes are not available here we can not directly advice you any more. You have to carefully check configuration of each project and find a solution how you can club together all the config files. Especially concentrate in spring-bean.xml and web.xml. You must be very careful else it will take ages for you to merge all the application. Good luck.
I'm searching for a hint/solution to generate one Template-Webapp with a custim design and then all my other Webapps should take the design from the Template-Webapp. Is that possible? The Idea behind is, that we I have multiple Webapps for Projects, but the Layout,... is the same on all Project. And when changing the Template-Webapp, all other Webapps are changed without changing the source-code in all Webapps.
I've got a running Webapp with my design(Template-Webapp). All is runing on a Tomcat 7. In another Webapp, I tried to use the template-file from the Template-Webapp, but I can't get it running. Perhaps its not possible to navigate through the webapp-folders on Tomcat? It's showing "invalid path"-failure. But, the path is correct. With selecting the path, I can easy access the template-file.
Does anybody had the same problem or knows how to solve it ?
Thanks for every help, even if it's small
You should look into breaking your front-end pieces up.
It ends up being like moving magnets around on a refrigerator. Except the magnets are components of your template!
It is called templating with composite values.
My Related Answer:
How does JSF render more than one JSP pages into 1 single view?
Documentation:
JSF 2 fu: Templating and composite components
Core J2EE Patterns - Composite View - Oracle
Hope that helps!
Take a look on some webresources how to work with templating (JSF > 2.0) and contracts (JSF > 2.2).
http://www.mkyong.com/jsf2/jsf-2-templating-with-facelets-example/
http://hantsy.blogspot.de/2013/08/jsf-22-theme.html
The best way to share one template across many projects is to bundle the contract including all recources into one *.jar
As far as i know, the template/contract/whatever should be in the right directorys, thus within the same project at least included as jar.
I am working on project in JSF, using Tomcat 7. The application will have two parts - the presentation and the administration, main part is about the administration. What I want to do is to create something like a web library.
To have this more clear, I'll try to show structure of the project:
The main application project (let's call it admin) that is builed into WAR file.
Second project using the first one (let's call it presentation).
Presentation is using the admin WAR file.
both projects are typical JSFs - admin has pages, beans, etc.
In NetBeans, I have no problem with adding WAR file as a library, but, there are two things. The first: are all ManagedBeans in admin initialized together with presentation run, so I can use them in presentation? The second: how do I access pages from presentation that are located in admin?
Maybe I am wrong about this idea and I should use different way how to achieve this - so just tell me please.
Also, I probably will not be able to deploy two war files and run them - most of the hostings where the application will run allow deploying of one WAR file only.
Make it a common JAR instead of WAR, so that you can include it in both WARs. This way you can share managed beans and templates between both WARs. The Facelets resources can just be placed in /META-INF/resources of the JAR. The JSF artifacts like managed beans will be auto-discovered if you provide a /META-INF/faces-config.xml file.
See also:
Structure for multiple JSF projects with shared code
I want to build a java web application and I don't have any background how to do that.
Can you plz tell me what is the starting point to do that and where can I found useful open source codes that I can use them to design my web application.
There are many different frameworks and without more information it's difficult to know what would suit you.
http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#Java is a good starting point.
You have to know concepts such as Servlet, Servlet Container, Application Server(such as Apache tomcat) and little information about Html.
Exist several book for this goal, my opinion is : you start by a book related to Jsp/Servlet concept, these books good explained.
Here you can learn how java web applications work and here is a very basic java web application example to get you started. I hope this helps :)
You should follow the Java EE tutorial, its Web Tier part. I think it's the fastest way to get knowledge that would allow you to understand the base concepts...
The minimal structure of a web application is the following:
/WEB-INF
/classes - stores the compiled Java classes your webapp uses
/lib - contains the additional libraries your webapp may need to run
web.xml - key file in every webapp; explained below
web files and folders (HTML/JSP/CSS/Javascript)
You may want to start out with Eclipse for Java-EE, since it automatically creates the webapp structure for you, so it's the perfect place to start learning, in my opinion; you can find it here.
After you install, the basic steps to create your web application are:
Create your project by accessing File > New > Dynamic Web Project.
Name your project, click Next, Next and check the Generate deployment descriptor checkbox. Now hit Finish.
Now that the structure is created, your main points of interest will be:
Deployment Descriptor - Is an overview of your web.xml file. Here you can declare all your servlets and their URL paths, you can point to specific error pages triggered by specific codes (e.g 404, 500) or exceptions that occur in your Java/JSP code (e.g NullPointerException, FileNotFoundException), plus do many other things to enhance your webapp. You can trigger between text and graphical XML editing in the bottom-left of the code window.
Java Resources - Here you define your Java classes and servlets. The main role of a Java class in a webapp will be to collect and process data. For example you can define your own math class that exposes methods which do basic calculations. A servlet will usually call one of these classes and output the result to the response output stream. Be sure to provide a solid project structure with the help of packages.
WebContent - this will contain all the web pages your webapp will show, including scripts, images and stylesheets. You are free to create your own folder structure in this section.
Some useful tutorials to get you started:
HTML
JSP
Servlets, Server setup
CSS
Once you're done with your webapp, you can either Run it on a server directly from Eclipse, or you can export it as a WAR file and deploy it on the server of choice, which is usually done by copying the WAR file in the webapps folder.
Finally, try to experiment with all the webapp features Eclipse exposes to you. Good luck!
I am starting a new Java Web Project which is using Hibernate and a standard MVC Architecture.
I have just started to layout the projects structure and while doing this I started to look around to see if there was any standards in this area, about where Controllers should go and generally the best way to lay everything out. However I have not really found any guidelines.
So what I am curious to know is
Is anyone aware of any best practise guidelines for the layout of a Java Web Project?
Does anyone have a particular set of hard rules that they always follow for different types of project?
Do people tend to split packages by the different layers such as presentation, business, and application?
It really depends on your web framework.
For example if you use Wicket, java files and webpages co-exist in the same directory while
in most other frameworks, pages (.jsp files or whatever is your presentation engine) and
code-behind stuff (java files ) are completely separate.
So read the documentation that comes with your framework (Spring MVC, Struts, JSF e.t.c).
Another good proposal is to use Maven Archetypes to generate a skeleton for your specific framework. Some web frameworks (such as seam) have even their own code generation tool that lays the foundations for your web project.
My only good suggestion (that is not mentioned by Yoni) for the src directory is
to make packages according to business purpose and NOT according to type/layer
That means packages for
com.mycompany.myproject.customers
com.mycompany.myproject.departments
com.mycompany.myproject.billing
com.mycompany.myproject.reports
com.mycompany.myproject.admin
and NOT
com.mycompany.myproject.entities
com.mycompany.myproject.tables
com.mycompany.myproject.graphs
com.mycompany.myproject.dialogs
com.mycompany.myproject.servlets
The second structure is too generic, tends to resolve around huge packages with unrelated stuff and is hard to maintain.
To continue my previous answer, I have many web projects. In all of them the structure under src is more or less the same. The packages are roughly separated to 3 logical layers.
First is the presentation layer, as you said, for servlets, app listeners, and helpers.
Second, there is a layer for the hibernate model/db access layer. The third layer for business logic. However, sometimes the boundary between these layers is not clear. If you are using hibernate for db access then the model is defined by hibernate classes so I put them in the same area as the dao objects. E.g. com.sample.model holds the hibernate data objects and com.sample.model.dao hold the dao objects.
If using straight jdbc (usually with Spring), then sometimes I find it more convenient to put the data objects closer to the business logic layer rather than with the db access layer.
(The rest of the stuff typically falls under the business layer).
First, the to follow the conventional structure of a popular ide, ala Eclipse, Netbeans, etc.
In Eclipse, for example, everything is arranged already with a WEB-INF and META-INF folders, so packaging and deployment is easy. Classes source code (typically under src) is automatically copied to WEB-INF/classes.
There are a few other considerations:
If you are using MVC, then it is possible that you don't need to access your JSPs directly. If so, keep the JSP source code under WEB-INF/jsp, for security reasons.
Similarly, keep custom tag files under WEB-INF/tags.
Keep javascript files in js folder, css files in style folder, etc. All folders should be at the same level as WEB-INF to mimic a real deployment.
It is good to separate your code into packages according to layers. Obviously your Hibernate daos don't need to be at the same package as your servlets.
If you end up with too many servlets in the same package, consider sub-packaging them accord to functionality, but it is nice that they have a common ancestor package - it helps with readability.
Java Web Structure
Use the Maven webapp archetype layout.
project
|-- pom.xml
`-- src
`-- main
|-- java
`-- webapp
|-- WEB-INF
| `-- web.xml
`-- index.jsp
I've included the java folder in the example here, maybe it was obvious, but it was left out in the above link for some reason.