The configuration of spring and SPRINGMVC in Web.xml __spring

Source: Internet
Author: User

(1) Question: How do I configure the spring IOC container in a Web project?

A: If you need to use spring's IOC container in your Web project, you can make the following configuration in the Web project configuration file Web.xml:


<!--Spring Listener-->


<listener>
<listener-class>org.springframework.web. Contex T. contextloaderlistener</listener-class>
</listener>

--------------------------------------------------------------------------------------------------------------- ------------------------------------

(2) How do I configure Spring MVC in a Web project?

A: To use spring MVC, you need to configure its front-end controller Dispatcherservlet in Web.xml in the Web project configuration file:


<!--Spring MVC servlet-->


<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web. Servlet. Dispatcherservlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

</servlet>


<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>

</servlet-mapping>


===============================================================================================================


Spring-mvc.xml File Contents:


<?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: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.1.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.1.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">


<!--automatically scans the package so that Springmvc thinks the class with @controller annotations under the package is a controller-->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>


<context:component-scan base-package= "com. Xxxx.controller ">
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/>
</context:component-scan>


<!--define the prefix of the file to jump, view mode configuration-->
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/"/>
<property name= "suffix" value= ". jsp"/>
</bean>


<!--enable default configuration-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults= "true" >
<!--configuration Fastjson support-->
<bean class= "Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" >
<property name= "Supportedmediatypes" >
<list>
<value>application/json</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


<!--configuration file upload, if not using file upload can not configure, of course, if not, then the configuration file does not need to introduce the upload component package-->
<bean id= "Multipartresolver"
class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!--default encoding-->
<property name= "defaultencoding" value= "Utf-8"/>
<!--file size maximum-->
<property name= "maxuploadsize" value= "1048576000"/>
Maximum value in <!--memory-->
<property name= "maxinmemorysize" value= "10240"/>
</bean>


</beans>




===============================================================================================================

So <mvc:annotation-driven> tags are mainly used to help us process request mapping, determine which controller method to handle the current request, exception handling.

<context:component-scan/> Tags:
Its implementation class is Org.springframework.context.annotation.ComponentScanBeanDefinitionParser.
The annotation class used to scan the @repository @Service @Controller in the package is then registered in the factory.

and Context:component-scan activates @ required. @ resource,@ autowired, @PostConstruct @PreDestroy @PersistenceContext @PersistenceUnit. This allows you to use @autowired when the bean is applied. Primarily for annotations of entities to spring container management


============================================================================================== < Annotaion-driven/> Tags: contains the following two examples:

requestmappinghandlermapping Processing of @requestmapping annotations

Requestmappinghandleradapter the controller class that handles @controller annotations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.