当前位置: 首页 > 知识库问答 >
问题:

Spring MVC dispatcher servlet,不带应用程序名的URI

陈项禹
2023-03-14

我正试图联系我的控制器:

package com.atmWebApp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FirstController {

        @RequestMapping("/welcome/")
        public ModelAndView helloWorld() {

            String message = "<br><div align='center'>" + "<h1>Hello World, Spring 3.2.1 Example by Crunchify.com<h1> <br>";
            message += "<a href='http://crunchify.com/category/java-web-development-tutorial/'>More Examples</a>";
            return new ModelAndView("welcome", "message", message);
        }

        @RequestMapping("/")
        public ModelAndView helloWorld1() {

            String message = "<br><div align='center'>" + "<h1>Hello World, Spring 3.2.1 Example by Crunchify.com<h1> <br>";
            message += "<a href='http://crunchify.com/category/java-web-development-tutorial/'>More Examples</a>";
            return new ModelAndView("welcome", "message", message);
        }
}

应用程序servlet。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-3.0.xsd

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

网状物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"
    id="WebApp_ID" version="2.5">
    <display-name>AtmWebApp</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app><?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"
    id="WebApp_ID" version="2.5">
    <display-name>AtmWebApp</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

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

具体地说,我想启动我的服务器,让我的根目录位于localhost:8080/,但我似乎连FirstController都打不到。所以我的主要问题是:为什么我没有在FirstController中使用这两种方法?

我认为我的问题在于应用服务器。xml或web。xml文件。我在这里、这里、这里和这里查看了堆栈溢出,但我不知道我与这些人做了什么不同。

有什么帮助吗?

共有2个答案

马阳晖
2023-03-14

据我所知,你应该有一个@RequestMapping(…) 。因此,你应该有这样的东西:

@Controller
@RequestMapping("/")
public class FirstController {
    ...

此外,您还可以检查此响应中的URL映射。

宦炜
2023-03-14

在网上。xml文件,在servlet映射下,尝试将Url模式从/更改为/*。

 类似资料:
  • 我想知道一个应用程序的包名,我只知道那个应用程序的应用程序名。假设我想知道一个电子邮件应用程序的包名,只是它的名字,然后如何得到它 我只知道应用程序名。 这是代码,以获得所有的应用程序的包名称,但我需要知道特定的应用程序。

  • 我正在使用create-react-app开发ReactJS应用程序。 npm start和npm run build应用程序工作正常,该应用程序运行在端口号为4000或pushstate-server build的端口9000,问题是我需要在公共url(如http://sample/home/)中运行没有端口号的构建应用程序,当我直接浏览build/index.html时,它正确显示了主页,但路

  • 问题内容: 我需要从Docker容器中在后台运行的Java应用程序中启动Selenium。启动失败,因为在运行时无法访问X11环境。请参阅下面的内容。 我该怎么办? 问题 我从安装Java 8和Jetty 9.3.x 的简单程序开始运行一个简单的服务(实际上是selenium的东西)。该服务实际上是为了启动一些需要UI才能执行的事情而设置的。我遇到的问题是其中的任何内容执行失败,因为UI在我运行的

  • 大家好,请告诉我如何在普通jersey rest服务或任何没有spring的web应用程序上添加aop。我用这个链接试过了http://ganeshghag.blogspot.in/2012/10/demystifying-aop-getting-started-with.html但它没有起作用。在本例中,我添加了aop。META-INF中的xml。但它仍然没有检测到我用@Aspect注释添加的类

  • 我已经用JavaFX创建了我的应用程序。一切都很好,所以我使用Jpackage在Windows中制作了一个可安装的应用程序版本。 在我安装包之后,Windows计算机仍然要求安装JVM。 我的期望是,我可以使用Jpackage在任何计算机上安装我的JavaFX应用程序,并自动设置JVM。。用户只需点击图标并运行应用程序。 关于Jpackage中的正确命令的任何建议,以将我的JavaFX应用程序ja

  • 2.6.2.应用程序签名 Android 程序在安装前必须进行签名。在开发时,我们可以使用开发者密钥(debug key)作为临时签名——这个密钥已经包含在开发环境中。 但作为商品发布时,你就必须使用自己的密钥进行签名。关于签名的具体步骤可以在 Android 网站的Signing Your Application页面找到。在后面我们将具体地讨论这个话题。