java - Request resource not found when run Spring 4 controller on Tomcat -
i try run simple code can't seem work , call controller
@configuration @enablewebmvc @componentscan(basepackages = "com.mypackage") public class appconfig extends webmvcconfigureradapter { public void configureviewresolvers(viewresolverregistry registry) { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); registry.viewresolver(viewresolver); } /** * configure resourcehandlers serve static resources css/ javascript etc... */ @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/static/**").addresourcelocations("/static/"); } }
this configuration file have controller in com.mypackage follows:
@controller @requestmapping("/") public class testcontroler { @autowired appdataservice appdataservice; @requestmapping(value = "/hello") public model register(model model){ model.addattribute("name", "y"); return model; } @requestmapping("/") public string index(modelmap model){ date start = new date(); system.out.println(start); list<appdata> app = appdataservice.getlistappdata(); date end = new date(); system.out.println(end); string result = ""; (object : app) { result+=a+" <br>"; } model.addattribute("name", result); return "hello"; } }
and have added hello.jsp
. when run project on tomcat server through eclipse don't hello.jsp
resource not found, error 404. not sure doing incorrect here how understand should getting start page hitting controller because of @requestmapping("/")
not sure wrong or goes wrong here.
i have annotation version of web.xml
follows:
public class appinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class[] { appconfig.class }; } @override protected class<?>[] getservletconfigclasses() { return null; } @override protected string[] getservletmappings() { return new string[] { "/" }; } }
1). remove 1 more @requestmapping("/")
above testcontroler
class, because produces double mapping since it's there in index()
method.
2). modify configureviewresolvers()
return viewresolver
object instead. annotate @bean
:
@bean public viewresolver configureviewresolvers() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); return viewresolver ; }
like :
@configuration @enablewebmvc @componentscan(basepackages = "com.mypackage") public class appconfig extends webmvcconfigureradapter { @bean public viewresolver configureviewresolvers() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); return viewresolver ; } /** * configure resourcehandlers serve static resources css/ javascript etc... */ @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/static/**").addresourcelocations("/static/"); } }
Comments
Post a Comment