【问题标题】:how do you create 2 servlets within one web.xml file?如何在一个 web.xml 文件中创建 2 个 servlet?
【发布时间】:2014-12-26 16:59:35
【问题描述】:

这是我的 web.xml 文件中一个 servlet 的代码:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>

        <welcome-file-list>
            <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>

我可以复制粘贴并更改某些值并再次使用吗?

第二个问题(可能有助于回答第一个问题)

目前,当我使用此 url localhost/HelloSpring 在 glassfish 中部署我的应用程序时 我得到一个 404。但是,当我像这样在末尾添加 /home 时:localhost/HelloSpring/home 它转到正确的页面。我还可以在最后添加 /index localhost/HelloSpring/index ,它也可以正常工作。那么如何从启动加载 localhost/HelloSpring/home 而不必手动输入,从而避免 404 错误呢?

注意:在 servlet 映射中将 url 模式从 / 更改为 /home 时,修复了 /home 问题,但随后意味着 /index 将不起作用

总而言之,我怎样才能从部署中加载 localhost/home,然后在 url /index 中输入 index 并显示正确的页面?

附言stackoverflow 不会让我输入完整的地址链接,所以假设我说 localhost 它包含端口 :8080 直接在

之后

【问题讨论】:

  • 我认为这是您的 url 模式,我正在使用类似 *.html 的东西来处理所有 .html 请求,因此您可能需要使用这。我不记得 /* 是否合法。只是一个大概的想法
  • 这是我以前使用的,但我认为没有 .html 扩展名的 Web 应用程序看起来更整洁。我希望只是在没有扩展的情况下做到这一点。这绝对是可能的,因为大多数网站都这样做。我也想过/*,但在那里也找不到快乐

标签: xml spring spring-mvc servlets web


【解决方案1】:

您的 servlet 不能自动识别主页。您必须通过使用重定向页面(您已经将其包含在欢迎文件列表中)来告诉它,或者将提供主页的控制器也映射到根目录:

@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)

【讨论】:

  • 不幸的是它不喜欢那样。将您的代码行复制并粘贴到我的代码中,然后尝试使用 / 和 /home 的 url 模式。它给了我和以前一样的结果。这是从部署localhost:8080/HelloSpring 加载的网址可能与此有关吗?
  • 你能发布处理 /home 请求的控制器吗?
  • 打包服务;导入 org.springframework.stereotype.Controller;导入 org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.bind.annotation.RequestMethod; @Controller public class WebController { @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET) public String home() { return "home"; } @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } }
  • 抱歉,看起来很乱。转贴在下面没有导入:@Controller public class WebController { @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET) public String home() { return "home"; } @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } }
  • 看起来不错。您是否重新部署了应用程序?顺便说一句,您可以在下次需要附加代码时重新编辑您的问题。不要在 cmets 中这样做。
【解决方案2】:

对于第一个问题,您当然可以在 web.xml 文件中声明任意数量的 servlet,但我实在想不出一个声明多个 DispatcherServlet 的用例。

您的第二个问题似乎与在 Spring MVC 应用程序中点击根 url 的问题有关。我前段时间尝试在 SO 中发布我发现的问题:Match for root url and serving of static resources

【讨论】:

  • 所以我可以复制并粘贴当前 servlet 的所有代码,然后重命名吗?
  • 当然可以,但是你必须担心哪个servlet会被哪个请求命中,并相应地配置控制器
猜你喜欢
  • 2014-03-07
  • 2015-12-11
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多