我的控制器/servlet 带有“/”映射不会覆盖“Hello App Engine!”

发布于 2024-12-27 18:24:30 字数 2453 浏览 3 评论 0原文

我正在谷歌应用程序引擎上使用 Spring MVC,尽管我已经有了一个基本的 hello world 示例,但当我使用“/”的请求映射时,我无法让我的 servlet 显示出来。即使我在控制器中指定“/”作为请求映射,我仍然会收到“Hello App Engine!”页面包含我的项目的链接。我已经从我的 web xml 中提取了欢迎文件声明。

基本上...

package my.package.for.spring.stuff.controllers;

import ....;

// It doesn't seem to make a difference if 
// I have this reqeustmapping or not...
@Controller
public class MainController {

  // If I change mapping to "/main" and then go to
  // localhost:8888/main then everything works as expected
  @RequestMapping("/")
  public String HelloWorld() {
    return "MyView";
  }
}

仍然会转到“Hello App Engine!”页。另外,这是我的 web.xml...

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


    <servlet>
        <servlet-name>SpringAppEngine</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringAppEngine</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

然后这是我的 spring xml...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <context:component-scan base-package="my.package.for.spring.stuff" />

        <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                p:prefix="/WEB-INF/views/main/" p:suffix=".jsp" />

</beans>

即使我在控制器中声明根映射,为什么应用程序引擎生成的欢迎文件仍然显示?我知道我的设置应该是正确的,因为当我更改请求映射时,一切都会按预期进行。

I'm working with Spring MVC on google app engine and even though I've gotten a basic hello world example working, I can't get my servlet to show up when I use the request mapping of "/". Even when I specify "/" as my request mapping in my controller, I keep getting the "Hello App Engine!" page with a link to my project. I've already pulled the welcome-file declaration out of my web xml.

Basically...

package my.package.for.spring.stuff.controllers;

import ....;

// It doesn't seem to make a difference if 
// I have this reqeustmapping or not...
@Controller
public class MainController {

  // If I change mapping to "/main" and then go to
  // localhost:8888/main then everything works as expected
  @RequestMapping("/")
  public String HelloWorld() {
    return "MyView";
  }
}

is still going to the "Hello App Engine!" page. Also, here is my web.xml...

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


    <servlet>
        <servlet-name>SpringAppEngine</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringAppEngine</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

And then here is my spring xml...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <context:component-scan base-package="my.package.for.spring.stuff" />

        <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                p:prefix="/WEB-INF/views/main/" p:suffix=".jsp" />

</beans>

Why is the app engine generated welcome file still showing up even though I'm declaring the root mapping in my controller? I know my setup should be right because when I change the requestmapping, everything works as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年少掌心 2025-01-03 18:24:30

servlet 3.0 规范说:

仅包含“/”字符的字符串表示“默认”
应用程序的 servlet。

它还说:

默认情况下,所有应用程序都会有index.htm(l)和index.jsp
欢迎文件列表的列表。该描述符可用于
覆盖这些默认设置。

因此,我猜测容器认为隐式 index.html 欢迎文件是完全匹配的,它优先于映射到 / 的默认 servlet。

解决方案是删除index.html 文件,或者可能在描述符中定义一个显式的空欢迎文件列表。

The servlet 3.0 spec says:

A string containing only the ’/’ character indicates the "default"
servlet of the application.

And it also says:

By default all applications will have index.htm(l) and index.jsp in
the list of welcome-file-list. The descriptor may to be used to
override these default settings.

So, I guess that the container considers that the implicit index.html welcome file is an exact match, which takes precedence over the default servlet mapped to /.

The solution is to delete the index.html file or, probably, to define an explicit empty welcome file list in the descriptor.

下壹個目標 2025-01-03 18:24:30

显然,从 web.xml 中提取 index.html 映射是不够的,您实际上必须删除 index.html。如果有人可以发布答案解释为什么我仍然会投票并接受。

Apparently pulling the index.html mapping out of the web.xml isn't enough, you actually have to delete the index.html. If someone can post an answer explaining why I'll still upvote and accept.

彼岸花似海 2025-01-03 18:24:30

这篇文章对我有帮助,我想我可以扩展为什么删除文件可以解决问题。根据 GAE,war 目录中的任何文件(JSP 和 WEB-INF 中的任何文件除外)都会获得到该文件名的映射。此隐式映射似乎取代了 web.xml 中的任何 Servlet 规则:

https://developers .google.com/appengine/docs/java/gettingstarted/staticfiles

删除或重命名 index.html 后,将使用“/”的 Servlet 规则,而不是静态文件的隐式映射。

This post helped me and I think I can expand on why removing the file fixes the problem. According the GAE, any file in the war directory (except JSPs and anything in WEB-INF) gets a mapping to that file name. This implicit mapping appears to supercede any servlet rules in web.xml:

https://developers.google.com/appengine/docs/java/gettingstarted/staticfiles

Once you remove or rename index.html, your servlet rule for "/" is used instead of the implicit mapping for the static file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文