SpringMVC example tutorial (2). springmvc example
Previous blog briefly introduced the SpringMVC framework to build, and then are the use of the default way, that is, the need to SpringMVC core configuration file in/WEB-INF/directory, it is automatically loaded by the project, but in our actual project development process, it is generally not handled in this way, and such default configuration is rarely used, and this default method looks messy, therefore, we usually separate the configuration files and manage them separately.
1. Manually specify the loading location of spring MVC's core configuration file
Solution:
1. Create a package under the src directory to place the configuration file
2. Move the springMVC configuration file under the original WEB-INF directory to the path of this configuration package.
3. Use it in the web. xml file<init-param>Configure the load path.
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:com/feizi/config/springmvc-servlet.xml</param-value> </init-param>
The configuration of the entire web. xml file 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" 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>springmvc_demo_04</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:com/feizi/config/springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Project Structure:
The subsequent operations are the same as the demo in the example tutorial (1. After the configuration, run it and check the effect in the browser.
Ii. SpringMVC Annotation
1. Enable annotation ing in the spring MVC core configuration file [servlet-name]-servlet. 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: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! -- Configure annotation support --> <mvc: annotation-driven> </mvc: annotation-driven> <! -- Enable automatic scan --> <context: component-scan base-package = "com. feizi. controller"> </context: component-scan> <! -- Configure the view parser --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> <! -- <Property name = "prefix" value = "/"> </property> --> <property name = "prefix" value = "/WEB-INF/jsp/"> </property> <! -- Set the view type to jsp --> <property name = "suffix" value = ". jsp"> </property> </bean> </beans>
2. Then you can use the annotation method in the Controller class.
/*** File Description * @ Description: Extension Description * @ Copyright: 2015 dreamtech.com.cn Inc. all right reserved * @ Version: V6.0 */package com. feizi. controller; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod;/*** @ Author: feizi * @ Date: 4:00:23, January 1, May 7, 2015 * @ ModifyUser: feizi * @ ModifyDate: 2015 May 7 4:00:23 * @ Version: V6.0 * // identify HelloController through @ Controller annotation. This class is a Controller @ Controllerpublic class HelloController {@ RequestMapping (value = "/hello ", method = RequestMethod. GET) public String sayHello () {System. out. println ("================================ Hello World !!! ================== "); Return" hello ";}}
Next, please note: we may report an error when starting tomcat. If tomcat is used, the following exception may be reported during startup:
Problem description: spring3.2 architecture cannot be started normally in tomcat6.0, and a java. lang. NoClassDefFoundError: javax/servlet/AsyncListener error is thrown.
Cause of exception:
1: org. springframework. web. servlet-3.2 supports version Servlet3.0.
2: tomcat6.0 only supports Servlet2.5, while tomcat7.0 supports Servlet3.0.
As a result, the requirement and supplier are not compatible with the Servlet version.
Solution:
Replace the servlet-api.jar file in the tomcat6.0/lib folder with tomcat7.0.
Note: Be sure to replace the jar in tomcat/lib. If you put it in WEB-INF/lib, an error will also be reported. Cause: tomcat6.0 provides a servlet-api.jar, and the order in which the jar file is loaded at startup starts from tomcat6.0/lib, and then loads the WEB-INF/lib. So put it in WEB-INF/lib only, the jar file is invalid.
We copy the servlet-api.jar package in the lib directory of the tomcat7.0 installation directory to the lib directory of the tomcat6.0 installation directory for replacement.
After that, start Tomcat and then it will start normally.
Run the following command to check the effect in the browser:
OK. The entry to SpringMVC is complete. Of course, the entry is very simple, however, the reason for SpringMVC, the reason for such design, and the case for choosing such a framework technology still need to be studied. SpringMVC framework is so popular, it is certainly for some reason, but this is not to say that one day or two things, ice freezing is not a day cold, cool, continue to work !!!
All demo examples of the two blogs have been packaged and uploaded to csdn:
Http://download.csdn.net/detail/hu1991die/8673299