2016-03-09 41 views
0

我试图通过编写一些简单的控制器来学习Spring Mvc。这是我到目前为止有:Spring Mvc不能与`/`以外的任何`url-pattern`一起使用

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

[更新]

我设法找到一些日志了卡特琳娜的。我编辑了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!

[更新]

这一次我加"/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 

回答

0

尝试这种模式<url-pattern>/services/*</url-pattern>这对我的作品在我的应用程序

+0

谢谢,但它不适合我!我已经测试了所有你能想象得到的,它不起作用。值得一提的是我找不到错误日志。这是一个码头形象,标准输出显示没有错误,所以永远! – Mehran

+1

@Mehran \t然后您的日志记录可能配置错误(假设有错误)。 –

+0

@DaveNewton没有'/ usr/local/tomcat/logs/catalina.out',当我将'attach'添加到容器时,刷新浏览器时没有条目添加到stdout!你能帮我找到错误日志吗? – Mehran

0

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

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

+0

仍然是完全相同的错误。 – Mehran

+0

更改你的控制器有url“/ home”,并尝试检查是否全部正确,并在浏览器中尝试url/MyApp/services/home –

+0

对不起,我的坏。这不是完全相同的日志。我已将新日志添加到问题中。 – Mehran

相关问题