有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring Mvc不适用于除`/`

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

家庭控制器。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;
    }
}

网络。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不足以将所有控制器URL从http://localhost:8080/MyApp更改为http://localhost:8080/MyApp/services

[更新]

我设法从卡塔利纳找到了一些日志。我编辑了日志。属性文件并添加了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

[更新]

这一次,我将"/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) 个答案

  1. # 1 楼答案

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

    尝试将url模式更改为“/MyApp/services”以确认

  2. # 2 楼答案

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