spring - The requested resource is not available when wanna map to .html file -


i'm using spring mvc, , wanna make web app send json data client , client should visualized them using js.

i have questions:

1-my project have *.html beside *.jsp file how can handle both without web.xml. code had written work fine *.jsp give "the requested resource not available." error mapping html files.

2-as said service have send list of object in json form client, when want parse json string on server in *.jsp file code like

myclass data = new gson().fromjson(mylist.get(0).tostring(), listtype) 

,i face many problems, decide job in client side in *.html file, want know how should handle parsing job when don't want client know the structure class? or plz tell if have share class client, how should send stucture of html file?

3-how should access data created in restapi.jsp on usersideapp.html file?

these files:

appinitializer.java

public class appinitializer implements webapplicationinitializer {  public void onstartup(servletcontext container) throws servletexception {      annotationconfigwebapplicationcontext ctx = new annotationconfigwebapplicationcontext();     ctx.register(appconfig.class);     ctx.setservletcontext(container);      servletregistration.dynamic servlet = container.addservlet(             "dispatcher", new dispatcherservlet(ctx));      servlet.setloadonstartup(1);     servlet.addmapping("/"); } } 

appconfig.java

public class appconfig {  @bean public viewresolver viewresolver() {     internalresourceviewresolver viewresolver = new internalresourceviewresolver();     viewresolver.setviewclass(jstlview.class);     viewresolver.setprefix("/web-inf/views/");     viewresolver.setsuffix("");      return viewresolver; } } 

the part of appcontoller.java

@controller @requestmapping("/") public class appcontroller {  @autowired hackdataservice service;  @requestmapping(value = "/restapi", method = requestmethod.get) public string jsonapi(modelmap model) {     list<hackdata> newlist = service.findallnewdata();     objectwriter ow = new objectmapper().writer().withdefaultprettyprinter();     string json="";     try {         json = ow.writevalueasstring(newlist);     } catch (jsonprocessingexception e) {         e.printstacktrace();     }      model.addattribute("list", json);     string newjson = new gson().tojson(newlist);     model.addattribute("newlist", newjson);     return "newtest.jsp"; }  @requestmapping(value = "/app", method = requestmethod.get) public string htmlapp() {     return "usersideapp.html"; }  } 

and both .html , .jsp files in "/web-inf/views/"

after spending day on first problem, find answer of it, first of instead of implementing webapplicationinitializer, extend abstractannotationconfigdispatcherservletinitializer , java configing extends webmvcconfigureradapter.

then create pages folder in web-inf , codes changes these:

appinitializer became other internet's samples.

appconfig.java

@configuration @enablewebmvc @componentscan(basepackages = { "com.attackmap" }) public class appconfig extends webmvcconfigureradapter {  @override public void addresourcehandlers(resourcehandlerregistry registry) {     registry.addresourcehandler("/pages/**").addresourcelocations("/web-inf/pages/");   @bean public viewresolver viewresolver() {      internalresourceviewresolver viewresolver = new internalresourceviewresolver();     viewresolver.setviewclass(jstlview.class);     viewresolver.setprefix("/web-inf/views/");     viewresolver.setsuffix("");     return viewresolver; } } 

appcontroller.java

@controller @requestmapping("/") public class appcontroller {  @requestmapping(value = { "/", "/info" }, method = requestmethod.get) public string welcome(modelmap model) {     model.addattribute("message", "home page info site");     return "homepage.jsp"; } @requestmapping(value = "/finalapp", method = requestmethod.get) public string test() {     return "redirect:/pages/final.htm"; } } 

there way

creating "resources" directory in "src/main/webapp"

add bellow cod in appconfig.java

registry.addresourcehandler("/web-inf/views/resources/*.html").addresourcelocations("/resources/"); 

and viewresolving use code bellow:

@requestmapping(value = "/newapp", method = requestmethod.get) public string test2() {     return "/resources/usersideappnew.html"; } 

but still 2 other questions unsolved, main problem first 1 if know sth, these 2 plz tell me them


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -