unable to render index or mobile page with spring mobile -
i able project run fine until brought in spring-mobile. strings return in browser spring-mobile method returning:
@controller public class devicedetection { private logger logger = loggerfactory.getlogger(devicedetection.class); @requestmapping(value="/index") public string detectdevice(device device) { if (device.isnormal()) { system.out.println("inside isnormal()"); return "index"; } else if (device.ismobile()) { system.out.println("inside ismobile()"); return "mobilepage"; } else if (device.istablet()) { return "mobilepage"; } return "index"; } }
so decided needed internalresourceviewresolver gives me following error:
error creating bean name 'viewresolver' defined in class path resource [org/springframework/boot/autoconfigure/web/webmvcautoconfiguration$webmvcautocon figurationadapter.class]: initialization of bean failed; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.boot.autoconfigure.mobile
and
devicedelegatingviewresolverautoconfiguration$devicedelegatingviewresolverconfigur ation$internalresourceviewresolverdelegateconfiguration.viewresolver; nested exception org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [org.springframework.web.servlet.view.internalresourceviewresolver] defined: expected single matching bean found 2: getviewresolver,defaultviewresolver
resolver class
@configuration public class mvcconfiguration extends webmvcconfigureradapter { @bean public viewresolver getviewresolver() { internalresourceviewresolver resolver = new internalresourceviewresolver(); resolver.setprefix("/web-inf/"); resolver.setsuffix(".html"); return resolver; } @override public void configuredefaultservlethandling( defaultservlethandlerconfigurer configurer) { configurer.enable(); } }
main class
@springbootapplication public class stidhamfinancialapplication extends springbootservletinitializer { public static void main(string[] args) throws unknownhostexception { springapplication app = new springapplication(stidhamfinancialapplication.class); environment env = app.run(args).getenvironment(); system.out.println(string.format("access urls:\n----------------------------------------------------------\n\t" + "local: \t\thttp://127.0.0.1:%1s\n\t" + "external: \thttp://%2s:%3s\n----------------------------------------------------------", env.getproperty("server.port"), inetaddress.getlocalhost().gethostaddress(), env.getproperty("server.port"))); } @override protected springapplicationbuilder configure(springapplicationbuilder builder) { return builder.sources(stidhamfinancialapplication.class); } }
project structure:
i running on tomcat 8...
--------update 1--------------- deleted configuration package , add @responebody
detectdevice method so:
//if remove @responsebody complains of circular view path [index] @responsebody renders string , not view @requestmapping(value="/index") public @responsebody string detectdevice(device device) { if (device.isnormal()) { system.out.println("inside isnormal()"); return "index"; } else if (device.ismobile()) { system.out.println("inside ismobile()"); return "mobilepage"; } else if (device.istablet()) { return "mobilepage"; } return "index"; }
only string index
or mobilepage
render in browser. if delete @responsebody
project gives error saying following:
2016-05-29 08:55:03.682 error 8230 --- [nio-8080-exec-1] o.s.boot.context.web.errorpagefilter : forwarding error page request [/index.html] due exception [circular view path [index]: dispatch current handler url [/index] again. check viewresolver setup! (hint: may result of unspecified view, due default view name generation.)]
if remove /index
method ignored , spring-boot maps index file , works again need mobile site work.
-------------------update 2-------------------
ok added thymeleaf , index page rendering again. better devicedetection
working once project looks mobilepage
following error:
error resolving template "mobile/index", template might not exist or might not accessible of configured template resolvers
i had add follow ing @configuration
project work. spring-boots defaultviewresolver causing me many issues. here project structure now:
i updated project on git too.
first needed use thymeleaf , create viewresolver. spring-boots defaultviewresolver causing me issues:
@configuration public class webconfiguration extends webmvcautoconfiguration.webmvcautoconfigurationadapter { @override public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { configurer.enable(); } @bean public dispatcherservlet dispatcherservlet(){ dispatcherservlet dispatcherservlet = new dispatcherservlet(); dispatcherservlet.setthrowexceptionifnohandlerfound(true); return dispatcherservlet; } @bean public servletcontexttemplateresolver templateresolver() { servletcontexttemplateresolver resolver = new servletcontexttemplateresolver(); resolver.setprefix("/web-inf/"); resolver.setsuffix(".html"); resolver.settemplatemode("html5"); resolver.setorder(1); return resolver; } }
since using thymeleaf needed bindingresult , few other things. implemented springs mobile device
interface detect device.
@controller public class formcontroller { @requestmapping(value="/", method= requestmethod.get) public string contactform(@valid @modelattribute("person") contact contact, bindingresult bindingresult, httpservletrequest request, model model, device device) throws ioexception { if(bindingresult.haserrors()){ system.out.println("there error "+bindingresult); return "error"; } model.addattribute("contact", new contact()); if(device.isnormal()){ system.out.println("i normal"); return "index"; } else if(device.ismobile()){ system.out.println("i mobile"); return "mobilepage"; } else if (device.istablet()){ system.out.println("i tablet"); return "mobilepage"; } return "index"; }
now needed put different views in correct directories so:
that , working perfectly.
here form created use implement thymeleaf:
<!-- contact form --> <form action="#" th:action="@{/contact}" th:object="${contact}" method="post" class="contact-form clear-fix"> <div class="clear-fix"> <ul class="list-0 clear-fix"> <!-- name --> <li> <div class="block field-box"> <label for="contact-form-name" class="text-color-black">your name</label> <input type="text" th:field="*{name}" class="contact nomarr col-md-6 text-color-black" id="contact-form-name" /> </div> </li> <!-- /name --> <!-- e-mail address --> <li> <div class="block field-box"> <label for="contact-form-mail" class="text-color-black">your e-mail</label> <input type="text" th:field="*{email}" class="contact nomarr col-md-6 text-color-black" id="contact-form-mail" /> </div> </li> <!-- /e-mail address --> <!-- website url --> <li> <div class="block field-box"> <label for="contact-form-subject" class="text-color-black">subject</label> <input type="text" th:field="*{subject}" class="contact nomarr col-md-6 text-color-black" id="contact-form-subject" /> </div> </li> <!-- /website url --> <!-- message --> <li> <div class="block field-box "> <label for="contact-form-message" class="text-color-black">your message</label> <textarea name="comment" class="contact col-md-12 text-color-black" th:field="*{message}" id="contact-form-message"></textarea> </div> </li> <!-- /message --> <!-- submit button --> <li> <div class="block field-box field-box-button text-color-black"> <input type="submit" id="contact-form-submit" name="contact-form-submit" class="button text-color-black" value="submit"/> </div> </li> <!-- /submit button --> </ul> </div> </form>
Comments
Post a Comment