准备操作
web.xml
<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_3_1.xsd"
version="3.1">
<!--请求处理转交spring-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--post请求乱码解决-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
DispatcherServlet-servlet.xml
<?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"
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 ">
<!--扫描基础包-->
<context:component-scan base-package="controller"/>
<!--处理器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<!--配置json转换器-json支持-->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</property>
</bean>
<!--解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
知识点概况
@Controller
- 位置:声明类上
- 作用:标注当前类为ioc容器中的bean组件
- 子注解: @Component @Repository @Service(作用同父注解一样,可读性更高)
- 例子:
@Scope
- 位置:声明类上
- 作用:标注当前bean的声明周期
- 属性:
1.singleton(默认值)
全局有且仅有一个实例
2.prototype
每次获取Bean的时候会有一个新的实例
3.request
每次请求的时候会有一个新的实例
4.session
每一个会话都会有一个新的实例
-
例子:
@RequestMapping() -
位置:
-类上:定义访问此控制器(类)的根路径
-方法上:定义访问此方法的路径 -
属性:
1.value(常用):定义访问路径
2.method(常用):定义访问方式如:get、post -
例子:
那么,访问demo这个方法的url就为http://localhost:8080/user/demo.do,并且只支持get方式提交。
@RequestParam() -
位置:方法定义上的参数的前面
-
作用:对于参数进行更为详细的描述与控制
-
属性:
value:为参数的名字
required:默认为true,意思是请求必须有这个参数否则报错
defaultValue:默认值,如果请求没有为这个参数赋值则赋为默认值 -
例子:
@PathVariable -
说明:url模版映射可以restfull软件架构
-
例子:
@ResponseBody -
位置:方法定义上的返回值前面
-
作用:把后台pojo转换json对象,返回到页面。
-
例子:
-
@RequestBody
- 位置:在方法定义上的需要转换的参数的前面
- 作用:接受前台json数据,把json数据自动封装javaBean
- 例子:
坑点注意:
进行json操作时要加入jackson依赖(springMvc的json转换器中需要)
jackson依赖
springMvc json转换器类中的导包信息