java - Spring mvc calling controller from jsp results in wrong url -


i new in developing spring mvc. trying call controller jsp file. gave requestmapping annotation controller, when try call mainview.jsp found on url: (i testing on localhost) "/airlinedb_spring/views/mainview.jsp" (this works fine), project name disappears url , address gives me 404: "/passengers/" don't know why "airlinedb_spring/" disappears link, looks main problem.

my controller code:

@controller @requestmapping(value="/passengers") public class passengercontroller{      @suppresswarnings("unchecked")     @requestmapping(method=requestmethod.get)     public string list(model model) {         list<string> tl = new arraylist<string>();         tl.add("one");         tl.add("two");         tl.add("three");         model.addattribute("testlist", tl);         return "mainview";     } 

my jsp file's code is:

<html> <head> <meta http-equiv="content-type" content="text/html; charset=us-ascii"> <title>airline database</title> </head> <body>      <h2>passengers</h2>      <a href="/passengers/">click</a>      <c:foreach items="${model.testlist}" var="test" varstatus="loopstatus">          ${loopstatus.count}  ${test}<br/>      </c:foreach> </body> </html> 

my web.xml file:

  <context-param>     <param-name>contextconfiglocation</param-name>     <param-value>classpath*:web-inf/spring/*.xml</param-value>   </context-param>    <listener>      <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>   </listener>    <servlet>      <servlet-name>airlinedb</servlet-name>      <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>      <load-on-startup>1</load-on-startup>   </servlet>    <servlet-mapping>     <servlet-name>airlinedb</servlet-name>     <url-pattern>/</url-pattern>   </servlet-mapping> 

the servlet-context.xml contains this:

 <context:annotation-config/>  <context:component-scan base-package="airlinedb.controller"/>  <mvc:annotation-driven/> 

i searched through internet, found lot of problems in connection controller mapping, none of worked me, tried calling controller href="/passenger" got same 404. don't know configs missing, can see wrong url, have no idea might wrong. can me?

when <a href="/passengers/"> send like: http://localhost:8080/passengers/.

that's not want. want http://localhost:8080/airlinedb_spring/passengers/

so, either use <a href="/airlinedb_spring/passengers/"> or use that: <a href="${pagecontext.servletcontext.contextpath}/passengers/"> second option better pratice.

also, it's add mapping value requestmapping. try use follow:

@suppresswarnings("unchecked")     @requestmapping(value="/",method=requestmethod.get)     public string list(model model) {         list<string> tl = new arraylist<string>();         tl.add("one");         tl.add("two");         tl.add("three");         model.addattribute("testlist", tl);         return "mainview";     } 

add value="/" @requestmapping.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -