Spring MVC open index.jsp on "/"
12,303
Just add a @Controller
with an appropriate handler method
@Controller
public class RootController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String root() {
return "index";
}
}
assuming index.jsp
is in /WEB-INF/view
.
Author by
bsferreira
Updated on June 04, 2022Comments
-
bsferreira almost 2 years
How can I open index.jsp using this url
http://localhost:8080/myApp/
and how can I use an hyperlink like this<a href="/">HOME</a>
and go to index.jsp (http://localhost:8080/myApp/
)?Here's my web.xml:
<display-name>myApp</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>myApp</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myApp</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
And here is my myApp-servlet.xml:
<context:component-scan base-package="org.myApp.com" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean>
Thanks in advance!
-
bsferreira about 10 yearsIt works if a put in the url localhost:8080/myApp but not on the hyperlink
-
Sotirios Delimanolis about 10 years@user3476247 In a hyperlink, the single
/
means an absolute path relative to the host. That means it will dolocalhost:8080/
, notlocalhost:8080/myapp
. You need to provide the context path either directly or dynamically with${pageContext.request.contextPath}
. -
bsferreira about 10 yearsProblem solved. But now you make me wonder why my <a href="about">ABOUT</a> element doesn't need the context path :D
-
Sotirios Delimanolis about 10 years@user3476247 Because there is no preceding
/
. So when the browser generates the path to send the request to, it considers that value as relative to your current URL, replacing the last path element with your value. Sohref="somepath/more"
when you're at atlocalhost:8080/myapp/users/admins
, the browser will send the request tolocalhost:8080/myapp/users/somepath/more
.