Spring2.5 in the future , Development with annotation SpringMVC It's very powerful , So are annotations SpringMVC The essence of . In actual development , Will use annotations to achieve .

This makes SpringMVC Minimize development effort , Developers only need to focus on the business logic and the implementation of the page .

1、web.xml file

To configure DispatcherServlet, And the corresponding servlet-mapping.

This file is in addition to servlet-name, and springmvc Profile name for , The rest is fixed , Reusable .

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- register DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Associated with a springmvc Configuration file for , The naming rule is :【servlet-name】-servlet.xml-->
<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> <!--/ Match all requests : barring .jsp-->
<!--/* Match all requests : Include .jsp-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

2、Spring bean The configuration file

Follow the official nomenclature , This is used here springmvc-servlet.xml. This file is in addition to the package path to scan , The rest is fixed , It can be reused directly .

  • Open the annotation

    • newly added context constraint
    • Turn on auto scan package
  • Static resource filtering

    • Use default default-servlet-handler
    • Give Way Spring MVC Don't deal with static resources .css .js .html .mp3 .mp4
  • Support mvc Annotation driven

    • stay spring Generally used in @RequestMapping Annotation to complete the mapping relationship

      To make @RequestMapping Annotations to take effect

      You must register with the context DefaultAnnotationHandlerMapping

      And a AnnotationMethodHandlerAdapter example

      These two instances are handled at the class level and the method level, respectively .

      and annotation-driven Configuration helps us to automatically inject the above two instances .
  • view resolver

    • By default InternalResourceViewResolver
    • To configure .jsp The prefix of the full path file name 、 suffix .
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Auto scan package , Make the annotation under the specified package effective , from IOC Container management -->
<context:component-scan base-package="controller"/> <!-- Give Way Spring MVC Don't deal with static resources .css .js .html .mp3 .mp4-->
<mvc:default-servlet-handler/> <!-- Support mvc Annotation driven
stay spring Generally used in @RequestMapping Annotation to complete the mapping relationship
To make @RequestMapping Annotations to take effect
You must register with the context DefaultAnnotationHandlerMapping
And a AnnotationMethodHandlerAdapter example
These two instances are handled at the class level and the method level, respectively .
and annotation-driven Configuration helps us to automatically inject the above two instances .-->
<mvc:annotation-driven/> <!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- Prefix -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- suffix -->
<property name="suffix" value=".jsp"/>
</bean> </beans>

3、Controller Class writing

Develop... Using annotations SpringMVC,99% The amount of work we do is controller Class writing .SpringMVC Annotation Minimize development .

Controller Responsible for resolving user requests , Conduct business processing , And return a model .

Controller Through the implementation of interface and annotation definition two methods to achieve .

Realized Controller The class of the interface is the controller , This is the older way .

A controller class can only have one method . Multiple methods need to write multiple controller.

It is recommended to define the implementation with annotations .

  • @Controller

    • decorator , Ensure that the class can be Spring Automatically scan to , Automatic assembly for Spring bean
  • @RequestMapping

    Request address , take controller Class or its specific methods map to the specified front-end view

    • decorator , Represents the response path of all methods of this class, with this address as the parent path .
    • Modification methods : Represents the response path of the method , Method to respond to the specified front end view .
  • return

    • Return value : View jsp file name

    • @Controller In annotated classes , If the method returns the value corresponding to jsp The file name exists , Will be parsed by the view parser

@Controller // decorator , Ensure that the class can be Spring Automatically scan to , Automatic assembly for Spring bean
@RequestMapping("controller") // decorator , Represents the response path of all methods of this class, with this address as the parent path
public class MyController { // complete url:http://localhost:8080/( project name )/controller/hello
@RequestMapping("/hello") // Modification methods : Represents the response path of the method , Method to respond to the specified front end view .
public String sayHello(Model model){
model.addAttribute("msg", "Hello!SpringMVC Annotation!");
// Return value : View jsp file name
//@Controller In annotated classes , If the method returns the value corresponding to jsp The file name exists , Will be parsed by the view parser
return "hello";
}
}

4、 test

url:http://localhost:8080/springmvc_annotation_war_exploded/controller/hello

Development with annotation SpringMVC More articles about

  1. SpringMVC Frame learning notes (2)—— Develop... Using annotations SpringMVC

    1. To configure web.xml <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.sp ...

  2. Develop... Using annotations springmvc

    1. Import jar package commons-logging-1.2.jar spring-aop-4.3.6.RELEASE.jar spring-beans-4.3.6.RELEASE.jar spring- ...

  3. 02 Develop... Based on annotations SpringMVC project (jar package , asynchronous ,request, Parameter passing , Multiple choice reception ,Model The ginseng ,map The ginseng ,model The ginseng ,ajax, Redirect , Time date conversion )

     1  what is needed jar package The project structure is as follows : 2 web.xml The contents of the configuration file are as follows : <?xmlversion="1.0"encoding="UTF-8"?&g ...

  4. SpringMVC Second articles 【 Filter encoder 、 Annotation development 、requestMapping、 Business methods and traditional parameters 】

    SpringMVC Filter encoder stay SpringMVC In the controller , If nothing is done with the encoding , Then the Chinese data is garbled ! Even if we are handle() In the method , Use request Object set encoding doesn't work either ! The reason is also ...

  5. SpringMVC Learn to write with annotations SpringMVC Program

    SpringMVC Introduce Spring Of web Frame around DispatcherServlet Design .DispatcherServlet The role of is to distribute requests to different processors . from Spring 2.5 Start , Use Java ...

  6. SpringMVC Annotation development preliminary

    One .( Add ) view resolver ---XmlViewResolver effect : Separate configuration information . In the view parser ---BeanNameViewResolver On the basis of , Create a new one myView.xml Separate information stay ...

  7. SpringMVC Introduction to annotation development of

    1.Spring MVC Introduction to the framework Support REST Style URL Add more comments , Fully annotation driven introduce HTTP I / O Converter (HttpMessageConverter) And data conversion . format . Seamless integration of verification framework ...

  8. Spring+SpringMVC+MyBatis In depth learning and building ( sixteen )——SpringMVC Annotation development ( Advanced )

    Reprint please indicate the source :http://www.cnblogs.com/Joanna-Yan/p/7085268.html As mentioned earlier :Spring+SpringMVC+MyBatis In depth learning and building ( 15、 ... and )——S ...

  9. springmvc Learning notes (13)-springmvc Set type parameter binding for annotation development

    springmvc Learning notes (13)-springmvc Set type parameter binding for annotation development label : springmvc springmvc Learning notes 13-springmvc Set type parameter binding for annotation development Array binding Need to be ...

  10. Using pure annotation and configuration class development springMVC project , Get rid of xml To configure

    Recently I read the book of Mr. Yang Kaizhen , Explain profound theories in simple language springBoot2.x, Mining a lot of previously ignored knowledge , Developed for more than a year , I always use tradition in my work springmvc Development of , It's basically traditional xml Configuration development , It's mentioned in the book , ...

Random recommendation

  1. 【 English 】Bingo Oral notes (68) - come series

  2. use JSON Data adds data rows to a table with defined columns

    In fact, the way of adding is different from MVC Dynamic read in JSON Data is the same as creating a table , It's just a complete table addition , One is to add... From the middle of the table . I won't elaborate . <div> <table class="table ...

  3. React History

    1. browerhistory 1. Recommended , 2. nginx The server needs to be configured try-file, When the route is not found, jump to the home page 3. Use a browser history object , Direct access is no longer allowed nn So you can't copy it directly ...

  4. linux in FTP Automatic backup VPS Script

    There are more servers , There are more and more websites , I always feel that I can't let go of the whole backup , And several times rm It's a mistake , Cause irreparable losses . And most of them VPS The provider does not provide automatic backup function, or the price of this function is slightly higher . So it's necessary for me to do this job ...

  5. Use CSS To achieve Yin and Yang diagrams and other graphics

    CSS It's more powerful , Can realize Chinese classical " Yin Yang eight diagrams " Equal shape . Square #rectangle { width: 200px; height: 100px; backgrount-c ...

  6. Bootstrap Excellent template -Unify.2.6.2

    This is a very old card Bootstrap Business template , Comprehensive and stable , Yes LandingPage.BussinessPage.AdminPage Multiple modes , It is highly recommended to build an official website . Responsive applications Web. The management end Web ...

  7. mybatis Of xml in sql In the sentence in Writing ( Iterate through )

    Use here foreach label <foreach  item="item" collection="listTag" index="index&q ...

  8. Pandas in Loc Usage Summary

    Excerpt from :http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html The specific use , Suppose the data source is : > ...

  9. linux perform python There was no response after the order , Don't print log information

    Actually python The soft connection to execute is the path is /opt/python2.7/bin/python, The soft connection error is as follows : Modify the connection , yes python Point to /opt/python2.7/bin/python, ...

  10. Qt Use net-snmp The process record of the package

    Use C/C++ Conduct SNMP Development , What is more popular on the Internet is to use net-snmp and snmp++ . stay sourceforge In order to Qt and snmp Search for keywords , Found items net-snm ...