I have a spring boot app with Rest api and Oracle database and react front. It works well, but now I want to try datatables with server side processing. I have quite a lot of entities and controllers. I would like to clarify something. Every example I used to learn used jpa. Is it possible to use JDBC? (I have JDBC set up and I don't want to redo all in jpa). When I tried to change examples to jdbc they usually respond with null pointer exception. Also, how about the controller? I saw examples using both get and post. Usually I use get to get the data. But from what I tried to replicate and use with my database, post seems to be recognized, and Get throws the null. Do I need to specify the draw and other paramenters in body or where? Also, is there maybe only one controller which can somehow prepare the response from others or i should change every single controller? Sorry, it's my first time with datatables server-side.
This is what I use, is it enough?
componentDidMount() {
var index = null;
var table = $(this.refs.main).DataTable({
dom: '<"data-table-wrapper"t>',
data: this.props.data,
"processing": true,
"serverSide": true,
"pageLength": 1,
"ajax": $.fn.dataTable.pipeline( {
"url": "/api/calculations",
"type": "POS"
"data": function (data) {
return data = JSON.stringify(data);
}}),
'pagingType': 'simple',
'order': [[0,'asc']],
'pageLength': 100,
"columns": this.props.columns
});
$(this.refs.main).on('click', 'tr', () => {
var index = table.row(this).index();
var item = table.row(this).data();
this.updateIndex(index);
console.log(index);
console.log(item);
});
}
So, my frontend, after authenticating and authorizating, goes to that component, where I perform (there was pagination in api, i just decided to try without it now)
fetchItems = async (page, elements) => {
try {
const response = await CalculationsApiService.fetchAll()
this.setState({ items: response.data })
} catch (e) {
this.setState({
e,
isError: true,
items: null,
})
}
}
And later in render I Call my child component with my universal table and pass the columns and items as props (the first portion of code in this post) Maybe the error is that I call for data first in parent and then ajax in child? What about the controllers in general? Can I use normal rest controllers somehow and just send something special from my react? When not modified by the tutorial above i have a normal rest api with responseentity
Related
I'm working on a legacy app (struts 1, own framework, java 6, on WebSphere 7) where I need to change the behavior of an operation.
Let's call them (1) CopyItem, (2) AlterItem, (3) MarkAsCopied.
A button called one service which needs to be replaced with three other services.
Depending on the result of the first one, invoke the second one and so on.
I want to navigate in the end where the first one would take me (so it would look like the original behavior from user point of view).
Initially I thought I wrap every parameter I need into a form, POST it, and then on the Java side, I would call action execute for each service. Technically from CopyItemAction.execute() of CopyItem I would call AlterItem and MarkAsCopied executes as well.
I feel that this is pretty far from a clean solution.
Do you have a better idea, how to do this?
In the end I did it via synchronized Ajax. Which is definitely bad practice, so I do not recommend if you have the access on the backend code. However in my case now it turned out to be a working not-that-ugly solution. Based on Dave Newton's (https://stackoverflow.com/users/438992/dave-newton) proposal.
Keep that in mind the struts actions are still called according to the configuration defined in struts-config.xml.
I created the js function below and invoked the services with the specified forms one by one. This way a form (POST) submit calls the struts action, but doesn't navigate away. After they run successfully I redirected the page.
function submitForm(form) {
if (form !== null && form !== undefined) {
let formData = new FormData(form);
let xhr = new XMLHttpRequest();
xhr.open('POST', form.getAttribute('action'), false);
var successResponse = false;
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE) {
successResponse = xhr.status == 200;
}
}
xhr.send(formData);
return successResponse;
}
}
In conclusion: this is not a recommended solution, but if you're in such a situation like me, then it works.
I have a bunch of sensors scattered around.
These sensors transmit their status whenever they detect a change in their environment.
The data goes to a server(built using Java) where it processes that information then inserts it into a mongoDB.
My meteor App is essentially a dashboard for this information. I would like to do further processing on those entries as soon as they come in (analytics).
I started to use Collection-Hooks which works really nicely when the Meteor App makes changes to the database but not when the mongo Java-Driver does.
I need collection-hooks to detect new documents added to my mongoDB from the Java-driver. I'm also not married to collection-hooks, any other services suggested are welcomed.
What you want to use is an Observer on the cursor returned from a query:
https://docs.meteor.com/api/collections.html#Mongo-Cursor-observe
myCollection.find().observe({
added(document) {
// Do something with new document
},
changed(document) {
// Update analytics in response to change
},
removed(oldDocument) {
// Update analytics in response to change
}
});
This will depend on the contents of the actual database, unlike collection hooks that only operate when Meteor code is called
It's also worth noting that these hooks also track the specific query that was passed to find(). So if you only want to call these hooks for a specific subset of data, pass in the query like this this example from #scriptkid:
var date = moment().utc().format("YYYY-MM-DD HH:mm:ss.SSS");
log.find({ createdAt: { $gte: date } }).observe({
added(document) {
console.log("new document added!");
},
});
Good Evening, I try to do a checkbox which embedded in a JSP page. I use document.getElementByID("checkbox") in the JSP javascript function. How to pass the variables in the javascript function to another java file without passing it through url for security concern?
This is Checkbox Function:
var checkbox = document.getElementById("chbx");
function foo(){
if(checkbox.checked=true){
//passThisVariableToAnotherJavaFile-isChecked
}
else {
//passThisVariableToAnotherJavaFile-isNotChecked
}
};
This is Java File:
public class CheckBoxEvent{
if(isChecked) {
//then whatever
} else if (isNotChecked) {
//then no whatever
}
}
I am a newbie is this jsp stuff, I used to be doing this in PHP but everything mixed-up in my mind when there is a HashMap appear in the java file. Well, need some hints and help.
Thank You
How to pass the variables in the javascript function to another java file without passing it through url for security concern?
You have two options here and in both the cases you need to send the value to the server for processing :
An AJAX POST or GET request. This looks more appropriate to your requirement. You can get an example here.
Submit the form using POST.
Read here When do you use POST and when do you use GET?
In both the cases , there will be a Servlet/JSP/Controller handling the request in the server. You can call the methods in your Custom Java class with the request parameters.
when you do var checkbox = document.getElementById("chbx"); you are doing it at client side. Now if you want to propagate this value to server side i.e to some java class(most of the cases it will be servlets) then you have two options:-
1)Make an AJAX call. (You can also use jquery here to make AJAX call).Here is
the small example, you can find ample on google
$.ajax({
type: 'GET',
dataType: 'json',
url: servlerURL,
success: function(reply) {
},
error: function (xhr, textStatus, errorThrown) {
}
});
or
2)Submit the form
I am creating a one to many form in one transaction. I really need the record to be created entirely, or not at all. I have implemented a back code using java to rollback all operation should one of the data does not meet the requirement. Interface is using extjs, and I have REST interface with Jackson.
The problem is, how is the best way for ExtJS 4 to send everything in a particular form along with all detail records to a URL? Despite anything I have done, Ext.Store seems to send the data one by one.
Well, in short, I need the Ext.Store to POST something like this as raw with application/json content:
{
id: '',
party: 3,
machine: 'x1',
product: 'pr001',
runtime: 12,
materials: [{
item: 'rm001',
qty: '39.01',
align: '9.930'
}, {
item: 'rm002',
qty: '20.03',
align: '9.0234'
}]
}
The problem is, the child store always send the data as it is entered, and when I set autoSync to false, it still sends them one by one not everything at once through parent store.
Any example code?
Thank you
the way that I do it is through xml but the Idea should be the same
I have the parent store contain a serialize function
and it looks something like this
serialize: function(){
var store = this;
var xml = [];
store.each(function(record){
xml.push('id:'+record.get(id))
xml.push('party:'+record.get(party))
record.materials().each(function(materials_rec){
xml.push('item:'+materials_rec.get(item));
...
});
});
return xml.join('');
}
so I call this function from the controller and I submit an ajax request that will contain all the records of the parent and child stores
I'm used to do ROR, but I need to make a RESTfull WebService in a Java environnement. So I decided to try it with Play! since it really look like a great idea.
So I'm trying to find a way to add JSON to my already existing firstapp done following those instruction : http://www.playframework.org/documentation/2.0.3/JavaTodoList
What I want is something working similarly to ROR. At least, I want to be able to ask for JSON support by :
calling a .json URL
using "Accept: application/json" Header
So I tried some dirty thing like that :
JsonNode json = request().body().asJson();
if(json != null) {
return ok(Json.toJson(Task.all()));
}
return ok(
views.html.index.render(Task.all(), taskForm)
);
And it's obviously not working right now...
I need to detect wich type the client is requiring. I saw some people were adding dirty routes like that :
POST /bars BarController.index()
GET /bars.json BarController.indexJSON()
But it will clearly not support client using header to specify json request...
So, what I need is some kind of way to find out if there is any Header specifing content-type or Accept application/json. If it is so, BarController.index() would return BarController.indexJSON()...
All in all, it would be pretty much similar to ROR wich do :
respond_to do |format|
format.html # index.html.erb
format.json { render json: #bars }
end
All in all :
Does anyone have gone through the same reasoning than me and had reach an end ?
So I resolved my problem by using to function in controller, like this :
public static Result index()
public static Result indexJSON()
Then I added routes :
GET /tasks controllers.TaskController.index()
GET /tasks/json controllers.TaskController.indexJSON()
I would have preferred task.json but it wouldn't have allowed /tasks/:id.json ...
And for Header support, you need to check in your classic function if there is no header :
public static Result index() {
if (request().getHeader(play.mvc.Http.HeaderNames.ACCEPT).equalsIgnoreCase("application/json") || request().getHeader(Http.HeaderNames.CONTENT_TYPE).equalsIgnoreCase("application/json")) {
return indexJSON();
}
else {
return ok(
views.html.index.render(Task.all(), taskForm)
);
}
}
End that's all folks !
Does anybody have a better solution ? I don't like this one very much... Because I'm going to repeat many code ...