简体   繁体   English

Spring MVC调度程序servlet,不带应用程序名称的URI

[英]Spring MVC dispatcher servlet, URIs without the application name

I am trying to reach my controller: 我正在尝试联系我的控制器:

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);
        }
}

app-servlet.xml: app-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>

web.xml: 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"
    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>

Specifically, I would like to start my server and have my root be at localhost:8080/, but I can't even seem to hit FirstController. 具体来说,我想启动我的服务器,并让我的根目录位于localhost:8080 /,但我什至看不到FirstController。 So my main question is: why am I not hitting either method in FirstController? 所以我的主要问题是:为什么我不点击FirstController中的任何一个方法?

I believe that my issue is in the app-server.xml or web.xml files. 我相信我的问题出在app-server.xml或web.xml文件中。 I have looked through stack overflow here , here , here , and here , but am at a loss as to what I am doing differently than these folks. 我在这里这里这里这里浏览了堆栈溢出,但是我在做什么方面与这些人不同。

Any help? 有什么帮助吗?

From what I know you should have a @RequestMapping(..) on the Controller also. 据我了解,您还应该在Controller上有一个@RequestMapping(..) Hence you should have something like this: 因此,您应该具有以下内容:

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

As well you can check also this response for URL mappings. 您也可以检查响应的URL映射。

在web.xml文件的servlet映射下,尝试将Url-Pattern从/更改为/ *。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM