Java Spring MVC 在“/”上打开 index.jsp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22745704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:39:48  来源:igfitidea点击:

Spring MVC open index.jsp on "/"

javaspringjspspring-mvcservlets

提问by bsferreira

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/)?

如何使用此 url 打开 index.jsphttp://localhost:8080/myApp/以及如何使用这样的超链接 <a href="/">HOME</a>并转到 index.jsp ( http://localhost:8080/myApp/)?

Here's my web.xml:

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

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

提前致谢!

采纳答案by Sotirios Delimanolis

Just add a @Controllerwith an appropriate handler method

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

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

assuming index.jspis in /WEB-INF/view.

假设index.jsp/WEB-INF/view.