Configuration of the front-end controller for SPRINGMVC

Source: Internet
Author: User
Tags aop

Step: Configure the Web.xml--> configuration springmvc-servlet.xml (SPRINGMVC configuration file, which needs to be configured with processor adapters, processor Mapper, configuration view parser)

The front-end controller is a servlet:dispatcherservlet:

In the Web.xml:

<!--configuration Springmvc dispatcherservlet-->
    <servlet>
        <servlet-name>springmvc</ Servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class >
         You need to specify the spirngmvc.xml position in the initialization parameter.
(You need to configure the processor mapper, processor adapter, etc. in the configuration file so that Web.xml can know how to request processing: Requests--->web.xml--->servlet configuration--->dispatchereservlet--- >
Handlermapper--->handleradapter--->handler (Controller Handler Two is one thing) 5,6,7 is the processing chain). Parameter name: contextconfiglocation
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value> classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</ Load-on-startup>
    </servlet>
    All requests are blocked to Dispatcherservlet, if there are static js.html and so on need to use restful for processing.
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</ Url-pattern>
    </servlet-mapping>

Configure Spirngmvc-servlet.xml:

<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.2.xsd Http://www.springframework.org/schema/mvc http:// Www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http:// Www.springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http:// Www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http:// Www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--configuration Handler,handler implements the Controller interface--> <!-- For handler of annotations (ConTroller) A single configuration is recommended for actual development using component scans 
 Controller, service here lets scan controller, specify controller packets to the container to manage controller 
	 --> <context:component-scan base-package= "Cn.itcast.ssm.controller" ></context:component-sca> <! --Annotation Mapper--> <bean class= "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/> <!--annotation adapter--> <bean class=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/> <!--using MVC: Annotation-driven instead of the top note mapper and the annotation adapter configuration Mvc:annotation-driven Load Many parameter binding methods by default, such as the JSON transformation parser is loaded by default, if MVC is used: Annotation-driven do not configure the top requestmappinghandlermapping and requestmappinghandleradapter to use Mvc:annotation-driven when actually developing- -> <!--<mvc:annotation-driven></mvc:annotation-driven>--> <!--view parser here Configure the path where the prefix is JSP,VM, the suffix Configured to JSP,VM, so it is only necessary to return the name of the file in controller, the view parser will find the name of the file to render--> <bean class= "Org.springframewo Rk.web.servlet.view.InternalResourceViewResolver "> <!--Configure the prefix of the JSP path--> <property name=" prefix "value="/ web-inf/jsp/"/> <!--Configure the JSP path afterPrefix--> <property name= "suffix" value= ". jsp"/> </bean> </beans> 


That is, in the actual development, in the Springmvc-servlet.xml only need to write:

Processor adapter and processor mapper configured

<mvc:annotation-driven></mvc:annotation-driven>

Scans the controller that add annotations, and the container creates controller objects.

<context:component-scan base-package= "Cn.itcast.ssm.controller" ></context:component-scan>

Just OK.

In Controller you need to add a note @controller (identifying the container to manage the creation of the object) before the class and add @requestmapping ("/requesturl") before the method to process the URL request, so A controller can have more than one annotation requestmapping () method to handle multiple requests.


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.