I am totally new to Struts2 framework (and Java web services), and just went through few tutorials.
I have a general question here, in most tutorials, web contents like pages and back-end logic are stored in one single war package.
If I would need to make some quick changes to the pages (small ones, like updating text or picture), seems like I would need to deploy the war package again (according to the hello world tutorials).
In real life web applications, is there a better way to apply quick web page changes?
Thanks in advance.
It really depends.
There are companies using a Continuous Delivery approach, that allows them to release in production in minutes. For this lucky guys, it's not a problem.
Other companies are uglily drowned in burocracy, and every release means time, money and documentation, and hence this should be handled carefully.
Since JSPs (and Facelets) are not simple views but contain logic (not serverside logic like in the dark scriptlets days, but presentation logic), they should be part of a release process. Then you should keep them in the usual WAR, and extract from it only the pure presentation files, referencing them externally: .css, .js, images, HTML files and so on.
This way, if you will need to change the company logo, or the background color, you won't need to release anything except the static resources, if instead you'll need to add a new functionality, this will imply a release process.
This discussion can change with recent front-end frameworks (AngularJS, React, etc...) because HTML and .js there are part of the business logic. In those case, I'd take out just .css and images, keeping the rest under release control.
Related
I am starting a new job this fall, and I need to learn a bit about Maven, Spring and Angular before I start. I am fresh out of studies, and I have never used or heard about these tools before. I have read a bit about all three, but feel none the wiser.
Could someone give an easy explanation on what these three are used for, and what the connection between them are? I guess that there must be one since I have been asked to learn all three. Also, from what I could understand Angular use HTML and CSS, is this something I should learn before using Angular?
Spring is an enterprise JVM framework, which offers things like dependency injection, aspect-oriented programming, and offers a premier MVC model. It is primarily written in Java, with a notable exception being the JSP files - those are written typically with JSTL.
Apache Maven is a dependency management and build management tool, which has the capabilities of running a project's test suite in addition to building and producing JARs or deployable WARs for releases.
Angular is a JavaScript framework which can be used to control what is rendered on the screen, and also affect presentation.
There's a lot about these to take in, and I wouldn't expect a weekend to do it for you. However, the general approach to these projects is to think about them in layers.
Given that concept...
Spring is the backend layer; it is handling much of the heavy lift with regard to the business logic and processing of data
Angular is the frontend layer; it handles interaction with the user
Maven is divorced from both layers yet can play a role in both (depending on how it's set up); it governs how the project is built and what resultant artifacts are made to be published to production
AngularJS is a JavaScript framework, the aim is to work with HTML pages and CSS. Pure HTML pages produces static web pages, the CSS is used to bring some style (colors, positioning...) to the web page with that pure HTML code, then AngularJS puts some life to the page, turning that static page in a dynamic web page, that makes possible change the content of the page dynamically etc.
So if you want to learn about AngularJS you'll need to learn at least HTML.
Spring is a Java Framework, which has the role of backend to web applications. He gets the data from the frontend (AngularJS + HTML and CSS) and does something nice with that, most commonly he processes the data and puts on a database, for instance.
Maven brings a project's structure to life (thanks to Broser's comment). He manages the dependencies of all the things you want to use on your project, useful for the build process too. Maven does a lot of things related to dependencies and packaging. So Maven can help you on the dependencies of Spring Framework, for instance.
Further information about this technologies, you can find in the official page of each of them.
Helpful links:
HTML: w3schools.com/html/default.asp
CSS: w3schools.com/css/default.asp and
w3schools.com/bootstrap/default.asp (nice to learn)
JavaScript: www.w3schools.com/js/default.asp
AngularJS: docs.angularjs.org/misc/started
Maven:
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Spring:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview-getting-started-with-spring.html
From JavaScript contents I recommend you to search for at least one good book for each one.
I have a Java web app. Over time it has evolved in to what should really be two apps. There are things like CSS and Javacript files though that both ill need. Is there a way to have them shared so I do not need to duplicate these files.
Here is some additonal info:
Java 1.7
Using JSF running in Glassfish 4
If you refactor to 2 web projects say Web1 & Web2, make one of them base Web say Web1 and have other Web2 reference Web1, so you dont have to duplicate and keep them separate as well.
But unless you have a strong reason to split out 2 web projects, its advisable to keep it one if you have shared resources since a lot of shared resources like DB connection objects, services could be commonly pooled and will require relatively lesser resources
Perhaps the best option is to have a third web application, even in another domain (e.g. the Google Hosted Libraries), containing static resources such as CSS, JS, images, etc.
This is very convenient when the site is a huge sets of pages and different sites in the organization. Thus, if you need change to another style sheet or image, just change in one place.
I need to build a dashboard for an application, the dashboard will have different dashlets and each dashlet can have any one of the following things:
Graphs (JFreeCharts and some Javascript Chart)
Table data from tables
Data from external sources
Maps
What can be a good architecture for such kind of application?
What I have currently in mind is:
Each dashlet should have its own lifecycle and when the dashboard loads it should just show the UI of the dashlets initially.
After the page load each dashlet sends a server call (based on its type) to fetch its data
After the data has been fetched, each dashlet (based on its type) renders the data.
First of all, there are plenty of front-end frameworks to get you started. Some of the more popular ones include:
Backbone
Javscript MVC
Sproutcore
A bit of Google searching can yeild pros and cons of each and I would weight your options accordingly.
That all being said, the basic problem you posed actually seems similar to ours. In the end, we built something a bit different in house. Many of the frameworks out there are optimized to display a singular canonical "view" based on a Model reflected by the DB and a Controller to manage small changes. A dashboard has, in essence, a variety of different modules that must be doing their own independent things as you've mentioned in your question. Because of the high number of independent modules, I feel like you might feel pains in some of the frameworks listed above.
I can't tell you exactly how to implement such a module architecture, but here are some rules of thumb we used when designing ours:
Module Design:
Module-based. (Login module, Map module, each Dashlet may be a module, etc.)
Modules must have one Model, may have no more than one Collection (which is-a Model), and may have one or more Views.
A module may be used in multiple places and pages. The singular Model should stay the same, but the Views are likely different.
Rendering:
Almost all HTML on the page is written and updated by javascript modules. The template files are almost empty except for headers and basic scaffolding.
All modules render their full HTML selves and replace themselves into the DOM. The module should have as complete of a static HTML representation ready to go before inserting into the DOM. This means the render functions use “.replaceWith()” instead of “.append()”.
If simple HTML replacing isn’t an option (i.e. needs to be animated) a transition function should be defined detailing how to go from one rendered state to another.
Because rendering is expensive, Views by default do not auto-refresh on all Model changes. Re-rending happens through events only. _render() is in-fact an internal method.
Orthogonality:
A single inter-module event dispatcher on the page Controller handles all cross-effects between modules.
Modules should never “reach outside” of their own DOM context. If an event in one module affects another, it should go through the page controller dispatcher.
Each module as orthogonal as possible. They depend on each other as little as possible.
Each module in its own file.
Connecting to backend:
All modules use the same global backend adapter. Modules never talk to the backend by themselves. This makes your front-end back-end agnostic.
Recursive:
Modules are commonly included in other modules.
Modules re-render recursively.
Testable:
Because modules render static HTML, they can be predictably tested.
Modules must be testable.
Standard input -> Module -> Predictable static HTML output.
Standard events -> Module -> Predictable static HTML output.
If anyone knows of other frameworks along these lines, please share!
Our web app is based exactly on this architecture and in production since end of last year. You can see it at http://beebole.com
We just optimized the calls to our own server.
There is a single call to get the common data needed by most widgets, each time a screen is loaded.
Then if a widget needs additional data, it makes a call itself to our server.
The external widgets call their own data too, but to another server.
I would advise against using a custom web framework when there are so many free ones available.
As mentioned in another answer, the traditional MVC style frameworks don't really fit well to your 'dashboard' desired style of UI. They are best used for creating static web sites based on data retrieved elsewhere. They don't handle user interaction well and you usually have to hand roll your own AJAX to do anything useful without a page request.
A better breed of web frameworks to look at are the Web 2.0 fraemworks, also known as the frameworks which help you build web applications. It is important to understand the difference between web site and web applications. They are usually differentiated by the latter being interactive and the former being mostly static. Websites which also have some interactive components are still web sites. A good way to think of it is ask yourself "Does this feel like a desktop app?".
For web application development in the Java (JVM) realm, I would use Vaadin. It lets you write Java code similar to Swing programming, with event based methods. You can even avoid writting HTML altogether if you'd like by defining your views programatically. This lets you unittest your view logic (in web apps, there is more than usual) which is not posible with regular HTML template based frameworks. The other main advantage is that it has built in methods which allow you to write Java code to handle dynamic, asynchronous functionality and it all gets translated to JavaScript automatically. No need to write 4 different languages while writing your web app, just write Java for everything! Try it out, it is fun to work with!
Another web app framework that is getting alot of attention is Lift. I do not have experience with it but many devs I have spoken with have promoted it to me. I believe it uses HTML templates with Java code as the back-end. It is also apparently really easy to get started and your web app spun up. It also has built in support for doing AJAX like functionality. Worth looking into at least.
There are probably many more web app frameworks out there that would suit your needs. These all have the advantage of being tested, independently maintained, updated, and secure*. If you roll your own framework for this project, you need to worry about everything yourself. Written a web framework that doesn't offer anything new would be like written yet another programming language that isn't innovative; it is just a waste of time.
I think what you are looking for is more along the lines of managing or controlling your dashboard. I am designing something similar. I would suggest you look at google app engine it can be used to automate and control this: https://developers.google.com/appengine/docs/whatisgoogleappengine
Also look at these open-source dashboards: https://github.com/twilio/stashboard
I'm going to write my first Java based web app, and I'm sort of lost how to begin.
Firstly, I would like a web app and a desktop app that do pretty much the same thing, without the hackish idea of embedding a web browser into the desktop app because that doesn't allow to easily make changes to the desktop without affecting the web app and vice versa.
Now, here my questions.
Right now, I have a bunch of POJOs and they communicate with a single class that, right now, uses a flat file as a "database", of course, in production, I would use a legitimate database and just change that single class. Is this a good idea? Will I be able to go from POJOs to a web app?
Should I use a framework? I would like to have this app written pretty soon, seeing that all the buisness logic is there, I just need to wrap it so its usable, so, I don't want to spend an extreme amount of time learning, say, Spring (which AFAIK is huge), but, I don't want to keep reinventing the wheel throughout my app either. I can always just use JSP and scriptlets...
If you said yes to the above, what framework(s) do you suggest? Please note that I would like a framework that I can start using in maybe 3-4 weeks of learning.
Will I have to start from scratch with the POJOs that I have written? They're well over 30k LOC, so, if it is like that, I'll be hesitant.
You will need:
a web framework. Since you have Swing background, JSF 2 will be your best bet (everything will be painful, of course, but JSF will get you up and going quickly and will help you avoid the most tragic mistakes). Also, wrapping business pojos into web guis is the main use-case for JSF and it's biggest focus.
a "glue framework". One thing that is much different with web applications as opposed to desktop ones is that you cannot create view components by yourself - they must be created when browser requests a page. So you have to find a way to create the view objects and deliver all the references to the pojos that represent logic, some of which may have very different lifecycles (this is not a problem on desktop, but on web you have to distinguish between pojos that live along with the whole application, along with a single user session, along with a single request, and so on).
The "glue framework" could also provide the additional benefit of managing transactions. You have three choices:
Spring. It's not half as complex as you thing; you only need to learn some basic stuff.
EJB. You would need a real application server, like Glassfish or JBoss
bare JSF has good support for dependency injection, the only drawback is the lack of automatic transaction management.
If I were in your position, I would go with bare JSF 2.0 - this way you only need to learn one new technology. At first, try to avoid libraries like PrimeFaces - they usually work worse than advertised.
edit - and addendum
or - what is "dependency injection"(abridged and simplified)
When request comes to a web application, a new task starts in a new thread (well, the thread is probably recycled, but that's not important).
The application has already been running for some time and most of the objects you are going to need are already built and should not get created again: you have your database connection pool, maybe some parts of business layer; it is also possible that the request is just one of many request made during one session, and you already have a bunch of POJOs that the user is working on. The question is - how to get references to those objects?
You could arrange your application so that resources are available through some static fields. They may be singletons themselves, or they could be acquired through a singleton locator. This tends to work, but is out of fashion (hard to test, hard to refactor, hard to reuse, lifecycles are hard coded in application). The real code could look like this:
public void doSomething() {
Customer Service cs = AppManager.getInstance().getCustomerService();
System.out.println(cs.getVersion());
}
if you need clustering and session management, you could build a special kind of broker that would know and provide to anyone all kinds of needed objects. Each type of object would be registered as a factory under a different name. This also works and is implemented in Java as JNDI. The actual client code would look like this:
public void doSomething() throws Exception {
CustomerService cs = (CustomerService)new InitialContext().lookup("some_fancy_looking_name_in_reality_just_string");
System.out.println(cs.getVersion());
}
The last way is the nicest. Since your initial object is not created by you but by the server just after http request arrives (details depend on the technology you choose, but your entry point might be a JSF managed bean or some kind of action controller), you can just advertise which references you need and let the server take care of finding them for you. This is called "Dependency Injection". Your acts as if everything is taken care of before your code is ever launched. Spring or EJB container, or CDI, or JSF take care of the rest. The code would look like this (just an example):
#EJB
CustomerService cs;
public void doSomething() {
System.out.println(cs.getVersion());
}
Note:
when you use DI, it really uses one of the two former methods under the hood. The good thing is: you do not have to know which one and in some cases you can even switch them without altering your code;
the exact means of registering components for injection differs from framework to framework. It might be a piece of Java code (like in Guice), an XML file (classic Spring) or an annotation (classic EJB 3). Most of the mentioned technologies support different kinds of configuration.
You should definitely use a framework as otherwise sooner or later you'll end up writing your own.
If you use maven then simply typing mvn archetype:generate will give you a huge list of frameworks to choose from and it'll set up all of the scaffolding for you so you can just play with a few frameworks until you find the one that works for you.
Spring has good documentation and is surprisingly easy to get started with. Don't be put off by the pages of documentation! You could use JPA to store stuff in the database. You should (in theory) just be able to annotate your existing POJO's to denote primary keys and so on and it should just work. You can also use JSP's within Spring if that makes life easier.
... I a bunch of POJOs and they communicate with a single class that, right now, uses a flat file as a "database", of course, in production, I would use a legitimate database and just change that single class. Is this a good idea? Will I be able to go from POJOs to a web app?
qualified yes. if the pojo's are sane you should not have many problems. many people use hiberbate.
Should I use a framework? I would like to have this app written pretty soon, seeing that all the buisness logic is there, I just need to wrap it so its usable, so, I don't want to spend an extreme amount of time learning, say, Spring (which AFAIK is huge), but, I don't want to keep reinventing the wheel throughout my app either. I can always just use JSP and scriptlets...
probably. spring is huge, but things like grails or roo can help.
if you want to have a responsive web app, you will need to do some kind of rich client (AJAX). this may require a lot of your code to run on the client. this means writing a lot of javascript or using gwt. this will be a pain. it probably will not be so easy to just "wrap it". if you have written a swing app, then basically that code will need to run on the client.
If you said yes to the above, what framework(s) do you suggest? Please note that I would like a framework that I can start using in maybe 3-4 weeks of learning.
i like groovy and grails - grails uses spring-mvc, spring, hibernate. but there is roo, play and others.
Will I have to start from scratch with the POJOs that I have written? They're well over 30k LOC, so, if it is like that, I'll be hesitant.
the code that will run on the server can probably be mostly left alone. the code that has to run on the client needs to be rewritten in javascript or maybe you can get some reuse out of that code by using gwt,
The Play Framework is doing great things. I would recommend it highly. Having worked with EJB apps and Tomcat/Servlet/Spring apps it's a breath of fresh air. After framework installation you get a working app in a few seconds. Reminds me of Ruby on Rails or Node.js with the type-safety of Java.
Much quicker turnaround on getting started, faster development cycles, and a clearer configuration model than previous Java web app frameworks.
http://www.playframework.com/
We've got a legacy app to support. It's pure JSP, i.e. JSP opens connections, does business logic, submits forms (usually to the same JSP), and so on. It's 400+ pages, with some pages are as large as 100K.
The app is expected to be extended and modified over the next few years, so we're looking into ways to split presentation and business logic to simplify the maintenance. At the very minimum we'd like to port it into some simple MVC framework (Struts is #1 candidate).
Nobody is enthusiastic to refactor every page manually. We had an idea that may be somewhere there is a tool that does at least the partial refactoring, e.g. creates ActionForm based on request.getParameter() calls in JSP, moves all Java code into Action (though uncompilable), replaces some "<% if" with <c:if tags, and so on.
The remaining work is still very boring, but at least it has a much smaller scope.
Does anyone know such a tool?
I don't think it worths it. You're saying you have 400+ pages with some beyond 100k?
100k!!!
Probably the best approach is to take a good analysis on that webapp, and modularize it. You can have totally new modules written in other frameworks and still be used in conjunction.
For the pages that are 100K, they are good candidates of their own modules.
I don't really see any advantage of simply translate the whole JSP mess into some other framework mess. What will happen is that simply it will broke into pieces and nobody will feel like fixing them.
The good part is? What modules will go first? What other should not change?
I would start with those that have had more changes in the last months. The fact a file is 100K only means that new features have to be added, but the model was so badly designed, that instead of creating new objects, some code was simply copy/pasted and placed with an if ( I almost feel like I have seen your code already ) and the file grow and grow.
Some parts seems easy to migrate, but the source control says no one have touched in 2 yrs. leave them alone.
More than using a nice framework. you should migrate and re-write the most impacted parts of the system, and create test cases this time.
Also, you should create a project style and validate it automatically with something like checkstyle, so nobody commits new quick patches.
Eventually not all the application will be migrated, but the new changes will be easier to perform, and the application easier to maintain.