Render raw HTML from Razor view - json

I have a ASP.NET MVC4 controller function that returns dynamic html using a StringBuilder as a Json result.
This is an quality issue, because I don't want dynamic HTML being returned as a string property in a JSON result.
My goal is to make use of ASP.NET MVC to use .cshtml for example with a viewmodel so that I make an instance of the viewmodel, passing in the results and in the end, return the raw HTML of the view as a JSON result.
How can this be achieved ?

Instead of returning an HTML string inside of a JSON object, you may want to consider an action that returns a PartialView directly.
If you can't do this, because you need to return MULTIPLE html strings in one call, then I would advise taking a look at this thread which has a nice static method to turn a rendered view into a String: Render a view as a string.

Related

HTML to org.w3c.dom.Document using Thymeleaf

I am currently using Thymeleaf to parse a HTML document and output it as a String which is then used to perform further operations.
I now need to pass Thymeleaf's output to a library which expects a org.w3c.dom.Document instead of a plain String, but I can't figure out how to do it without using another tool (I'd prefer not to do so).
Is there a way to make Thymeleaf output a org.w3c.dom.Document?

Using HTML Templates at server side

I am working on play framework with SCALA as backend.
Json data is given to the front end from the controller.
I want to add HTML as value of some fields of json.
This HTML will be kept as a template and data will be added in this template at run time.
I think i should put unique names in the HTML template and then these names will be replaced by the data which i want to add at run time. Ultimately, this HTML will be added in the json response.
Is my approach right? If not, what is the best approach to add data in an HTML template,add this template in json response and send this combined response to the front-end for further use?
Is it a good practice to use string replacement to add data in an HTML template?
I think as long as you use Play, you can put your HTML templates into app/views package. Let's say you call your template mytemplate.scala.html
You can parameterize this view as any Play view.
In the place in your code where you generate your JSON response you can then call mytemplate(parameters) to get html generated, Play will do all the work here for you. Then using play.api.libs.json.JSON object's methods and related facilities you can convert this html to JSON.
So in your controller's code you will have something like Ok(JSON.toJson(mytemplate(parameters)))
This is of course a sketch, so you will need to elaborate and try.

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.

Dynamic tables in jsp using Spring framework

I am working in Spring MVC. These is the scenario..
I select a value from dropdown box and click a button on jsp .
An event is triggered using javascript function.
Based on input,some database queries are executed , records are retrieved and
data is set into JSON Object in controller class.
How can I populate this JSON Object into the same jsp using table .?
Can I populate the same in a new tab.?
If you suggest a solution , then it would be highly helpful for me and my friends..
Thank you...
To work with JOSN object better option is used JQuery and send request to controller using
JQuery and get the JSON object and simple print in html code.
another option is generate html code in controller and send as a response and then simple print that response as a html souce of div.
are you getting me?

Rendering Views as String with Spring MVC and Apache Tiles

I am trying to reuse some of my tiles in a controller which is returning a json response to the client. I would like to return a json response similar to the following format:
{
'success': <true or false>,
'response': <the contents of an apache tile>
}
In my controller I would like to perform logic similar to this pseudocode:
boolean valid = validator.validate(modelObj)
String response = ""
if(valid){
response = successView.render() // im looking for a way to actually accomplish
// this, where the successView is the apache tiles view.
// I would also need to pass a model map to the view somehow.
}else{
response = errorView.render()
}
writeJsonResponse(httpResponse, /* a Map whose json representation looks like the one I described above. */)
I belive that you want to implement a view class that will wrap the output of a jsp in json. The class in question may be org.springframework.web.servlet.view.tiles2.TilesView.
Another option may be to extend the JSON converter. org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
If you need to render the view using Apache Tiles 2, you must use
org.springframework.web.servlet.view.tiles2.TilesViewResolver
See the example tutorial here: http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
If you need to render the response as JSON, you can use the #ResponseBody which requires Jackson in your classpath. See the example here http://krams915.blogspot.com/2011/01/spring-mvc-3-and-jquery-integration.html (The controller returns JSON). You can also see a similar example of the #ResponseBody at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html