Im am planning on creating a desktop application with JavaFX. I would like to use JavaFX only for the UI, and all backend work should be done by java(with spring).Im not sure how to develop the frontend to call the service layer, and also receive the response and display the response on the next page.
Basically I need to know the following(just an example of want i want to accomplish).
1) to call the controller class I would do something like below in 'Login.fx'?
function btnLoginAction(): Void {
var loginController = LoginController {};
loginController.authenticateUser(txtboxUsername.text.trim(), pwdboxPassword.text.trim());
}
2) LoginController would be my java class and would call the service layer. after authentication, i would want to call another page('Welcome.fx') and pass the firstname of the logged-in user as a parameter. How do I accomplish this?
Im new to javafx(java gui development as a whole), hence let me know if there is a better approach to go about this, and some links to help me better understand.
NOTE: I am using javafx 1.3.
You can use java classes from javafx, e.g. the way you suggested is correct.
To achieve that you can just add a parameter to Welcome class and set it during creation/accessing with value received from LoginController instance.
P.S.: but I really suggest you to move to JavaFX 2.0. JavaFX 1.3 script language is not supported unfortunately, so no new functionality would be accessible.
Related
I need some advice about planning the application's architecture. I want to upgrade my console version of Chat Application to JavaFX. So I started planning. Now I have one main class to manage client and one to manage server. And the problem appears at first glance. I want to make login/register window where "I" will connect to the server and validate data from input fields such as login etc. And when everything is correct I want to pass my already created connection (socket, streams, etc.) to the main chat window.
How do I do that in the CORRECT way?
I mean how to manage one class Client through multiple windows?
When/Where instantiate the Client object?
Because you said :
I need some advice about planning the application's architecture
There are many patterns for JavaFx applications such as abstract MVC(Model-View-Controller),MVVM(Model-View-View-Model),MVC with Service layer,MVP(Model-View-Presenter) and the choice of pattern or the architecture depends on your project and what do you want achieve.
Based on #James_D comment ,it is better to use MVC model with Service layer (if you need Service Layer to manage your connection) .I made this schema for you to describe aproxiamtly the benefits of this pattern:
Now for client login you can validate your data in Login controller by checking client inputs and make your test (true or false) (it must having a stored data ) to manage many clients because if you use static test if(userNameField.getText()=="Ala Eddine") in this state you will handle many clients with same shared data after that you can show Dialog if iputs are false ..etc.
In your ServerController when you launch your server create a loop that recieve each time a new connection and in this state you have two choice :
Handle client in ServerContoller by creating Inner classes
Add ServiceImp Class to handle clients
For MVC model ,you should respect relationships OneToOne and OneToMany... etc between your models.Your model depends on real life,for example:
Father has many children
Child has one father
In your example
Server has many Clients.
Client has many Messages
Message has content,attachments,date as properties..
Now you can see aproxiamtly the tree of packages :
In the end you can see the communication between controllers :
I did it this way:
//loading main chat window fxml
FXMLLoader loader = new FXMLLoader(ClientMain.class.getResource("your_path.fxml"));
Parent mainSceneFXML = loader.load();
//getting controller object
MainController ctrl = (MainController)(loader.getController());
Now you can access needed attributes or methods from ctrl. Replace MainController with classname of your controller of main chat window.
There is this typical traditional JAVA Spring + JSP application that I am working on. It's a full fledged working application with more than 50 pages. The client feels its slower and wants to make it faster by using ReactJs for new pages. From performance point I understand his concerns. Now I am not a JAVA expert and I am new to ReactJS but i have worked on AngularJs(SPA) applications extensively before.
Right now the way the application works is when we call a url say http://example.com/mycontroller/myaction.do, the app maps the url to certain controller and action in a JAVA controller.
#RequestMapping(value = "/mycontroller/myaction.do", method = RequestMethod.GET)
public ModelAndView myfunction(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelAndView mav = new ModelAndView("myJSPPage");
mav.addObject("pageDetails", myPageDetails);
return mav;
}
Once the action gets executed the html page is rendered in the browser along with server data and jQuery takes care of the UI part.
Now speaking of ReactJs,
React is just a UI, Lots of people use React as the V in MVC.
Which comes to my questions:
Can i use React in Java JSP Pages and access Java variables in React ?
If not what are other options/ways to use React with these kind of applications.
If its not possible to use React in current application, do i need to write the whole application from scratch using React. What are the challenges i might face?
Yes. Its possible to pass the Java objects and lists to your javascript application using Nashorn which comes bundled with java 8.
Second option is to do the rendering on client and fetch the required data using ajax/websockets.
I believe sticking to using JSP’s along with React won’t benefit you much in terms of performance. Because the main benefit of using SPA’s is that you don’t need to re-render the whole web page when some data changes or when actions are dispatched.
If performance is your main concern then I encourage you to implement a React application (the V) and make Spring your controller (the C) and make them communicate JSON objects (the M (data)).
When developing a Web Application in Java, launching in Tomcat, I need to be able to create (dynamically) a new static address (link,URL) in the server that will be used to view the information of a new item, let's call it new_item_001, which have been just created by one user.
Say I want to create a new address
www.domain.com/webapp/items/new_item_001
which can be used to render a view of the contents of new_item_001.
Which is the best approach to do this?
Should I dynamically create a new servlet class for this view?
Should I dynamically create the folder items and one html file new_item_001 for this item inside of it?
Should I edit the server address mapping rules to create this static address and map it to a central servlet which somehow knows which item to display?
I understand the question is ill posed, and that I am far from even understanding the issue, so I would like some guidelines on what to look for.
None of the above.
You should simply have a servlet mapped to /items/*. When a request come to this servlet, analyze the actual path of the request, extract the part after /items/ to know the actual value (new_item_001) in your example, get the data corresponding to this item from the database, and send it to the browser.
Using a true MVC framework like Spring MVC would make that much easier. You could simply map a method of a controller using
#RequestMapping("/items/{itemId}")
public Item getItem(#PathVariable("itemId") String itemId) {
...
}
and let the framework do all the URL parsing for you.
I would like to tackle this in a simple way. Creating a servlet for each created item would be overkill and become quite cumbersome to manage after a successful run of the application for some time.
Changing/editing server mapping URL looks very naive approach and is not scaling too. Let configuration be there and change them only when you actually need to change them.
My suggestion is to create one servlet that handles all these requests. For example, you may save item information on a datastore or on file system(i.e images uploaded by user etc..). Next time a GET request is received by the application to fetch saved information of an item, servlet should be able to reference the item on database associated with the item id on the URL. If you don't wish to expose item id/surrogate key in the database, you can also have a simple mapping between them by implementing your own logic. Frameworks like Spring MVC do a good job in mapping URLs to resources like this should you wish to use a framework.
Additionally to minimize the number of requests to the same item, you can also implement an HTTP caching strategy(i.e. ETAG, If-Modified-Since) by instructing your web server at the time of first GET request from a user.
In my Android application I want to call some web-app methods.
I use hessian for this (hessdroid library for Android).
That's how it works:
In my Android app I have interfaces for all of the the server methods e.g. getUserById(), getMessageById(), getSomethingElse() etc.
With Hessian I can call these methods through http.
So I need to initalize Hessian Proxy for that:
HessianProxyFactory proxyFactory = new HessianProxyFactory();
UserAccountService api = (UserAccountService) proxyFactory.create(UserAccountService.class, "http://localhost:8080/server/UserAccountService", getClassLoader());
After that I am able to run server methods like that:
UserAccount user = api.getUserById(999);
The problem is that api calls like "api.someMethod();" require running from separeted thread. In any other case I have NetworkOnMainThread exception.
My question is about how to make these calls in right way?
Should I create AsyncTask for every server method call? Or it is possible to make that more generic? I really dont think that creating AsyncTask for every method is a right way. It makes code dirty.
Any ideas? I really appreciate any solution you can provide.
I am designing a REST API that I would like to be localizable in the future.
Therefore I am defining the URLs to be of the form
/en/<resource_url>
With the intention of being able to support
/fr/<resource_url>
In the future if need be.
However I only want to define each resource url service once. Therefore I figure I need to get the URL parsed and rewritten without the language piece of the URL before it is matched to services. Finally that language should be made available to the services somehow for them to localize if necessary.
How can I achieve this?
I am using Jersey 1.17 inside Jetty container embedded in a larger server process.
You can make the /en/ or the /fr/ part a variable. Then set your locale to the value of the variable. Here's an example:
#Path("/{locale}/username")
public class UserResource {
#GET
#Produces("text/xml")
public String getUser(#PathParam("locale") String locale) {
...
}
}
But that may not be the best way to go about it. After answering, I found this other SO question that is a better way to solve the problem: Getting the client locale in a jersey request With this way, you don't need to add this to the URL. Just make the client set a header.