2

Spring MVC で奇妙な問題が発生しています。次のような単純なコントローラーがあります。

@Controller
@RequestMapping("admin")
public class AdminController {

@RequestMapping(value = "", method = RequestMethod.GET)
public String home() {
    return "home";
}

サーバーを実行して url: localhost/admin にアクセスすると、404 エラーが発生します。ビュー home.jsp が存在し、レンダリングする必要があります。春のイベントログを確認すると、次のように表示されます。

DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/admin]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /admin
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String be.roots.buildinginspector.web.controller.AdminController.home()]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'adminController'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/admin] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [home]] in DispatcherServlet with name 'appServlet'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'domainOfExpertise' of type [be.roots.buildinginspector.business.model.DomainOfExpertise] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.domainOfExpertise' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [home] in InternalResourceView 'home'
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/home]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /home
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/home]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'appServlet'

すべてが正しく処理されますが、単にビューを表示する代わりに、DispatcherServlet は要求されたビュー名の URL に対して新しい GET 要求を行います。

私のweb.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/spring/config-core-business.xml
                 classpath*:/spring/config-app-security.xml
    </param-value>
</context-param>

<!-- Spring Security filter -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring/appServlet/config-core-web.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

関連するスプリング コンテキスト パーツ (config-core-web.xml):

<resources mapping="/resources/**" location="../../../resources" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources
     in the /WEB-INF/views directory -->
<beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/"/>
    <beans:property name="suffix" value=".jsp"/>
</beans:bean>
4

5 に答える 5

3
@Controller
@RequestMapping("admin")
public class AdminController {

@RequestMapping(method = RequestMethod.GET)
public String home() {
    return "home";
}

home() 関数の @RequestMapping の 'value' プロパティを削除します。

于 2012-05-10T12:47:13.573 に答える
1

結局、これは tomcat 関連の問題であることが判明しました。何らかの理由で、IDE で構成を再作成すると、エラーが解決されました。ご協力いただきありがとうございます。

于 2012-05-18T12:02:10.400 に答える
0

これを試して:

@RequestMapping(value = "admin",
        method = {RequestMethod.GET, RequestMethod.POST })
于 2013-01-04T11:52:12.413 に答える
0

メソッドで定義されたリクエスト マッピング アノテーションは、コントローラが「/admin/home」で始まるリクエストに応答するように制限します。

次の変更を適用します。

@Controller
@RequestMapping("/admin")
public class AdminController {

   @RequestMapping(method = RequestMethod.GET)
   public String home() {
      return "home";
   }
}
于 2012-05-10T12:57:53.047 に答える
0

これは、web.xml のサーブレット マッピングの問題だと思います。web.xml で /admin アドレスのみに変更します。おそらく今、あなたは持っています:

<url-pattern>*</url-pattern>

次のように変更します。

<url-pattern>/admin/*</url-pattern>
于 2012-05-10T12:32:34.843 に答える