What is a "reverse controller" in Play! 2.0? I've read the documentation, but I'm still struggling to understand it.
I'm specifically trying to understand the difference between controllers.routes.MyController and controllers.routes.ref.MyController. When should I use each one? (I'm using Java.)
The normal controller handles a HTTP request as defined in the file routes. The reverse controller can prepare the URL that would trigger the request.
You should use the reverse controller computed URLs instead of hard coded urls in the href attributes of your links, in the src attributes of your java scripts and in your test cases. etc. Whenever you need a link from your application back to your application, consider using the reverse controller instead of hard coding the URL.
This is useful when you later change the route to your controller, you don't have to change all the URLs in the HTML files or in the tests.
Related
I am building my first spring boot project with java in intellij. When capturing form data from an HTML page, is there a 'best practice' way of transporting the data to the server controller as a JSON request body? I've seen solutions with javascript / ajax, but I prefer something more native to the spring library.
There are two ways.
Setting your Back-End as a REST API and completely separate your Front-End and communicating through REST calls. (as you said with JS/Ajax)
Setting your complete app under one roof. Spring and Thymeleaf are a good option. It works like this, every time you do a /GET request then spring will provide a template. Which is a Thymeleaf HTML document which has Thymeleaf specific syntax to make your life easier.
You can submit your data to your back-end through forms. When you submit the form then your app decides what happens next, will you be redirected somewhere else or will another template be opened...That's your call.
If you want a quick way to do it, go with the second way. Personally, I like the first one. It separates the concerns perfectly.
I have AngularJS application already in production and I need to make it Google-friendly. I have read about Ajax crawling mechanism and I have following two problems:
1. Since I have backend written in Java, I have tried HtmlUnit to make static snapshots, but it didn't work well with AngularJS. How can I serve snapshots of my AngularJS pages using Java?
2. As I have mentioned, application is already published and it uses simple hash without !. E.g.: /#/about, /#/home. Is it possible to keep this scheme? Change to /#!/ would require modifications of all links and would break all existing links (posted on web).
Thanks in advance!
The SEO is always an issue with single-page application.
I suggest you make a quick implementation of Phantom.js to display already rendered page to google bots. Check out this link for more informations.
Phantom.js will be a gateway to which you redirect every indexing bot request, it will then render your app like a normal user will do, and then send back the rendered page to the bot.
Also, it would be better to change your /#/ to only /, it's better for your users and also SEO. You just need to redirect every request to the index.html page and to use '/#/' as fallback for old browser which doesn't support pushState.
You also have some paid solution like https://prerender.io/ which works beautifuly.
I've a java based web application running on Tomcat and it uses spring framework. I need to expose a ping URL to check whether the application is up and running. I've considered the following implementation approaches and all of them seems to work well when I tried them. However, I could not make up my mind whether one approach is better than another. Does it matter which path I take? Could someone advise which approach is better and why?
Create a web page and modify web.xml to redirect the url to the jsp page.
Create a REST service using Spring-WS
Create a servlet and return response
Use anything you want :-) But notice, that both Spring-WS and JSP are a little heavier (really not a meaningful reason here) than servlets. If you already have REST API to your aplication, use Spring-WS, if you render pages through JSP, use JSP. Or if you use none of these, write a plain servlet.
Since you are using Spring, assuming you use Spring MVC, you can just add another controller mapped to a certain URL which would be responsible for returning a status.
Solution 1 might not work depending on your requirement since a jsp page might work even if the rest of the app does not since it is not part of spring config.
I've written the bare bones of my application using the MVC pattern. I don't currently have any AJAX functionality in my application but I was looking for suggestions on how I would change the architecture of my application to achieve this, to that end I'll try my best to describe my current architecture:
I have a controller servlet "controller.java" which reads the servlet path i.e. request.getServletPath() to determine the action required
I have a number of different Enterprise Java Beans (EJB 3.1) which handle the business logic and which are called by my controller servlet depending on the action requested
I have a number of views which relate to different aspects of my application to which the request is forwarded (by the controller servlet) based on the action requested (i.e. request.getRequestDispatcher(url).forward(request, response);)
I understand that the current architecture could support AJAX functionality (by matching a pattern from my "controller.java" servlet) but I'm getting to the point where I have a huge number of actions supported by my controller and it's getting messy.
Does anybody have any suggestions?
Is there a standard pattern for doing this? I'm trying to stay free of any frameworks just now as I'm a relative beginner! :-)
Thanks
If your controller supports a huge number of actions - it's where you need refactoring. In general your architecture looks correct, if the number of actions is reasonable (up to 10, I would say) per each controller.
One possible way of refactoring is to group controllers into modules.
You can check for ajax requests as follows:
boolean ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
and then handle the kind of response accordingly. I.e. returning the view ID which is to be used in a forward or redirect, or returning some JSON which is then to be written to response body, or returning a special View object which contains this kind of information. Given this basic MVC example, it should not be that hard to expand it with ajax support.
Same idea with BalusC.
We have an MVC app that runs by itself. Now to add AJAX functionality we added JQuery and used jqGrid in the presentation layer. It communicates with the backend via AJAX. If we remove the JQuery and jqGrid, we still have a fully running MVC app.
I've put a demo of that at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration. Here we integrated Spring MVC 3 and jqGrid/JQuery
I've coded some algorithms in Java and I need to include those algorithms in my web application.
I'm using Apache Tomcat and what I need to do is that when, for example, I click on the start button on my web page a Java class should be executed. I think this done by using servlets, am I right? If so, do you know where I can find some literature about it? I've searching in internet but it's a little bit confusing for me.
Yes, you're right. You'd want to write a servlet that handles request to an URI.
Here's some introduction:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html
Tomcat comes with some samples, you might look at the source code as a start, they should be in the webapps/sample directory.
The Tomcat documentation is also a good start.
http://tomcat.apache.org/tomcat-6.0-doc
I would check out this introduction to servlets
A servlet receives an HTTP request (e.g. a request for a page etc.). It will process that request via the methods doGet() or doPost(), and return an HTTP response. That response will contain your results (most likely an HTML page, although that's not mandatory).
I would (in order)
get a static 'hello world' page going
get a static page returning some data from your libraries
get a dynamic page returning some data from your libraries using data from the HTTP request
Integrating your library will be trivial, since a servlet is simply a Java class that can instantiate/call on any other Java class.
You will need to learn how to use Java in a web server. I would recommend using JSP just to start with as it allows you to create web pages that call Java code easily.
There are many JSP tutorials on the net - I believe this one is suitable: http://www.jsptut.com/