I am having a utility Controller which need to have access to all the Models in the project. Please explain how should I 'use' all the Models inside a Controller.
Related
I would like to know which portion of code / logic should be put in Controller and Model ? I am using CakePHP. Are there some guidelines to split the code between Controller and Model ?
There are guidlines regarding Controller and Model separation.
You can find almost everything here.
Models
Your model should be responsible for managing almost everything regarding your data, its validity, and its interactions, as well as the evolution of the information workflow in your domain.
Controllers
Your controller should handle interpreting the request data, making sure the correct models are called, and the right response or view is rendered.
I am trying figure out best possible way to write a common logic in Spring MVC application. I have a Controller A which would be called which would invoke any another Controller based on request attributes which return json data.
I want the response back in controller A so that i can write common enrichment logic and send it back to browser. My intent is write enrichment logic only at one place and every request goes through it.
What your are looking for are so called HandlerInterceptor or ControllerAdvice.
I made a small repository on Github about Spring MVC Interception a while ago, it is a small Spring Boot application, which is showing you the ways to intercept a request with Spring MVC.
The question is always what is your main goal:
Do you want to just manipulate the output (e.g. adding a common field)? Use ControllerAdvice.
Do you want to call a logic before and after, without manipulating the request? Go for filters.
Do you want to change the object fundamentally (e.g. wrap it with another object)? Go for HandlerInterceptor.
May be you can create abstract controller class and put the common logic or code in it. So wherever you want to use this common code just extend this class and use it.
For example :
public class AbstractController{
// common logic
}
public class A extends AbstractController{}
public class B extends AbstractController{}
public class C extends AbstractController{}
I have an ADO Entity model in my ASP.NET MVC application. In the database, I have a table with data that I would like to output to ChartJS widget to display in my view. I would like to do this using AngularJS two way data binding. I would like to use a GET statement in my controller to pull the data I need using a JsonResult. I was told not to create a new variable in the view. I have set up my connectioin to database and is working. I have referenced the following: ChartJS, AngularJS, Dx, JQuery and a few more.
In Grails I have a model class User that has a one-to-one mapping with another model class Address. When returning a user in JSON format from my controller I never see the address class, just the id. In my User class I have:
class User {
Address address
...
static mapping = {
...
address fetch: 'join'
...
}
And then in my controller I do
User user = user.get(1)
render user as JSON
Is there a way to change the mapping to make the 'as JSON' pull back the address class?
I am running on Grails 1.3.7.
There are Two types of JSON Converters i.e:
grails.converters.deep.JSON
grails.converters.JSON
.
.
What you need is a Deep Converter,
Just change the Imported Class To:
// Dont Use: import grails.converters.JSON
import grails.converters.deep.JSON
Note: Grails 1.3.7 import grails.converters.deep.JSON is fiine, in Grails 2.0 its deprecated.
The Difference Between these two is, that the "Deep" one will also JSONify the nested Classes, Whereas Standard Converter will not.
Hope that help
Regards
Kushal
Have you tried setting your converter to be deep?
JSON.use("deep"){
User user = user.get(1)
render user as JSON
}
There is a bit of stuff around the Custom Object Marshallers that you can dig into here, but my first try would be to just try the deep conversion.
I'm new to grails. I'm trying to develop a simple page with just a dropdown that when the user makes a selection an AJAX call is made to the database. Thing is that there is are no domain files at all in my application. I only have controllers and views and I intend to keep it this way. So I basically want to use grails to issue a mySQL Select statement through AJAX and get the results.
You could do the below:
From the AJAX call (I prefer jQuery), make the call to the controller action method.
In the controller's method you can use Groovy SQL to execute your query.
Return the result as JSON object and display it whatever way you want it to.
See http://groovy.codehaus.org/Tutorial+6+-+Groovy+SQL for a tutorial on executing SQL using the built-in Groovy SQL library. You can then create a controller action and map your resultset to JSON, and you won't even need a GSP view file. Here's an example Controller class that will do basically what you need.
import groovy.sql.Sql
class MyController {
def sessionFactory
def myAction() {
def sql = new Sql(sessionFactory.currentSession.connection())
sql.execute("select ....") //execute SQL using Groovy SQL
render(contentType:"application/json") {
//render your DB query results as JSON
//you could also use JsonBuilder to render JSON output
}
}
}