除了`/`

时间:2016-03-09 16:13:16

标签: java spring-mvc url-pattern

我试图通过编写一些简单的控制器来学习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>

调度-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是否足以将所有控制器网址从http://localhost:8080/MyApp更改为http://localhost:8080/MyApp/services

[UPDATE]

我设法找到了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]

[UPDATE]

根据建议,我使用<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!

[UPDATE]

这次我将"/home"添加到@RequestMapping并尝试导航到/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

2 个答案:

答案 0 :(得分:0)

尝试使用此模式<button class="k-button" id="get" type="button" onclick="$('#Grid').data('kendoGrid').dataSource.read();return false;">Filter</button> 这适用于我的应用

答案 1 :(得分:0)

似乎您已将应用程序部署为Root上下文,因为MyApp未被视为webcontext。它正在寻找终点&#34; / MyApp / services /&#34;

尝试将网址格式更改为&#34; / MyApp / services&#34;确认。