1.@RequestParam
@RequestMapping("/menuQuery")
public Object meanQuery(@RequestParam Integer id){
......
......省略代码
}
这里写入的参数就会从前端传入到后端代码中
如果不强制要求传入参数的话,需要做如下设置
required该参数是否必须。默认为true;
defaultValue请求参数的默认值。
@RequestMapping("/menuQuery")
public Object meanQuery(@RequestParam(required = false) Integer id){
......
......省略代码
}
2.@RequestMapping
@RequestMapping("/menuQuery")
public Object meanQuery(){
......
......省略代码
}
通过@RequestMapping注解可以实现前端通过访问注解定义url地址,可以访问到后端注解下的代码块.
3. @Autowired
public class MenuService {
@Autowired
MenuMapper menumapper;
...........
..........省略代码
}
通过@Autowired注解实现的其实就是menumapper = new MenuMapper,把一个设置好的类实例化到当前类内部.
4.HttpServletRequest
HttpServletRequest与@RequestParam实现目的接收前端发送过来的数据,只不过HttpServletRequest接收的是一个封装好的对象.
HttpServletRequest接口最常用的方法就是获得请求中的参数,这些参数一般是客户端表单中的数据。同时,HttpServletRequest接口可以获取由客户端传送的名称,也可以获取产生请求并且接收请求的服务器端主机名及IP地址,还可以获取客户端正在使用的通信协议等信息。下表是接口HttpServletRequest的常用方法。
5.HttpServletResponse
在Servlet中,当服务器响应客户端的一个请求时,就要用到HttpServletResponse接口。设置响应的类型可以使用setContentType()方法。发送字符数据,可以使用getWriter()返回一个对象。
下表是接口HttpServletResponse的常用方法。
5.1sendRedircet(String location)
sendRedircet(放入一个模板页面) 他可以实现页面的跳转,运行到sendRedircet时,就会跳转到括号内传入的模板页面
6.@RequestBody
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);@RequestBody直接以String接收前端传过来的json数据.
GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
注:一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam。
7.@RequestParam
@RequestParam注解定义在方法传参内,实现的是前端网页传入参数后发生到后端
http://localhost/account/list?pageNum=1
问号,后面的pageNum=1就是前端写入的参数,会通过@RequestParam注解,传递到后端,然后在后端使用pageNum这个变量,进行相关的操作(一般是使用场景就是翻页的实现)
@RequestMapping("/list")
public String list(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5" ) int pageSize,Model model) {
PageInfo<Account>page = accountSrv.findByPage(pageNum,pageSize);
model.addAttribute("page", page);
return "/account/list";
}
- required:请求参数中是否必须提供此参数,默认值是true,true为必须提供
- defaultValue:默认值
8.@ConfigurationProperties
8.1使用场景:
将大量的参数配置在 application.properties或application.yml文件中,通过 @ConfigurationProperties 注解,我们可以方便的获取这些参数值
8.2注意:
使用时要通过添加 @Component 或者@Configuration注解让 Component Scan 扫描到
9.@Configuration与@Bean的共同使用
@Configuration可理解为用spring的时候xml里面的标签
@Bean可理解为用spring的时候xml里面的标签
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
使用@Bean 注解表明MyServiceImpl需要交给Spring进行管理,做一个单例模式,这样我们所以的线程都是共享同一个MyServiceImpl,不会出现线程安全问题
10Model
Model是放在方法的参数列表里面,使用model.addAttribute(“供前端调用的名称”,数据集)可以实现:从后端mysql查询数据传递给前端页面使用
@RequestMapping("/list")
public String list(Model model){
List<Account>accountList = accountSrv.findAll();//去server层获取数据库内容
model.addAttribute("accountList",accountList);//通过model把从数据库获取信息传递到前端
return "account/list";
}
11.PageHelper:分页的实现
11.1controller层示例代码
@RequestMapping("/list")
public String list(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5" ) int pageSize,Model model) {
PageInfo<Account>page = accountSrv.findByPage(pageNum,pageSize);
//我们拿到server层返回过来的封装好的PageInfo类型数据集,我们通过model发送给前端页面,前端可以通过page来调用
model.addAttribute("page", page);
return "/account/list";
}
11.2service层的示例代码
public PageInfo<Account> findByPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize); //页码,和每页显示数量
AccountExample example = new AccountExample();
List<Account> list = accMapper.selectByExample(example);
//通过传入的数据集list,和准备分几页,计算一页多少数据,做一个PageInfo的数据集返回出去
return new PageInfo<>(list, 5);
}
12.MultipartFile用户文件上传
MultipartFile是写在方法传入参数位置,通过MultipartFile实现用户上传文件,后端接收
//用户上传文件存储的路径应该避免中文字符,否则会报错
@RequestMapping("/fileUploadController")
public String fileUpload (MultipartFile filename,String password) { //这里要注意必须叫filename,要不然后面找不到MultipartFile
System.out.println("password:" + password);
System.out.println("file:" + filename.getOriginalFilename());
try {
File path = new File(ResourceUtils.getURL("classpath:").getPath());
File upload = new File(path.getAbsolutePath(), "static/upload/");
System.out.println("upload:" + upload);
filename.transferTo(new File(upload+"/"+filename.getOriginalFilename()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "profile";
}
上述代码就是实现的一个文件上传接口.一般文件上传都按照此类进行书写.
13.@Controller和@RestController
13.@Controller
将方法类注解为一个可供前端调用的接口
@Controller
public class HelloController{
@RequestMapping("/hello)
@RequestBody
public String hello(){
return "hello"
}
}
13.2@RestController
@RestController他是@RequestBody与@Controller整合版,有些人嫌上述麻烦,可以使用@RestController来简化代码
@RestController
public class HelloController{
@RequestMapping("/hello) //这里就少了@RequestBody,@RequestBody就被@RestController给整合了
public String hello(){
return "hello"
}
}