1、spring mvc 工作流程
用户发出 http 请求 --> 前端控制器(DispatcherServlet)把请求转发给后台控制器(Controller) --> 后台控制器把业务模型(model)再传给DispatcherServlet --> DispatcherServlet 对接收的model进行处理(图形化、XML、PDF、JSON等等) --> 处理完成之后,发送给用户。 整个流程算是告一段落,参考下图:
2、spring 访问静态文件
静态文件可以在 web.xml 中 <init-param></init-param> 中找到参数 <param-value></param-value> 文件进行配置即可,例:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:appServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<mvc:annotation-driven/>
<context:component-scan base-package="com.lykj.web.test" />
<mvc:resources location="/" mapping="/**"></mvc:resources>
</beans>
3、<context:component-scan base-package="com.lykj.web.test" use-default-filters="false"/>
将com.lykj.web.test 包下的所有类进行扫描,并个匹配的类注册为bean,如果不想把这个包下的所有类都注册为 bean,可以采用<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
base-package属性告诉spring要扫描的包
use-default-filters=”false”表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Responsitory,Controller注释修饰类。
4、<mvc:resources></mvc:resources>
访问静态资源这里配置就好,mvc框架可以帮助解决
5、@ResponseBody
它的作用是使 controller 直接返回数据,而不是一个 jsp 页面。
6、@RestController 与 @Controller
Rest Controller annotation is the same as Controller annotation, but assuming that @ResponseBody is active by default, so all the json are parsed to java objects.
7、解决 spring mvc 返回中文数据乱码
在 @RequestMapping() 添加 produces = {"text/json;charset=UTF-8"} 属性
8、什么是 REST
REST 是基于HTTP 处理 web 项目前后台交互的架构,它给后台的每个 service(或者叫resources)都定义了一个独一无二的 URL;
REST 是设计风格而不是标准,它通常基于使用 HTTP、URI、XML 及 HTML 这些标准;
链接协议具有无状态性;
可更高效利用缓存来提高响应速度;
9、@RequestMapping()
@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) :produces=MediaType.APPLICATION_JSON_VALUE 可将java bean 转换为 json 对象,默认也是将 java bean 转为 json 对象
@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE) : produces=MediaType.APPLICATION_XML_VALUE 可将 java bean 转为 xml 格式数据
详细可参考(转):http://json20080301.iteye.com/blog/1874074
@MatrixVariable 通过 get 的方式传递一个数组,用法:var;key=value1,value2,value3...
10、日期转换
get 方式传入:/convert/date/2010-07-04
@RequestMapping("date/{value}")
public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {
return "Converted date " + value;
}
显示效果:
Converted date Sun Jul 04 00:00:00 CST 2010
11、get 方式传入,以集合方式展示
/convert/collection?values=1&values=2&values=3&values=4&values=5
@RequestMapping("collection")
public @ResponseBody String collection(@RequestParam Collection<Integer> values) {
return "Converted collection " + values;
}
显示效果:
Converted collection [1,2,3,4,5]
12、@Valid
spring 验证机制感觉用起来效率很高,一般我们结合 java bean 来使用,代码如下:
public class JavaBean {
@NotNull
@Max(5)
private Integer number;
@NotNull
@Past
@DateTimeFormat(iso=ISO.DATE)
private Date date;
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
@RequestMapping("/validate")
public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) {
if (result.hasErrors()) {
return "Object has validation errors";
} else {
return "No errors";
}
}
前台输入:
/validate?number=3&date=2029-07-04
/validate?number=3&date=2010-07-01
得出结果:
在 JavaBean 中我们可以根据注解很容易理解:date 的时间要在过去的某个时间段,数字最大值为5
13、<jpa:repositories base-package="" />
配置了 jpa:repositories 之后, spring 初始化容器时将会扫描 base-package 指定的包目录及其子目录,从而产生一个代理对象,并将代理对象注册为 spring bean,业务层便可以用 类似@Autowired 的方式进行注入
14、@PathVariable Get 请求的时候我们通常使用此注解
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}
15、org.springframework.transaction.annotation.Transactional 与 javax.transaction.Transactional 区别
In a Java EE 7 application, you'll use the Java EE annotation.
In a Spring application, you'll use the Spring annotation.
16、spirng @Reposity、@Service、@Controllor 讲解
@Service告诉spring容器,这是一个Service类,标识持久层Bean组件,默认情况会自动加载它到spring容器中。
@Autowried注解告诉spring,这个字段需要自动注入
@Scope指定此spring bean的scope是单例
@Repository注解指定此类是一个容器类,是DA层类的实现。标识持久层Bean组件
@Componet:基本注解,标识一个受Spring管理的Bean组件
@Controller:标识表现层Bean组件
17、@Transactional