Angular 2 with existing Java application - java

I have an existing javaee application with multiple pages written in JSP.
My requirement is to create functionality on 3 independent pages with Angular 2.
So on each page , I will have a div that contains Angular2 component which performs CRUD operations on a specific tables. The rest of the page remains jsp.
the Angular functionality and pages are totally unrelated. For example one page is managing accounts, the other products, etc..
I am new to Angular2 so my questions are more about design and deployments.
1) Should I create 3 different Angular 2 apps for each functionality or one app with 3 components. If I do the latter, then what will my root component look like?
2) How to import the Angular2 app in the existing Javaee app? should I do compile.bundle and then copy the bundle?
As I mentioned, I am new to Angular2 so any guidance will be very helpful.

Angular is for "single page applications" and it's usually expensive to load it, but cheap to navigate when it's already loaded.
You can use Angular CLI for building and serving your apps. You will need the build - produced by "ng build" (Angular CLI command) to include in your war file and probably move the content of index.html to some of your JSPs.
Have you gone through the Angular tutorial (Tour of Heroes)? If not, give it a try, many things will get clearer for you.
Edit:
Maybe you could consider rewriting the existing JSPs in Angular, so it's all one application with routing. But if you really need to load it 3 times in JSPs, I would probably write just one application with four modules - three would represent the pages and would look like the AppModule. In main.ts, you would load a module according to the app configuration. The last module would be a code shared among them. This would make it easier to maintain than 3 separate apps.
Starting modules
In main.ts, you are starting the application by calling platformBrowserDynamic().bootstrapModule(AppModule). So you can import all modules and bootstrap just one of them. If you use Angular CLI, you can also import "environment" = configuration, so you can decide which module to load according to it.

Related

Create single web app (WAR) from multiple web applications

We already having an spring web application (maven project) say webApp1 which is hosted on production and working fine but now due some business requirements we want to develop an another web application say webApp2.
So some clients demands both apps, or some either one of them. we need freedom of which module to be deployed, there can be the case where some clients don't want webApp1 so deployment package must include only webApp2 or sometimes both.
Approach 1:
Lets create another maven project and develop individually, at the time of creating deployment package use of Apache Ant can be done which will create WAR file by combining libs,views,controllers of both the applications or of one. combining web.xml,root-context.xml,servlet-context.xml may be the manual task.
Approach 2:
After searching on approach 1 I come to know about EAR (similar question https://stackoverflow.com/a/2936464/1629242).
EAR package (similar question https://stackoverflow.com/a/2936464/1629242) can be the approach, but for this do I need to convert existing application as EAR? or what changes are required in existing working web application webApp1? also how I can control which module need to be added in EAR
As for approach 2. it really depends how exactly do you host your application, what is the container. If it's Tomcat/Jetty which can be a pretty common choice, than EAR is not even an option, since they don't "understand" (can't process) EAR files.
Regarding approach 1. The 'manual' part of combining various xmls can be tedious and certainly error prone. Moreover, if you combine the xmls you won't get a real separation (at the level of classloader) between webapp1 and webapp2. After all totally different applications will be hosted in the same WAR.
So, IMHO, you should go for approach 3:
Keep webapp1 and webapp2 as different deployment units (different WARs). Keep different web.xml, spring configuration files and so forth.
Deploy these wars as 2 different files in the same container. Container will happily serve 2 different Wars. This way, spring beans, servlets, filters and so forth won't interfere between two apps at runtime. Moreover this approach can be fully automated.

Did we get this full stack webapp architecture right?

I am working on a full-stack spring-java based web app. The app is current divided between two projects each of which produce a war file on build. One project (call it UI) serves as the front-end of the app. UI doesn't have much business logic in it. Mostly HTML templates, and JS/CSS and other resources. The other project (call it Server) carries a whole lot of weight in terms of providing auth, business logic, DB services, REST API for external world etc.
I am running into quite a few problems especially while making security work throughout the app given UI project has no direct way to authenticate a user or checking user roles etc. UI relies on invoking a REST OAuth2 password flow to Server to authenticate. So authentication itself works fine but I am having a tough time checking user access roles or any other fine grained permissions within any code in UI project.
Before I dig a whole lot deeper and try to make this all work, a couple of questions for the gurus here:
a) Is this design one of the acceptable ways to build web apps?
b) Are there any gotchas that I should be aware of if I attempt to bundle the two projects within one war?
I hope this is to the point but let me know if you need any more details, and I will be happy to add color.
Try another concept for separation:
Module 1 = core business logic -> produces jar file on build
Module 2 = web -> produces war file on build
Module 2 depends on module 1. All security stuff goes to module 2.
Hope it helps.

Sharing presentation logic between different Java Web projects

We have a couple of Java Web applications that serve different purposes (one for the public and one for internal administration) but work on the same database. We keep most of our business and database logic in a project a third project that is included in the two projects and this works well.
However, now we want to share presentation logic between the applications. (In other words we want to have the same pages controlled by the same action classes appearing in each application.)
What is the best way to accomplish this goal? Can JSPs be stored in in jars and used? Can two web apps refer to common jsps? Are there good ways for the two applications to share session data so users can go back and forth between applications?
Following can be done to achieve what you are planning:
Create a common EAR having multiple Web/EJB projects for your application. Thus all the WebApps can reside inside a common EAR file and can use common JARs kept at EAR level.
Just like a java project can depend upon another Java project, a Web Project can also depend upon other Web Projects. Hence create a common Web Project with JSPs and Utility Classes. Add this common Web Project to the Build Path of other Web Projects. They would inherit the JSPs and Java classes, and can have their custom JSPs as well.
The above would provide you the basic framework. There is no standard way for Web Apps to share data. If you want users to use common session you can use SSL or token based session maintenance. Session data from one App cannot be replicated to the other, but you can put some logic inside an EJB which can be called from both Apps there by linking them in a clean way.
The fact that you want to share presentation logic and session data betwen both application shows that these two apps should be merged in a single one, where administrators simply have more privileges than simple users.
We had similar problem. We wanted to share JSPs between several projects. JSPs can be stored in one base web project in maven war package. Then you can share this resources from base project (JSPs, CSS, Javasripts, etc.) as a maven dependency and you can override particular JSPs, CSS etc. in dependent project. It works very well for us and we don't need to e.g. repair one same JSP in many projects.
For detailed information, see: maven overlays

Design Web application

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!

java web templates across multiple WAR files

I have a multi WAR web application that was designed badly. There is a single WAR that is responsible for handling some authorization against a database and defines a standard web page using a jsp taglib. The main WAR basically checks the privileges of the user and than based on that, displays links to the context path of the other deployed WARS. Each of the other deployed WARs includes this custom tag lib.
I am working on redesigning this application, and one of the nice things that I want to retain is that we have other project teams that have developed these WAR modules that "plug into" our current system to take advantage of other things we have to offer.
I am not entirely sure how to handle the page templates though. I need a templating system that would be easy enough to use across multiple wars (I was thinking of jsp fragments??). I really only need to define a consistent header and main navigation section. Whatever else is displayed on the page is up to the individual web project.
Any suggestions?
I hope that this is clear, if not I can elaborate more.
Have done something similar in the past using Sitemesh
we defined a new web app called skins-app which only has the common header, footer, navbar which all other need.
Sitemesh is configured via a file named WEB-INF/decorators.xml in the skins-app
then in any consuming webapp, you add a WEB-INF/decorators.xml as well.
And point your pages to be 'decorated' by the skins from another app
<decorator name="main" page="/decorators/layout.jsp" webapp="skins-app">
<pattern>/*</pattern>
</decorator>
You can have detailed include/exclude as well in your consuming webapp, if any pages needed to be excluded from the 'decoration'. Take a look at the Visual Example on the Sitemesh link page.

Categories

Resources