无法通过混合XML和JavaConfig加载Spring MVC应用程序(Unable to load Spring MVC Application with Mixing of XML and Ja

编程入门 行业动态 更新时间:2024-10-23 17:28:53
无法通过混合XML和JavaConfig加载Spring MVC应用程序(Unable to load Spring MVC Application with Mixing of XML and JavaConfig)

我有一个旧的应用程序,它是基于xml的配置的SPRING MVC应用程序。 由于某些原因,我将它修改为具有XML和Java配置的Spring MVC应用程序。 我在下面提到了两个链接

https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/

用spring混合xml和java配置

但它不工作,我想我失去了一些东西。 请通过下面的代码: -

1. web.xml -- Please refer below the web.xml file. <?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> <display-name>spring-tutorial-mvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> </web-app> 2. mvc-config.xml :- Please refer below the dispacter servlet file. <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <context:component-scan base-package="com.org.smrs"/> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/> </beans> 3. app-ctx.xml - Please refer below the application context file. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.mpmvvcl.smrs" /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/local.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@host:port:sid" /> <property name="username" value="uname" /> <property name="password" value="pwd" /> </bean> </beans> 4.AppConfig.java :- The java configuration file to load the configurations ---------------- @Configuration @ImportResource({ "classpath:mvc-config.xml", "classpath:app-ctx.xml" }) public class AppConfig { } 5. ConsumerDetailController : Controller class --------------------------- @Controller @RequestMapping(value = "/user/ca") public class ConsumerDetailController { // -------------------Retrieve All @RequestMapping(value = "/consumer_list/", method = RequestMethod.GET) public ModelAndView getConsDetail() { ModelAndView mav = new ModelAndView("smrs/consumer_detail"); return mav; } } But when I hit url http://localhost:8080/Web/user/ca/consumer_list/ in browser i get 404 page . Please advice. Here i don't want to change to complete java configuration.

I have an old application which is SPRING MVC application with xml based configuration. Due to some reasons i have modified it as Spring MVC application with XML and Java Configuration. I referred below two links

https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/

Mixing xml and java config with spring

But it is not working , i guess i am missing something. Please go through the below code:-

1. web.xml -- Please refer below the web.xml file. <?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> <display-name>spring-tutorial-mvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> </web-app> 2. mvc-config.xml :- Please refer below the dispacter servlet file. <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <context:component-scan base-package="com.org.smrs"/> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/> </beans> 3. app-ctx.xml - Please refer below the application context file. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.mpmvvcl.smrs" /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/local.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@host:port:sid" /> <property name="username" value="uname" /> <property name="password" value="pwd" /> </bean> </beans> 4.AppConfig.java :- The java configuration file to load the configurations ---------------- @Configuration @ImportResource({ "classpath:mvc-config.xml", "classpath:app-ctx.xml" }) public class AppConfig { } 5. ConsumerDetailController : Controller class --------------------------- @Controller @RequestMapping(value = "/user/ca") public class ConsumerDetailController { // -------------------Retrieve All @RequestMapping(value = "/consumer_list/", method = RequestMethod.GET) public ModelAndView getConsDetail() { ModelAndView mav = new ModelAndView("smrs/consumer_detail"); return mav; } } But when I hit url http://localhost:8080/Web/user/ca/consumer_list/ in browser i get 404 page . Please advice. Here i don't want to change to complete java configuration.

最满意答案

mvc-context.xml应该由调度器servlet加载而不是由根应用程序上下文加载。 将其从AppConfig导入资源中删除。 这条路

根本应用程序上下文(在您的情况下为AppConfig)将由AnnotationConfigWebApplicationContext加载 servlet应用程序上下文mvc-context将由调度程序servlet加载

你的web.xml应该是这样的。 app-ctx.xml和mvc-context.xml,如果它们在webapp下不使用classpath,如果它们在资源下使用classpath前缀

<web-app> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support. AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>package to .AppConfig</param-value> </context-param> <listener> <listener-class>org.springframework.web.context. ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet. DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

mvc-context.xml should be loaded by the disptacher servlet not by the root application context. remove it from AppConfig import resource. This way

the root application context , which is in your case AppConfig , will be loaded by AnnotationConfigWebApplicationContext servlet application context, mvc-context, will be loaded by the dispatcher servlet

your web.xml should be something like this. app-ctx.xml and mvc-context.xml if they are under webapp don't use classpath, if they are under resources use classpath prefix

<web-app> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support. AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>package to .AppConfig</param-value> </context-param> <listener> <listener-class>org.springframework.web.context. ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet. DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

更多推荐

xml,org,value,电脑培训,计算机培训,IT培训"/> <meta name="description&quo

本文发布于:2023-04-29 07:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   加载   XML   Spring   JavaConfig

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!