使用注解开发

1.注册一个DispatcnerServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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">
<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:SpringMVC-servlet.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>
</web-app>

特别的配置:

1
2
3
4
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC-servlet.xml</param-value>
</init-param>

初始化,就是配置文件的位置;

2.配置注解支持和数据处理,视图解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- more bean definitions go here -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.saxon.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver" >
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/JSP/"/>
</bean>
</beans>

mvc:annotation-driven:就是两个的注释

1
2
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

mvc:default-servlet-handler:是否过滤静态资源;这样的话,静态请求(css,html,js)就不会走这里;

context:component-scan扫描这个包下的所有的controller;

internalResourceViewResolve视图解析器,会根据你返回的值进行一个字符串的拼接;

3.编写一个controller使用注解@controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.saxon.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {
@RequestMapping ("/hello")//详相当于servletmappering
public String hello (Model model) {
model.addAttribute ("msg", "Hello Mr Mo");
return "Hello";
}

@RequestMapping ("/hello1")//详相当于servletmappering
public String hello1 (Model model) {
model.addAttribute ("msg", "Hello Mr Mo 2");
return "Hello";
}
}

@Controller:表名是一个controller可以被spring托管

@RequestMapping:访问的地址;