Spring MVC and Backbone.js Integration Issue - spring

I am trying to understand the integration of Spring MVC with backbone.js as its view technology. I find a couple of examples online such as here and here too However, in both of these samples, I am facing the same issues that the Spring MVC controller (the TodoController in the first link and TaskController in the second) does not seem to get called.
First, there are no log messages printed (even if I do a desperate System.out.println() in the controller class). Second, when I try to set break points in the controller classes and go into debug mode, it was not stopped at all, but the example is accessible through localhost:8080/todo (Assume that is the url of the deployed war file). In both examples, a code based configuration approach is used (there is no web.xml). So I am wondering, does the controller class ever got called?
I have been stumbling on this for the weekend. So I would really appreciate it if someone can point me the right direction. Thanks!!!

Thanks for your answer soulcheck :)
I went back to the old fashioned web.xml as opposed to the code-based configuration approach, and I am able to see that the controller class gets properly scanned and called, as printed by the log!! I do not know why though, but i just have new respect for web.xml :)

Related

Trying to create a "general" logging solution for requests in Spring Boot

I'm currently trying to create a info log for every request in the API I'm working on, but instead of creating a log for every request at the controller level, I wanted to try creating a "catch all" solution but I'm not being able to fullfill my goals.
I've decided to start the idea using Spring's #Aspect annotation, but using it isn't the best idea because I'd have to create one aspect for every controller that I have.
After some digging I found 2 possible solution paths that I could try to work on, #ControllerAdvice and creating an interceptor (which in theory is the one idea that I think has the most chances of succeeding and being a clean solution).
The problem is that I'm not being able to find much information regarding these ideas.
For the #ControllerAdvice idea I'm only finding it being used to handle exceptions, and for the interceptor idea I'm not finding anything similar to what I want to create.
Are these ideas feasible at all? Or should I just stick to logging everything at controller level?
Not sure if it's important but I'm currently working using LogBack library.
There is a ready solution for complete request and response logging for different client- and server-side technologies.
I'm using logbook tool for tracking request-response info for my rest endpoints.
https://github.com/zalando/logbook

Jersey (javax.ws.rs): how to do initialisation at page level ONLY not at application level

I need to do a lot of initialisation for few pages when they are called for only first time. Here is a decent post that addresses this: Initialize database on Jersey webapp startup
But the problem with the above approach is that the initialisation is done at application level which I don't want. I am not sure if and when a particular page will be called and so I want to go with initialisation only when the page is called for the first time.
Any thoughts on how to achieve this when using jersey. (Spring has #PostConstruct and #PreDestroy for this purpose at controller/page level... so some thing similar is what I need). A small example or code will help greatly.

Simple working Spring MVC / Maven configuration

I’ve been trying to set up a Spring MVC application from scratch, using Maven, in IntelliJ Idea. I know there are probably some nice Archetypes that can do this for you, but I really want to understand what’s going on. Here’s what I want to do:
Create a simple web application that shows displays “Hello World” under https://localhost:8080/, using an embedded Tomcat (with the Maven plugin). So, one controller, one request mapping, and one template. I’ve been able to get the spring application to boot (the Spring logo appears in the console) and Tomcat seems to run as well (the site responds). The problem is, I haven’t been able to get the request to map to my RequestMap method in my Controller. I’m quite sure that the controller isn’t the problem, but that it’s some configuration/setup issue.
Here’s the thing: I’ve been looking at tutorials, StackOverflow, Spring documentation, etc. for hours now, but I haven’t found a source that really explains how to configure a Spring MVC Maven project. Everyone seems to have a different opinion on what XML files you need, what they should be named, and where they should be located. The consensus seems to be that you need a web.xml file in a folder named WEB-INF, but even there, everyone has a different opinion on where that folder should go. I appreciate that there are multiple different ways to do this, and that there’s no “right” way, but in my experience, there are definitely many “wrong” ways =).
My question right now isn’t necessarily how to get my current project running (I don’t mind starting over), but what kinds of config files are out there (web, spring, app-config, servlet, …), what they do, which ones you need, where they need to go, what they should be named, how they connect, etc. Some people also use the Maven Compiler plugin, and others don’t, and nobody says why :D. Essentially: How do I let Spring know where to find controllers, templates, etc., and how to run and deploy all of this on the embedded Tomcat. I would like to find a source that explains the entire core-ecosystem of Spring MVC in a unified way. I have found many sources that provide “how-to” tutorials, but with little to no explanation (like “add the following servlet.xml file to your WEB-INF folder”). So, if your setup slightly differs from the tutorial (e.g. because you are using IntelliJ instead of Eclipse, Tomcat instead of Jetty, embedded Tomcat, a slightly different folder structure) nothing works, and, most importantly, you have no clue why.
Has anyone learned these setup-basics with anything better than copy/pasting or trial & error? :D
Easiest way to start is by using Spring Boot. Go to following link:
Spring initializr
Choose "Web" for "Selected Dependencise" and click "Generate Project". Download of maven project should start after that. Then import that project in IntelliJ IDEA as a Maven project. And that is a good starting point. You'll get main class that you can run and embeded Tomcat will start at 8080 port. Than you can add some controller, template etc...
Also good guides

Example for CDI-SessionScoped LogIn with TomEE

i'm currently fighting with TomEE, JSF and CDI (i think).
Is there any example out there which has "#javax.enterprise.context.SessionScoped" annotation using TomEE 1.5.1 (or current snapshot)?
I DONT want to use #ManagedBean or something else from javax.faces, just plain CDI.
My problem is: when i change some #SessionScoped user session instance i end up in having a new sessionID (session fixation problem?!?).
My Use-Case:
I want to have a login on one page and have the possability to login from anywhere, the user comes from a JPA-layer which scope i don't know exactly if have to choose.
Is there another way to archieve this?!
I already got this pages, but didn't helped me out:
http://openejb.979440.n4.nabble.com/Guest-user-td4655258i20.html
https://issues.apache.org/jira/browse/TOMEE-734
https://issues.apache.org/jira/browse/TOMEE-745
Thanks for any help!
After a lot of time i created my own "proof-of-concept"-example working with cdi-beans:
https://github.com/FibreFoX/cdi-sessionscoped-login
I hope it will help others, like it helped me while developing that piece of awesome :)
I Use TomEE but I don't understand your problem. Do you use the web-profile?
Did you setup beans.xml correctly?
The bugs you linked are unrelated probably your setup. Impossible to say since I don't have anything to go on. Link a sample project I can try or at least paste your bean and tell were you put beans.xml

what's the 'right' way to do a MVC for JSPs in Java EE 5?

I've inherited an incomplete but small web project (Java EE 5, running on WebSphere 7).
The project consists mostly of JSPs that are accessed directly via their URL, and most JSPs look up their own reference to the EJBs (services) they need. Also, there's a Servlet for every form that gets submitted by the HTML code in the JSPs.
Architecturally speaking, is there anything wrong with this?
I was thinking it would be better to have an MVC design. I don't want to convert everything to JSF because I don't want to convert all the HTML and embedded Java scriptlets into JSF tags and managed beans.
I don't really want to use Struts or Spring MVC because they're not part of the Java EE 5 toolkit that comes out of the box with WebSphere, and I don't want to add additional complexity with the additional libs and config files.
I was thinking about building my own little MVC with a "ControllerServlet" that accepts a command and dynamically build and execute the command object, and redirect to the JSP view.
But I ask myself again, is there anything "wrong" with JSPs that post to Servlets? It's actually kind of elegant in its simplicity.
What do you think?
Any suggestions are GREATLY appreciated! Rob
You're asking a rather subjective/localized question. But ala.
There's technically nothing wrong with individual JSPs that submit to individual servlets. The only real problem is when the servlets turn out to contain duplicated code for quite common tasks like collecting request parameters, converting/validating them, setting bean properties, invoking actions, performing navigation. That is not DRY and is what a MVC framework with a single front controller and a well definied lifecycle is supposed to solve.
Or, if the servlet's tasks are actually well refactored with homegrown code to perform those common tasks, then this is in turn not very maintainable as no one else than the original developer knows the ins and outs of this custom framework. So it's hard to find anyone else willing to maintain this webapp without learning another framework again which the new developer wouldn't likely to see in other future webapps. That is why companies usually adopt an existing and well-developed MVC framework like JSF, Spring MVC, Stripes, Struts, etc.

Resources