一、DispatcherServlet请求的分发器
所有的请求首先会到DispatcherServlet中,由DispatcherServlet分发给Spring MVC的Controller,Controller负责处理请求。所以DispatcherServlet需要知道有多少个控制器,具体的请求又应该给哪一个控制器。
二、配置DisptcherServlet
1.1使用硬编码的方式,继承AbstractAnnotationConfigDispatcherServletInitializer
改方案是借助于Servlet3.0规范的,所以需要服务器支持Servlet3.0.
1.2.继承类Abstract...的任意类都会自动的配置servlet和spring应用的上下文,注意这里需要支持javax.servlet-api,如果没有需要通过maven下载
1.3
1.getServletMappings表示它会将一个或者多个路径映射到DispatcherServlet中,“/”表示它会分发所有的请求
2.getServletConfigClasses方法要求DispatcherServlet创建的时候加载配置文件中web组件的bean,控制器,视图解析器,以及处理映射。所以这里指定一个配置类
3.getRootConfigClasses对应ContextLoaderListener解析的bean
1.4 配置WebConfig
/**
* web的一些配置
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.crazyorange.spring.test.*"})
public class WebConfig extends WebMvcConfigurerAdapter {
//模型解析,在请求时为模型视图名称添加前后缀 比如在controller类中需要请求/WEB-INF/page/index.jsp文件,直接写index就可以了
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-inf/pages/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// super.configureDefaultServletHandling(configurer);
configurer.enable();
}
}
**1.5 配置RootConfig **
@Configuration
@ComponentScan(basePackages = {"com.crazyorange.spring.test"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class RootConfig {
}
2.0使用web.xml的方式配置
2.1 将下面的信息加入到web.xml中
<!-- spring框架必须定义ContextLoaderListener,在启动Web容器时,自动装配Spring applicationContext.xml的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定SpringMVC配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- 指定请求的后缀,可以随意写,这里用.html作为请求后缀 -->
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
2.2 在Spring的xml中配置视图解析器
<!-- 模型解析,在请求时为模型视图名称添加前后缀 比如在controller类中需要请求/WEB-INF/page/index.jsp文件,直接写index就可以了 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value="*.jsp" />
</bean>
三、配置请求的方法
3.1 RequestMapping 的value声明访问的方法名,method声明请求的方式,Controller告诉Spring容器这是一个控制器,用来处理请求
四、使用单元测试
**4.1 需要有Junit和Spring-Test Library **
standaloneSetup方法创建一个测MockMvc接管controller
perform指定调用的请求,andExpect设定预期的结果。如果没有达到预期的结果,会报错。**
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
五、使用@RequestMapping映射一个请求
使用@RequestMapping注解标记一个方法,value属性映射了它要处理的请求,method表示请求类型,同时它还支持数组类型的参数,表示映射多个方法
**该方法使用一个model模型作为参数,这样数据就会填充到模型传递给视图层。当我们不指定key的时候,它会自动根据参数的类型生成一个key,比如这个例子UserRepository返回一个List<User>那么就会生成一个userList的key。
使用单元测试
andExpect是期望得到的结果。
参数的传入
1.使用@RequestParam表示参数,它的defaultValue表示默认值
2.使用@PathVariable获取路径中的参数,Spring支持从路径中截取参数,首先需要告诉spring路径中的那一段代表参数,这里就需要在@RequestMapping中使用占位符,占位符用{}括起来。路径中该位置的字段就会被当做参数