java ee - how to put generated html response into json object - json

i'm developping a program in java ee.
i know how to display a view from a servlet with a code like this
this.getServletContext().getRequestDispatcher( VUE).forward( request, response );
where my view (VUE) is in a jsp page.
i know also how to return a json object with something like that
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonErreurEtRole);
I'm using google Gson to convert java objects.
Now i want to return a json object that contain a view in one field and some others messages in others field.
But i can't find how to do it.
Is there a way for that ?
EDIT
I will try to be more clear,
My problem is that : users ara requesting content by calling a servlet (a controller), i check their right on that content. And if they don't have right i send a message like "you don't have right ..."
that message should not replace any content in the html page but just appear as a notification.
So i wanted to return a json object like [message, content]
by the way i solved my problem by sending only message in a json object if user don't have right and html content if he has. And in je jquery code i'm testing the return type of the response.
I used this post :
jQuery ajax returned data: json and html mix?

Related

Why is MVC JsonResult controller method returning HTML markup in browser

So I have a controller called 'HistoryController' which has a 'JsonResult' action which returns json data.
There is no associated View or Model with this method.
When I enter the url .../HistoryController/GetData, I get strange behaviour.
Instead of getting the json data (or maybe even an error message), I get the entire master page markup back in the browser. Why is this?
All I want is for some json data to show in browser but what I get is this:

How to read JSON content (in Servlet Filter) sent via GET method?

In follow-up to this question , my GET request is returning a 'application/json' content type.
I want this JSON content as a String, so that i can use the Jackson Library to validate if this is a properly constructed JSON or not. Is there any other way i can read this content apart from request.getParameter() ?
Note: For a POST request, i used request.getReader() to get the JSON content. But this doesn't work for a GET request.

Tell modelbinding that MVC action parameter is JSON

I am using an upload control to send a file to a JsonResult, but I am also sending up a JSON string as a second parameter. This is all getting posted with the Content-Type:multipart/form-data;
[HttpPost]
public JsonResult UploadDocument(HttpPostedFileBase file, DocumentViewModel model)
{ ... }
I know MVC is capable of binding directly to a viewmodel if the content type is set to application/json but I don't think it's possible for me to set that in this case.
Is there any way for me to get MVC to automatically bind my posted json string to model?
That's not possible out-of-the-box. You will have to manually deserialize the JSON string parameter that you would read from the request to your view model inside the controller action or write a custom model binder for it that will do the job. Ideally you shouldn't be posting the model data as a JSON string but rather respect the content type you specified : multipart/form-data. So the correct way to handle this scenario is to modify the client code that is sending the request in order to respect the content type.
As I was unable to change the content-type I found this blog to be exactly what i needed.
"... our whole request stream(data) won’t be json string. Only the guest parameter will be supplied as json string..."
http://ishwor.cyberbudsonline.com/2012/07/fun-with-aspnet-mvc-3-custom-json-model-binder.html

Pass Json Object from Play! framework to HighCharts JS

http://www.playframework.com/documentation/2.1.x/JavaTodoList
Using the above tutorial as a reference, I have created an application which sends data from the model to view via the Application controller.
I have managed to display the model(Tasks) as a high chart. The code is here.
public static Result format(){
return ok(views.html.frmt.render("Visualize it",Task.all()));
}
This goes to this view page.
http://ideone.com/ycz9ko
Currently, I use scala templating inside the javascript code itself. Refer to lines 9-14 and lines 20-24.This unelegant style of doing things is not really optimal.
I want to be able to accomplish the above using Json instead.
public static Result jsonIt(){
List<Task> tasks = Task.all();
return ok(Json.toJson(tasks));
}
My Qns are how to send the JSON objects to a view template.
And how to parse it into a Highcharts format. Is there some standard procedure to do this ? Or else I have to write my own method to do this ?
It'll great if someone can show me a code snippet. Also I would prefer a post not using Ajax. I would just want to know how to do this first.
I also found this stackoverflow post useful.how to parse json into highcharts. However, it didnt answer the part about converting from Play format to Highcharts format.
Thanks in advance
You don't need to pass a json object to your template, instead you might do an ajax call from your client side javascript (your template) and get json response that you could use futher in javascript code to build a chart. For example :
You have some path that is bind to your controller jsonIt() like so /chartsdata/json
then using jquery shorthand for ajax request:
var chart_data = $.get('/chartsdata/json', function(data) {
return data;
});
now you can use a chart_data that is an array of objects where each object represents a Task, in your further javascript code to build a chart.

Java servlets and JSON parsing

I currently have a servlet returning a JSON string on a POST to the output stream of the response.
This is my code:
...
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(jsonString);
out.flush();
I'm not sure how to handle this on the client side as it just displays the string on the page. What needs to be done?
A servlet returning a JSON is not meant to be called directly by the browser. It's meant to be called with JavaScript or another artifact that can interpret JSON.
Usually you will have something like:
var myObject = JSON.parse(myJSONtext, reviver);
That will get you an object parsed from JSON contents you send from servlet.
To get myJSONtext you usually do an AJAX call within a piece of Java Script code.
Google for: json ajax example
You will get a lot of information online.