Spring MVC -- Configuration details and annotations

Keywords: Spring JSP JSON xml

Links to the original text: https://my.oschina.net/u/1781072/blog/542647

<?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:tx="http://www.springframework.org/schema/tx"
	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/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
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!-- Package name for automatic scanning -->
    <context:component-scan base-package="com.app,com.core,JUnit4" ></context:component-scan>
    
    <!-- Default annotation mapping support -->
    <mvc:annotation-driven />
    
    <!-- View Interpretation Class -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/jsp/"/>
    	<property name="suffix" value=".jsp"/><!--Can be empty,Easy to implement your own logic of selecting View interpretation classes based on extensions  -->
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    </bean>
    
	<!-- Interceptor -->
    <mvc:interceptors>
		<bean class="com.core.mvc.MyInteceptor" />
	</mvc:interceptors>	  
	
 	<!-- Access Scheme 1 for Static Resource Files (Option 2) -->
 	<mvc:default-servlet-handler/>
 	
 	<!-- Access Scheme 2 for Static Resource Files (Option 2)-->
	<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
	<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
	<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

</beans>


<context: component-scan/> scans annotations on classes in specified packages. Commonly used annotations are:

@Controller Declare Action Components
@ Service: Declare Service Component @Service("myMovieLister")
@ Repository declares the Dao component
@ Component refers to a component in general, when it is not easy to categorize.
@ Request Mapping ("/menu") Request Mapping
@ Resource is used for injection, (provided by j2ee) is assembled by name by default, @Resource (name= "bean Name")
@ Autowired is used for injection, (provided by srping) default type-by-type assembly
@ Transactional (rollbackFor={Exception.class}) transaction management
@ ResponseBody is used to read the body part of the Request request, parse it with the HTTP MessageConverter configured by default, and then bind the corresponding data to the object to be returned.
@ Scope("prototype") sets the scope of the bean

 

<mvc: annotation-driven/> is a shorthand form, which can be replaced by manual configuration. The shorthand form can enable beginners to quickly apply the default configuration scheme. <mvc: annotation-driven/> automatically registers Default Annotation Handler Mapping and Annotation Method Handler Adapter bean s, which are necessary for spring MVC to distribute requests for @Controllers.
It also provides: data binding support, @NumberFormatannotation support, @DateTimeFormat support, @Valid support, JAXB support, JSON support.
json support is used when we deal with responses to ajax requests.
When writing JUnit unit tests for action, you need to take Default Annotation Handler Mapping and Annotation Method Handler Adapter beans from the spring IOC container to complete the test. You need to know that the two beans registered in the sentence <mvc: annotation-driven/> are the two beans.


<mvc:interceptors/> is a short form. You can configure multiple Handler Mapping by looking at the big picture above. <mvc: interceptors/> injects an interceptor for each Handler Mapping. In fact, we can also manually configure each Handler Mapping to inject an interceptor.

 

<mvc: default-servlet-handler/> uses the default Servlet to respond to static files.

 

<mvc: resources mapping="/images/**" location="/images/" cache-period="31556926"/> matching URL/images/** URLs are treated as static resources, read out by Spring into memory and then respond to http


Spring MVC--Detailed explanation of Dispatcher Servlet

Reproduced in: https://my.oschina.net/u/1781072/blog/542647

Posted by strangebeer on Sun, 08 Sep 2019 07:54:48 -0700