java - how to properly use Models on Spring 4? -
i new using spring's mvc (but ive been using years @ other php frameworks).
i have many doubts, read spring info , seems right , all.. ichecked tutorial http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/ , works , all, on controller part, there code dont understand, , id know how use models on spring.
as far know, models should call make calls db, so, services (interfaces , implementations) , dtos?
at example make this:
@controller public class helloworldcontroller { @requestmapping("/hello") public string hello(@requestparam(value="name", required=false, defaultvalue="world") string name, model model) { model.addattribute("name", name); return "helloworld"; } }
it receives model parameter... bet if there any, spring use default one, if want add more interaction , lets say, specifiy model call db? idea how that?
and if want add service... little bit clueless it, if me understand...
thanks in advance
model map representing data view needs. can contain 1 or more entities, or simple objects, or strings, or whatever want.
mvc doesn't require use of database. model not "call db". can inject repository controller load data db model.
@controller @requestmapping("/foo") public class foocontroller { @autowired private foorepository foorepository; @requestmapping string getfoos(model model) { list<foo> foos = foorepository.findall(); model.addattribute("foos", foos); model.addattribute("someotherdatayourviewneeds", "bar"); return "foo/list"; } }
Comments
Post a Comment