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 .
- stay spring Generally used in @RequestMapping Annotation to complete the mapping relationship
-
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、 To write hello.jsp page
Put it in /WEB-INF/jsp/ Under the path
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Zuka</title>
</head>
<body>
${msg}
</body>
</html>
5、 test
url:http://localhost:8080/springmvc_annotation_war_exploded/controller/hello