With the integration between spring and SPRINGMVC, the jar package in SPRINGMVC contains the jar packages in spring, so there is no need to import the jar package separately, just import the Springmvc jar package.
One of the following:
At this time, create a new two source folder, one for config file, and the other for Test, a program designed for testing, in this small integrated project, itself
The test source folder is not used, but it is also good to add.
Now, create a new two XML file in the Config folder, a spring configuration file for Applicationcontext.xml, and a SPRINGMVC configuration file of Springmvc.xml, two XML
The file is configured as follows:
Applicationcontext.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd BEAN 2.0//en" "Http://www.springframework.org/dtd/spring-beans-2.0.dtd" [<! ENTITY contextinclude SYSTEM "Org/springframework/web/context/web-inf/contextinclude.xml" >]><beans> <bean id= "Springmanager" class= "Cn.com.controller.SpringManager" ></bean></beans>
Springmvc.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" 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/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 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http ://www.springframework.org/schema/aop/spring-aop-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http Www.springframework.org/schema/tx/spring-tx-3.0.xsd "><!--MVC annotation-driven--><mvc:annotation-driven/> <!--once the scanner isMvc:annotation-driven does not need, the scanner already has the annotation-driven function--><context:component-scan base-package= "Cn.com.controller"/> <!--prefix + viewName + suffix--><bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" ><!--Webroot The path to a specified folder--><property name= "prefix" value= "/web-inf/jsps/" ></property><!- -View name suffix--><property name= "suffix" value= ". jsp" ></property></bean><!--id= " Multipartresolver "must be Multipartresolver--><bean id=" Multipartresolver "class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "><!--maxuploadsize: The maximum file upload value is in bytes-- ><property name= "maxuploadsize" value= "1024000" ></property></bean><!--<MVC: interceptors><mvc:interceptor> interception of a module:/mypath/**, intercepting all requests/**<mvc:mapping path= "/**"/><bean class= "Cn.itcast.springmvc.interceptor.MyIntercepor" ></BEAN></MVC:INTERCEPTOR></MVC: Interceptors>--></beans>
Now, to configure the appropriate configuration in Web. XML, the Web. config is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 "> <context-param> <param-name>contextConfigLocation</param-name> < Param-value>classpath:applicationcontext.xml</param-value> </context-param> <!-- Configure Spring Start Listener entry-<listener> <listener-class> org.springframework.web.context.contextloaderlistener</listener-class></listener><!-- Configure SPRINGMVC to start the Dispatcherservlet Ingress--><!--Central Controller--><servlet><servlet-name>springmvc</ Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param><param-name>contextconfiglocation</param-name&gT;<param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1 </load-on-startup></servlet><filter><filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class><init-param ><param-name>encoding</param-name><param-value>utf-8</param-value></init-param ><init-param><param-name>forceencoding</param-name><param-value>true</ param-value></init-param></filter><!--encoding filter for JSP page--><filter-mapping> <filter-name>encodingfilter</filter-name><url-pattern>/*</url-pattern></ filter-mapping> <servlet-mapping><servlet-name>springMVC</servlet-name><!--struts wont use/* , in Springmvc, regardless of--><url-pattern>/</url-pattern></servlet-mapping></web-app>.
Now set up the scanned package in Springmvc.xml, with the following package name and class name:
The following sequence is the code for each program:
Ispring:
Package Cn.com.controller;public interface Ispring {void get ();}
Springmanager:
Package Cn.com.controller;public class Springmanager implements ispring {public void get () {System.out.println (" Spring and Spring MVC integration success! ");}}
Usercontroller:
Package Cn.com.controller;import Javax.annotation.resource;import Javax.servlet.http.httpservletrequest;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.context.webapplicationcontext;import Org.springframework.web.context.support.webapplicationcontextutils;import org.springframework.web.servlet.support.RequestContextUtils; @Controller @requestmapping ("/user") public class Usercontroller {@Resource (name= "Springmanager") private ispring Springmanager; @RequestMapping ("/tosuccess.do") Public String tosuccess (HttpServletRequest request) {//spring context Webapplicationcontext AC1 = Webapplicationcontextutils.getwebapplicationcontext (Request.getsession (). Getservletcontext ());// SPRINGMVC Context Webapplicationcontext Ac2=requestcontextutils.getwebapplicationcontext (request); SpringManager.get ( ); return "Success";}}
So, the integration of spring and SPRINGMVC is complete, now let's run the project and see if it's successful.
Debug Debug Watch Ac1 and AC2:
As shown in the following:
Spring Context:
Spring MVC context:
The JSP page is:
In this way, the integration of spring and SPRINGMVC is successful.
Spring and Spring MVC integration