Spring MVC的开放的index.jsp的“/”(Spring MVC open index.

2019-10-19 17:17发布

我怎样才能打开index.jsp,使用此URL http://localhost:8080/myApp/我如何可以使用超级链接这样<a href="/">HOME</a>并转到的index.jsp( http://localhost:8080/myApp/ )?

这里是我的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>

提前致谢!

Answer 1:

只需添加一个@Controller与适当的处理方法

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

假设index.jsp/WEB-INF/view



文章来源: Spring MVC open index.jsp on “/”