繁体   English   中英

Spring Mvc不能与`/`以外的任何`url-pattern`一起使用

[英]Spring Mvc does not work with any `url-pattern` other than `/`

我正在尝试通过编写一些简单的控制器来学习Spring Mvc。 到目前为止,这是我得到的:

HomeController.java

package com.mehran.controller;

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

@Controller
public class HomeController
{
    @RequestMapping(value = "/")
    public ModelAndView index()
    {
        ModelAndView mav = new ModelAndView("home");
        String msg = "Running HomeController.index() method";
        mav.addObject("msg", msg);
        return mav;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Intellij Idea Native Spring MVC</display-name>

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

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

    <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>
</web-app>

dispatcher-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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.mehran.controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

上面给定的代码工作正常,我可以在输出中看到字符串。 但是,如果我将url-pattern更改为<url-pattern>/services</url-pattern> ,则无法再获得输出。

我在这里误会什么吗? 更改url-pattern是否足以将所有控制器URL从http://localhost:8080/MyApp更改为http://localhost:8080/MyApp/services

[更新]

我设法从Catalina登出一些日志。 我编辑了logging.properties文件并添加了org.apache.catalina.level=ALL 我不确定是否还有其他事情可以做! 但是在这里:

09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/MyApp/services]
09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]
09-Mar-2016 17:52:38.698 FINE [http-nio-8080-exec-3] org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId  Requested cookie session id is E9055A049B294DCFE62C0264580241A6
09-Mar-2016 17:52:38.699 FINE [http-nio-8080-exec-3] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /MyApp/services
09-Mar-2016 17:52:38.699 FINE [http-nio-8080-exec-3] org.apache.catalina.realm.RealmBase.findSecurityConstraints   No applicable constraints defined
09-Mar-2016 17:52:38.699 FINE [http-nio-8080-exec-3] org.apache.catalina.authenticator.AuthenticatorBase.invoke  Not subject to any constraint
09-Mar-2016 17:52:38.699 WARNING [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/MyApp/services] in DispatcherServlet with name 'dispatcher'
09-Mar-2016 17:52:38.711 FINE [http-nio-8080-exec-8] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/favicon.ico]
09-Mar-2016 17:52:38.711 FINE [http-nio-8080-exec-8] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 17:52:38.711 FINE [http-nio-8080-exec-8] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]

[更新]

根据建议,我使用了<url-pattern>/MyApp/services</url-pattern> ,结果如下:

09-Mar-2016 18:16:59.085 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/MyApp/services/]
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId  Requested cookie session id is E9055A049B294DCFE62C0264580241A6
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /MyApp/services/
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.realm.RealmBase.findSecurityConstraints   No applicable constraints defined
09-Mar-2016 18:16:59.086 FINE [http-nio-8080-exec-30] org.apache.catalina.authenticator.AuthenticatorBase.invoke  Not subject to any constraint
09-Mar-2016 18:16:59.099 FINE [http-nio-8080-exec-31] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/favicon.ico]
09-Mar-2016 18:16:59.100 FINE [http-nio-8080-exec-31] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 18:16:59.100 FINE [http-nio-8080-exec-31] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]

警告消失了,但输出结果(在浏览器中)仍然是404!

[更新]

这次,我在@RequestMapping添加了"/home" ,并试图导航到/MyApp/services/home

09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [uriBC] has value [/MyApp/serviecs/home]
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [semicolon] has value [-1]
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parsePathParameters The variable [enc] has value [utf-8]
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId  Requested cookie session id is E9055A049B294DCFE62C0264580241A6
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /MyApp/serviecs/home
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.realm.RealmBase.findSecurityConstraints   No applicable constraints defined
09-Mar-2016 18:23:19.592 FINE [http-nio-8080-exec-43] org.apache.catalina.authenticator.AuthenticatorBase.invoke  Not subject to any constraint

试试这个模式<url-pattern>/services/*</url-pattern>在我的应用中对我有用

似乎您已将应用程序部署为Root上下文,因为MyApp没有被视为webcontext。 它正在寻找端点“ / MyApp / services /”

尝试将网址格式更改为“ / MyApp / services”以进行确认。

暂无
暂无

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

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