مشكلة تكوين التعليق التوضيحي لـ Spring MVC
-
26-09-2019 - |
سؤال
أحاول تحسين تكوين Spring MVC الخاص بي حتى لا تتطلب ملف تكوين جديد لكل servlet أضفه ، لكنني أواجه مشكلات. لقد حاولت استخدام هذا البرنامج التعليمي كنقطة انطلاق ، لكنني أواجه مشكلة لا يمكنني اكتشافها.
المشكلة هي أنه عندما أقوم بالوصول إلى servlet ، أعود إلى خطأ 404. إليك التكوين الخاص بي ومقتطف Java التمثيلي من وحدة تحكم:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>SightLogix Coordination System</display-name>
<description>SightLogix Coordination System</description>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
/WEB-INF/application-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/slcs/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
/WEB-INF/application-security.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
</web-app>
Application-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-init-method="init" default-destroy-method="destroy">
<mvc:annotation-driven />
<context:component-scan base-package="top.level" />
</beans>
Application-security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http>
<intercept-url pattern="/**" access="ROLE_MANAGER" requires-channel="https" />
<http-basic />
</http>
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService">
<password-encoder hash="sha"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService"
class="path.to.my.UserDetailsServiceImpl">
</beans:bean>
</beans:beans>
مقتطف من فئة وحدة التحكم (واحدة من العديد ، لكنها جميعها تبدو مثل هذا بشكل أساسي):
@Controller
@RequestMapping("/foo.xml")
public class FooController
{
@RequestMapping(method=RequestMethod.GET)
public void handleGET(HttpServletRequest request, HttpServletResponse response) throws IOException
{
...
هل يمكن لأي شخص أن يخبرني ماذا أفعل بشكل غير صحيح؟ شكرًا!
المحلول
الشيء الوحيد في مكانه هو أنك استخدمت نفس ملفات تكوين السياق لكل من سياق WebApp الجذر و سياق servlet الخاص بك. يكاد يكون ذلك مضمونًا ليكون فكرة سيئة ، وسيؤدي إلى الكثير من السلوك الغريب. قد يكون هذا سبب مشكلتك.
ال ContextLoaderListener
تم تكوينه مع contextConfigLocation
<context-param>
, ويخلق ويدير الجذر WebApplicationContext
.
ال ServletDispatcherServlet
تم تكوينه مع contextConfigLocation
<init-param>
, ويخلق ويدير servlet WebApplicationContext
.
الجذر WebApplicationContext
هو والد servlet appcontext ، أي أي حبوب في الجذر WebApplicationContext
مرئية لتلك الفاصوليا في servlet WebApplicationContext
.
يجب أن تكون خطوتك الأولى هي فصل هذه التكوينات. مع الفاصوليا الصحيحة في الأماكن الصحيحة (على سبيل المثال ، يجب أن تذهب جميع الأشياء MVC في سياق Servlet). يفعل ليس شارك تعريفات الفول بين الاثنين ، وسوف تصبح مربكة و/أو كسر.
نصائح أخرى
هذا لا يجيب على سؤالك مباشرة ، لكنني وجدت دائمًا أنه من المفيد تمكين تسجيل تصحيح الأخطاء في الربيع عندما لا أستطيع معرفة الخطأ.
فيما يلي اثنان أستخدمه عادة في ملف التسجيل الخاص بي. properties ملف:
org.springframework.beans.factory.support.level=FINEST
org.springframework.security.level=FINEST
هل لديك أكثر من واحد GET RequestMapping
في وحدة التحكم الخاصة بك بأي فرصة؟ إذا كان هناك أكثر من واحد وهناك غموض في حلها إلى طلب معين ، فلا يخطط الربيع إلى أي من التعيينات الغامضة.