2016-05-07 27 views
1

我在日食以下Spring项目:设置Spring MVC Web应用程序的开始页面?

enter image description here

当我去:

http://[my-host]:8082/webapp-module/hello 

WEB/INF/JSP/hello.jsp中页面加载就好了。但我也想定义默认起始页(WEB/INF/index.jsp)之后,当我去:

http://[my-host]:8082/webapp-module 

目前不起作用。我需要为此添加一个单独的控制器吗?

的web.xml文件

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Application</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/webapp-module-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener>  

    <servlet> 
     <servlet-name>webapp-module</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>webapp-module</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

而且我的webapp模块-servlet.xml中文件:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:annotation-config/> 

    <context:component-scan base-package="com.samples" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

回答

1

第1步:移动的index.jsp /WEB-INF/jsp/文件夹内。

步骤2:在您的@Controller类添加以下方法:

@RequestMapping("/") 
public String home(){ 
    return "index"; 
} 

你完整的控制器类应该是这样的:

@Controller 
public class LoginController { 

    @RequestMapping("/") 
    public String home(){ 
     return "index"; 
    } 

    @RequestMapping("/hello") 
    public String showhello(){ 
     return "hello"; 
    } 
} 
相关问题