angularjs - How to configure Spring and Angular to work together -
i have spring rest server (v3.2) , angularjs client code.
from understanding in basic scenario user navigates base domain .com, index.html being sent , and point angular manages communication.
my questions are: 1. how set spring return angular file. 2. how handle situation user not go though base domain , navigates .com/books/moby-dick returns json representation of moby-dick book suppose rendered client
a tutorial highly appreciated. web initialzer class:
public class webappinitializer implements webapplicationinitializer { private static logger log = loggerfactory.getlogger(webappinitializer.class); @override public void onstartup(servletcontext servletcontext) { webapplicationcontext rootcontext = createrootcontext(servletcontext); configurespringmvc(servletcontext, rootcontext); filterregistration.dynamic corsfilter = servletcontext.addfilter("corsfilter", corsfilter.class); corsfilter.addmappingforurlpatterns(null, false, "/*"); // configurespringsecurity(servletcontext, rootcontext); } private webapplicationcontext createrootcontext(servletcontext servletcontext) { annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); // rootcontext.register(coreconfig.class, securityconfig.class); rootcontext.register(coreconfig.class); servletcontext.addlistener(new contextloaderlistener(rootcontext)); servletcontext.setinitparameter("defaulthtmlescape", "true"); return rootcontext; } private void configurespringmvc(servletcontext servletcontext, webapplicationcontext rootcontext) { annotationconfigwebapplicationcontext mvccontext = new annotationconfigwebapplicationcontext(); mvccontext.register(mvcconfig.class); mvccontext.setparent(rootcontext); servletregistration.dynamic appservlet = servletcontext.addservlet( "webservice", new dispatcherservlet(mvccontext)); appservlet.setloadonstartup(1); set<string> mappingconflicts = appservlet.addmapping("/"); if (!mappingconflicts.isempty()) { (string s : mappingconflicts) { log.error("mapping conflict: " + s); } throw new illegalstateexception( "'webservice' cannot mapped '/'"); } }
this mvc configuration file:
@configuration @enablewebmvc @componentscan(basepackages = {"com.yadazing.rest.controller"}) public class mvcconfig extends webmvcconfigureradapter { @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/resources/**").addresourcelocations("/resources/"); } }
(disclaimer: author of jhipster)
you can have @ jhipster generate such application you, spring backend , angularjs frontend.
as generator goes far beyond need (security, etc), can have @ our sample application.
Comments
Post a Comment