Spring MVC and Angularjs - java

Currently I´m trying to learn Angular JS, but firstly I want to setup my environment with Spring mvc.
At the moment I only want to work with rest, but I have a doubt for what is the best way to place the resoucres in Spring MVC
My simple applicaction has this squeleton:
my-simple-app:
src
main
java
resources
webapp
resources
WEB-INF
If I want to put the app folder from the angular-seed, what is the best place to put it?
I tried to put in src/webapp/resources/app but then I have to move the html files to WEB-INF?
How was your skeleton in your angular-js spring mvc applications?
What is the best way to do the redirect to the app/index.html? to the welcome file and then work only with angularjs $routeproviders?
Thanks!

As far as I can understand, your Front End technology is Angualar JS and your Back End technology is Spring MVC.
I'm a Front End developer and hence I can provide you the advice on the structure of your HTML, CSS, and Javascript.
Here are my recommendations:
Mode Of Communication Between Front End and Back End: JSON (Should be strictly followed for MVC Pattern)
File Location: All your Front End files should be in WEB-INF folder with this structure:
WEB-INF/Assets: All your JavaScript Files, JavaScript Libraries, Images, CSS etc. Should Be Places Here. You Can Open A Separate Folder Each Resource Type Inside Assets
WEB-INF/JSP: All Your JSPs should be placed here. As Angular's greatest strength is Single page Application, you can create one JSP per main page and place them here
WEB-INF/HTML: All the static resources that would be injected into the JSPs using <ng-view>or <ng-include> can be placed here
Hope this helps!

The easiest way to get going is to take all the files in the angular-seed/app directory and copy them into your src/main/webapp directory. After copying these files, you should be able to redeploy the app and have a running sample.
Most servlet containers will include index.html as a default welcome file. If yours doesn't, you can add that config in web.xml <welcome-file-list>.
The WEB-INF directory is for web resources that should not be exposed directly to the web. web.xml is a example of a file that should not be exposed to remote users. In this case, it's safe to expose all of the app's resources directly to the web; thus you don't need to place the resources under WEB-INF.

I recommend this structure for your project:
my-simple-app:
src
main
java
controller
MySpringCtr.java
models
Person.java
House.java
*.java
webapp
WEB-INF
resources
css
style.css
*.css
js
angularCtr.js
*.js
pages
index.jsp
*.jsp
mvc-dispatcher-servlet.xml
web.xml
I found a tutorial for beginners which explains step by step how to combine SpringMVC and AngularJS, you can find the tutorial and the complete code in this blog I hope it will be useful :)

Related

How to build Angular6 project while having html,css and js files

I am developing a website using angular6 and spring boot with microservice architecture.
I have got the UI from designer team in the form of HTML, CSS, and JS file, whose structure is attached here HTML and CSS files
Also, I had created a angular6 project named DIS, whose structure is attached here Angular6 project
What will be the best way to start with angular 6?
How to include these files in angular6 project considering microservices architecture?
If i understood your question correctly.Yes you can do that.
Step 1: You need to create the Necessary components, Modules needed to build your website.
Step 2: Replace the components with the necessary HTML file and CSS file. As you generate a component with CLI you should have those 2 files just replace them.
When you have global CSS files that can be shared among other files as well. You can configure using Angular CLI.
Regards to microservices architecture, that does not have anything to do with your front end applicaiton, its totally on the server side. But you can consider while creating the components based on the services you have.
ng new ng6-proj --style=scss --routing
Just make changes in the main style.scss that will apply globally.
Images you can keep it anywhere, you just need to use it by providing its path

Where to keep JS and HTML files using both Thymeleaf and AngularJS?

I am starting to build a Spring Boot web application on top of the Dave Syer's example here. He uses plain html files with AngularJS, that's why he has a file structure like this:
Everything is under static folder. Since the example application is really small, he has created a folder for each page (which i will definitely change in the future).
But i also want to use Thymeleaf and it uses the templates folder as default, as the spring boot defines this default property:
spring.thymeleaf.prefix=classpath:/templates/
Now i do not know if i simply should overwrite that prefix property in my application.yml to sth like spring.thymeleaf.prefix=classpath:/static, or create a template folder and move all my html files in that folder, leaving just the JS files in static. What is the best way of having this combination?

Using Maven, in which directory should I place the image files for my icons?

I'm thinking that I should put them in
C:\ProjectName\src\main\resources\com\company\projectname\icons
but I want to be sure that I'm using Maven's best practices.
Resources is as good a place as any.
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
I am having the same problem here. I search for almost half a day trying to get some help on this subject. There is not much useful information on this subject. I did some experiment on my end. Still, I would wish more experienced expert's opinion. Here is what I found out.
My project is maven, and I am using Tomcat 7. Java 7, which may be irrelevant.
My directory layout (ignoring irrelevant ones) :
src
main
java
resources
images
mytest.gif
META-INF
webapp
images
mytest.gif
js
WEB-INF
*.jsp
If you look at the war file, the images in the resources directory is under
WEB-INF/classes/images/mytest.gif; whereas, the images in the webapp directory is publically accessable simply as images/mytest.gif.
Since I use both servlet and JSP in my web application, the question is which location for the images directory is better? It looks that the webapp to be better because the JSP is good for view whereas the servlets are good for logic and model. There is no way that you can access the images located in the resources directory from the JSP (possible if you go through the proxy of another servlet). Images don't seem to contain secrets, so you should make them public.
Or you may keep both directories, one for images containing secrets and the other don't.
It all depends what you want to do.
1) If you are going to load images using serveletContext then resources folder is the right one.Because whatever in the resources will end up in the WEB-INF.
2) If you want to load them directly by their path from the root of the War folder, then you will need the maven WAR plugin.

User Downloads In Java Spring

I need to make a link in my Java Spring application that will allow the user to download a CSV file . I have the CSV file in my project's root directory and I have an tag in my view that runs a Javascript function when it's clicked. Is there any way I can use either Javascript or the Java Spring framework to allow the user to download the CSV file? I'm a noob Spring user/developer so any help is appreciated. :)
Put the file in your WebContent (or whatever you call the web resources directory) and link it directly. Everything below WebContent (but not in WEB-INF) is accessible in your context root.
So file WebContent/test.csv is accesible with relative url /test.csv.

how to start coding and setting up a web server with java?

so now i think i have learned all basics and terminology for java. but what i don´t know is how to code and display a web page with Netbeans in Java.
the most tutorials contains lots of talks about different technologies "Java uses Java Beans, JSP and servlets" and so on. Where can i find short practical tutorials that actually teach me where to code what and then compile and where to put all the files (war, jar, ear..) in Glassfish to be able to see the output from a Web browser. Simples things that makes one understand all these different "layers" which are just classes using classes. Feels like i never get to know how i can put up a web server with Java cause I can´t find this kind of tutorials.
Would be great if someone could send some links to such practical stuff.
Thanks.
This is the first such document I found: http://www.java-tips.org/java-tutorials/tutorials/introduction-to-java-servlets-with-netbeans.html
More:
http://netbeans.org/kb/docs/web/quickstart-webapps.html
http://blogs.oracle.com/jonasdias/entry/webservices_with_jsp_on_netbeans
http://www.fuzzylizard.com/archives/2005/09/18/628/
http://cit.wta.swin.edu.au/cit/subjects/CITP0014/tutorials/netbeans/tomcat/Running_Tomcat_from_Netbeans.html
http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/
I even found a small ebook on this (PDF!) http://www.comp.dit.ie/bduggan/Courses/projects/Getting%20Started%20with%20Tomcat%20&%20NetBeans.pdf
How do I code and display a web page with NetBeans in Java?
Let's go! Fire up NetBeans. I'm using NetBeans 6.7.1 with the Java EE stuff installed, and I've got a GlassFish installed and tied up, so I don't have to care about that stuff. Your setup might differ in the details.
Do a File->New Project, and pick "Java Web" from the categories. Select "Web Application" and hit Next. Enter a project name and tweak the location, if liked. Hit Next. The next page should have a server selection drop-down; as hinted above, mine has "GlassFIsh v2.1" selected. That's fine - as long as NetBeans can interact with a Java application server of some sort, this crash course will run okay.
Make a note of the "context path" - this will be based on the project name, and basically forms the base of the URL at which your application will reside. Hit Next. Ignore the next page, for now, which talks about various frameworks, and hit Finish.
Churn, churn. You should eventually see your web project created. It's a very simple application which contains a single JSP file, and that will be open in the main editor. It's got a bunch of HTML in it, and some JSP syntax.
Take a look at the project structure. You've got a "web pages" folder which contains a WEB-INF directory, and an index.jsp file. That's the same file you're looking at. WEB-INF is a standard directory which contains the metadata used to deploy your application, and also the compiled classes that power it.
The only thing you should need to do now, in order to get to the original objective, is to hit the big ol' Run button, or right-click on the project and select "Run" from the menu. NetBeans will compile, and then fire up your application server and deploy the application to it. Finally, your web browser should pop open a new tab with the classic "Hello, world" page in it.
At this point, what do you actually have? You've got an empty web project with a single JSP file in it. You could customise it, but that's maybe not very exciting. What you're really looking at is a basic framework in which you can apply your learning of JSP and of servlets as you get to grips with them.
How to proceed with said knowledge transfer? I recommend a decent book or two. The one I used to get going was "Beginning JSP, JSF and Tomcat Web Development: From Novice to Professional" (Zambon, Guilio; Apress; ISBN 1-59059-904-7), which has a decent beginner's guide to how JSP and servlets work together, and a handy reference guide for the former.
As soon as possible, you're going to want to migrate away from raw servlets and JSP to tying them together in a slightly more flexible way using one of the frameworks I skipped over earlier. I'm not going to tell you which one to learn; there are several pretty decent ones. Try Spring MVC, or Struts. Once again, I'd suggest getting a decent book.
I would start by taking a look at servlets and JSP. I found this book helpful when I read it: Head First Servlets and JSP
Netbeans comes with many sample projects, create a few and browse the source code.
A already working sample is always a good starting-point for your coding.
If you're also interested in using Eclipse, which makes it very simple to create and deploy web applications here's a nice tutorial - WTP Tutorials
Hi to start code and setting up web server with java follow the following procedure.
First you need to install Apache-Tomcat or Jetty any web container or servlet container.
And you have set the classpath of servlet-api.jar file.
Next you will have to save your web-page code stuff in web-apps folder of tomcat.In web-apps folder there you have to create a web-inf folder in which web.xml file is stored.And after writing servlet and jsp programs the compiled class files are stored in classes folder of web-inf folder.JSP files are stored along with WEB-INF folder.
You have to keep all the jar files in LIB folder of classes folder.
The web container will take care of initalizing servlet, loading class using inti method.Using service method it will create two objects request and response.
The Java EE stack is quite a mouthful. I suggest you just look at writing a Web Application (WAR), and deploy it to Tomcat.
Unfortunately doing a full WAR-file deployment is rather tedious so you generally want some help from your IDE or the web container.
The easiest place to start is probably installing and starting Tomcat and then fiddle with the files in the file system. There is an example application.

Categories

Resources