我有一个旧应用程序,它是基于xml配置的SPRING MVC应用程序。由于某些原因,我将其修改为具有XML和Java配置的Spring MVC应用程序。 我在下面提到了两个链接
https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/
但它不起作用,我想我错过了一些东西。请仔细阅读以下代码: -
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.
答案 0 :(得分:1)
mvc-context.xml应由disptacher servlet加载,而不是由根应用程序上下文加载。从AppConfig导入资源中删除它。这样
你的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>