Simple SpringMVC program instance and springmvc instance
Simple StringMVC program example
Step 1: import the jar package
Step 2: Create a WEB-INF file under the spring-servlet.xml folder.
<? 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: p =" http://www.springframework.org/schema/p "Xmlns: context =" http://www.springframework.org/schema/context "Xmlns: aop =" http://www.springframework.org/schema/aop "Xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans Spring-beans-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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context /Spring-context-3.0.xsd "> <! -- Register the aop function --> <aop: aspectj-autoproxy/> <! -- Automatically scan com. all class files under the springmvc directory and Its subdirectories are automatically injected with all annotation classes --> <context: component-scan base-package = "com. yxl. * "/> <! -- Process the returned value of the response request. The following configuration correctly returns the string-type return value. If the returned value is an object, it is automatically converted to json --> <bean id = "handleAdapter" class = "org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "> <property name =" messageConverters "> <list> <ref bean =" mappingJacksonHttpMessageConverter "/> <! -- Json converter --> <ref bean = "mappingStringHttpMessageConverter"/> </list> </property> </bean> <bean id = "mappingJacksonHttpMessageConverter" class = "org. springframework. http. converter. json. mappingJacksonHttpMessageConverter "/> <bean id =" mappingStringHttpMessageConverter "class =" org. springframework. http. converter. stringHttpMessageConverter "/> <! -- Resolve the Model View name, that is, add the prefix suffix to the model view name --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"/> <property name =" suffix "value = ". jsp "> </property> <property name =" order "value =" 1 "> </property> </bean> <! -- Spring File Upload encoding --> <bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. commonsMultipartResolver "p: defaultEncoding =" UTF-8 "/> </beans>
Step 3: Configure springmvc in the web. xml file.
<? 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 "> <display-name> springmvc1 </display-name> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index. jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default. jsp </welcome-file> </welcome-file-list> <! -- Set encoding --> <filter-name> characterEncodingFilter </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> <! -- Configure DispatchcerServlet --> <servlet-name> Spring MVC Dispatcher Servlet </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <! -- Configure the location and name of the configuration file under Spring mvc --> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/spring-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <! -- Configure the DispatchcerServlet blocking path '/' All accesses are blocked --> <servlet-mapping> <servlet-name> Spring MVC Dispatcher Servlet </servlet-name> <url-pattern>/ </url-pattern> </servlet-mapping> </web-app>
Step 4: Create a controller.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!