SpirngMVC:基于java实现的MVC模型的轻量级Web框架
一、创建控制器类
1.将它交给spring管理
@Controller
2.定义当前请求的具体方法
3.设置当前方法的访问路径
@RequestMapping("/save")
4.设置当前返回类型为String
@ReponseBody
public String save(){
.....
return "springmvc"
}
二、初始化SpringMVC环境
SpringMvcConfig
配置类
@Configuration
@ComponentScan("com.shhssh")
三、创建Tomcat的Servelet容器配置类
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
//创建SpringMvc容器
protected WebApplicationContext createServletApplicationContext() {
// ApplicationContext ctx = new AnnotationConfigApplicationContext();
//加载配置文件,创建SpringMvc容器
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置哪些请求交给SpringMvc处理
protected String[] getServletMappings() {
return new String[]{"/"};//所有的请求
}
//创建Spring容器
protected WebApplicationContext createRootApplicationContext() {
return null;
}
请求与响应
一、请求路径与方式
@RequestMapping()在类中加入路径 方法中加入请求路径
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'module':'user save'}";
}
}
二.参数-常规类型+日期类型+文件类型
普通类型
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(@RequestParam("name") String username, Integer age){
//@RequestParam("name") 绑定前端传入的参数
System.out.println("username:"+username);
System.out.println("age:"+age);
return "commonParam";
}
POJOl类型
@RequestMapping("/pojoParam")
@ResponseBody
public String pojoParam(User user){
System.out.println(user);
return "pojoParam";
}
嵌套POJO类型
private String name; private Integer age; private Address address;
public class Address { private String province; private String city;
}
@RequestMapping("/pojoContainPojoParam")
@ResponseBody
public String pojoContainPojoParam(User user){
System.out.println(user);
return "pojoContainPojoParam";
}
数组类型
@RequestMapping("/arrayParam")
@ResponseBody
public String arrayParam(String[] likes){
System.out.println(Arrays.toString(likes));
return "arrayParam";
}
List集合类型
//@RequestParam 告诉前端的数据直接放入likes中而不是实例化
@RequestMapping("listParam")
@ResponseBody
public String listParam(@RequestParam List<String> likes){
//@RequestParam 告诉前端的数据直接放入likes中而不是实例化
System.out.println(likes);
return "listParam";
}
Map类型
@RequestMapping("mapParam")
@ResponseBody
public String mapParam(@RequestParam Map<String,String> maps){
System.out.println(maps);
return "mapParam";
}
}
日期类型
默认方式2024/3/21
在SprinMvcConfig配置类前
@EnableWebMvc
开启SpingMVC的辅助功能
@DataTimeFormat(pattern = ''yyyy-MM-dd'')
@Controller
public class DateController {
@RequestMapping("/dateParam")
@ResponseBody
public String dateParam(Date date,@DateTimeFormat(pattern = "yyyy-MM-dd") Date date2){
System.out.println(date);
System.out.println(date2);
return "dateParam";
}
}
文件类型参数
前端提供文件上传的功能,需要书写文件上传接口
1、引入依赖包:
<!--添加fileupload依赖-->
<dependency>
<groupId>commons-fileupload</ groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
2、配置multipartResolver解析器
在SpringMvcConfig配置类中书写
@Bean( "multipartResolver")
public commonsMultipartResolver multipartResolver (){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding( "UTF-8"); //设置编码
resolver.setMaxUploadsize(1024*1024); //限制最大文件大小
return resolver;
}
3.书写文件类型控制器
@Controller
public class FileController {
@RequestMapping("fileParam")
@ResponseBody
public String fileParam(MultipartFile file) throws IOException {
if(!file.isEmpty()){
//自定义文件
File file1 = new File("D://test.txt");
file.transferTo(file1);
}
return "fileParam";
}
}
Json类型(重点)
先导包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
在传参前面加入@RequestBody 表示请求
@Controller
public class JsonController {
@RequestMapping("/pojoParamForJson")
@ResponseBody
public String pojoParamForJson(@RequestBody User user){
System.out.println(user);
return "pojoParamForJson";
}
@RequestMapping("/arrayParamForJson")
@ResponseBody
public String arrayParamForJson(@RequestBody String[] list){
System.out.println(Arrays.toString(list));
return "arrayParamForJson";
}
@RequestMapping("/pojoArrayParamForJson")
@ResponseBody
public String pojoArrayParamForJson(@RequestBody List<User> list){
System.out.println(list);
return "pojoArrayParamForJson";
}
}
中文乱码问题
在pom.xml tmocat依赖中加入
<uriEncoding>UTF-8</uriEncoding>
post 需要再加一个配置
zaiServerletContainerConfig配置类中书写处理编码deFilters
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
return new Filter[]{characterEncodingFilter};
}
响应处理结果
ResponseController 返回数据测试控制器
加入ReponseBody就将会直接将page.jsp当字符串返回前端
@Controller
public class ResponseController {
//返回页面
@RequestMapping("/toPage")
public String toPage(){
System.out.println("toPage");
return "pages.jsp";
}
//返回文本
@RequestMapping("/toText")
@ResponseBody
public String toText(){
System.out.println("toText");
return "pages.jsp";
}
//返回JSON数据
@RequestMapping("/toJson")
@ResponseBody
public User toJson(){
User user = new User();
user.setName("itcast");
user.setAge(13);
Address address = new Address();
address.setProvince("shanghai");
address.setCity("shanghai");
user.setAddress(address);
return user;
}
@RequestMapping("/toJsonArray")
@ResponseBody
public List<User> toJsonArray(){
List<User> list= new ArrayList<User>();
User user = new User();
user.setName("itcast");
user.setAge(13);
Address address = new Address();
address.setProvince("shanghai");
address.setCity("shanghai");
user.setAddress(address);
User user2 = new User();
user2.setName("itcast");
user2.setAge(13);
Address address2 = new Address();
address2.setProvince("shanghai");
address2.setCity("shanghai");
user2.setAddress(address2);
list.add(user);
list.add(user2);
return list;
}
}
类型转换器:根据类型匹配的类型转换器
RESTful风格
软件构架风格 隐藏资源访问行为 使用行为动作区分对资源进行了那些操作
-
http://localhost/users
查询全部用户信息 GET(查询) -
http://localhost/users/1
查询指定用户信息 GET(查询) -
http://localhost/users
添加用户信息 POST(新增/保存 -
http://localhost/users
修改用户信息 PUT(修改/更新) -
http://localhost/users/1
删除用户信息 DELETE(删除)