0

I started my adventure with Spring MVC framework. My current background is rather Adobe CQ5 applications. Most of the articles, tutorials and books that I read cover simple examples of views and then they switch to db integrations and so on.

I would like to create simple application which will be a frontend for my backend OSGI based application. So it is important that framework works on OSGI. (I managed to run Spring MVC on Apache Felix - Karaf)

So, I would like to have components shared across pages like:

  • autopopulated, dynamic menu
  • bar, which behaves differently depending on who is logged in
  • footer & header (also can be dynamic)

and a couple of page specific components. I know that application I mentioned isn't complex at all, but it's just an example.

In my opinion, each of these elements should have separate model, view and controller. These components should have access to some kind of session. And I don't see a way of doing it in Spring MVC.

Is Spring MVC good solution for what I want to accomplish? I reached to the point, where it seems that I made a bad decision. I feel a bit cheated, since there are lot of examples of Spring MVC doing web pages (like PetClinic) but it seems that doing more complex stuff would be a pain.

I have two additional questions. What is Spring MVC good for? Do you have any suggestions on what framework should I look for instead?

Michal Chudy
  • 1,883
  • 2
  • 21
  • 41
  • 1
    Your requirements seem to be more about front-end. Spring doesn't really help you with any of these questions. Also saying that Spring documentation is outdated is just non-sense. Even their own website has plenty of fresh tutorials that help with everything along with full code examples in Git. – Vaelyr Jul 30 '14 at 19:34
  • Well, I did not say that documentation is outdated, but that there is a plenty of articles on internet, which relates to Spring from couple of years before. (Which is a huge amount of time in IT) This mess things up since lot of approaches or practices are outdated, one have to be careful. – Michal Chudy Jul 30 '14 at 21:08

2 Answers2

2

Short answer: You don't just rely on Spring MVC to help you componentise and rule over the complexity.

By asking for a separate Model, View & Controller on a given "compisite view" you are essentially asking for a component based architecture.

Spring MVC is request based rather than component based and so your choice of framework is not not aligned. That does not mean you can't make Spring MVC modular. You have to use JSPs includes or JSTL. JSP tricks to make templating easier?.

You can access HttpSession by method injection. Simply add this as a parameters as below:-

public JsonResponse getSessionIncontroller
         (@RequestBody String jsonReqString ,HttpSession session) 
{

  session.setAttribute("success" , "successfully accessed");
  return jsonResponse;
 }

However I have given up this way of developing web applications long back. I advise you to lay down all the complexieties on a table and look for a framework to address all as to which helps you conquer your complexities gracefully. So allow me to introduce you to other alternatives. Go for one of the following:-

Single Page Apps

You can develop small applications very fast. Consider AngularJS, EmberJS, KnowkoutJS or Backbone.js. Whether your Menu changes dynamically or stays the same you have have the client side Javascript control this and send your Authorization JSON object once.

Component Based Arch.

Go JSF route via PrimeFaces or similar library. Server side rentering willl let you build compisite views wheree your header, footer is now just a fragment/part of your view and you have your Handlers. (I would not go this route as a matter of taste.)

GWT

Checkout GWT. They lets you build your views and Java objects and the code gets generated server side into Javascript etc. Consider this option option if you are extremely afraid of Javascript. (On a side extreme note if you are afraid of Javascript please don't build web applications.)

Good luck.

Community
  • 1
  • 1
bhantol
  • 9,368
  • 7
  • 44
  • 81
  • Thanks, It clarifies a lot. Regarding GWT and alike - I don't like these solutions. It feels like I'm having my hands tight up and custom development seems to be hard and the code is not clean. It feels like writing a desktop app. So the only way may be going Single Page App. One more constraint is that the framework should be easy to setup and use on OSGI container. I feel kind of cheated, since lots of examples on the internet indicates that you can create web page with Spring MVC, but when it gets complicated - it is rather useless. – Michal Chudy Jul 30 '14 at 21:24
  • 1
    I am with you with respect to GWT and also share the same sentiments about Spring MVC. I use Spring MVC for @RestController - the other alternative is using Jersey. But I am increasingly going Micro SOA with REST services serving JSON entities. FYI - SPAs don't even need any container - deploy stuff to your Web server. Use Yeoman/Lineman to generate the project use Grunt to do your regular house keeping. OSGI - Your server layer is OSGI compliant. Never thought aboout the client but something I read in the past perhaps http://modio.io/angularjs-using-the-http-whiteboard-service/ – bhantol Jul 30 '14 at 22:01
0

Most of the web application features you are interested in are outside the scope of vanilla Spring MVC, which is really more of a back-end framework. You can accomplish most of these features using additional Java technologies, however.

  1. For user authentication and sessions, look into Spring Security.
  2. For headers, footer, navigation bars and such, you should look into creating reusable tags with JSTL and Spring Expression Language.
  3. You can use great HTML/CSS frameworks like Twitter Bootstrap.

Hope this helps.

woemler
  • 7,089
  • 7
  • 48
  • 67