2014-03-30 28 views
1

如何使用此URL打開index.jsp http://localhost:8080/myApp/以及如何使用像這樣的超鏈接 <a href="/">HOME</a>並轉至index.jsp(http://localhost:8080/myApp/)?Spring MVC open index.jsp on「/」

這裏是我的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> 

這裏是我對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> 

在此先感謝!

回答

7

只是用適當的處理程序方法

@Controller 
public class RootController { 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String root() { 
     return "index"; 
    } 
} 

假設index.jsp是在/WEB-INF/view添加@Controller

+0

它適用於URL http:// localhost:8080/myApp /但不在超鏈接 – bsferreira

+0

@ user3476247在超鏈接中,單個'/'表示相對於主機的絕對路徑。這意味着它會執行'localhost:8080 /',而不是'localhost:8080/myapp'。您需要直接或通過'$ {pageContext.request.contextPath}'動態地提供上下文路徑。 –

+0

問題解決了。但是現在你讓我想知道爲什麼我的ABOUT元素不需要上下文路徑:D – bsferreira