SpringMVC基础
1.spring mvc概述
MVC : model + view + controller(数据模型+视图+控制器)
三层架构 : 展现层+应用层+数据访问层2.spring mvc项目快速搭建
简单的web项目搭建Spring MVC 常用的注解3.spring mvc常用注解
(1).@Controller
@Controller 注解在类上,表明这个类是spring mvc里的controller,将其声明为spring的一个bean,dispatcher servlet会自动扫描注解了此注解的类(用注解做拦截方式的例子原理类似),并将web请求映射到注解了@RequestMapping的方法上,这里特别指出,在声明普通的bean时,使用@Component、@Service、@Repository、@Controller是等同的,因为@Service、@Repository、@Controller都组合了@Component元注解;但在spring mvc声明控制器bean的时候,只能使用@Controller.
(2).@RequestMapping
@RequestMapping注解是用来映射web请求(访问路径和参数)、处理类和方法的。@RequestMapping可注解在类和方法上,注解在方法上的@RequestMapping路径会继承注解在类上的路径,@RequestMapping支持servlet和reponse作为参数,也支持对request和response的媒体类型进行配置。
(3).@ResponseBody
@ResponseBody 支持返回值放在response体内,而不是返回一个页面,我们在很多基于ajax请求的程序时,可以以此注解返回数据而不是页面;此注解可放置在返回值前或者方法上。
(4).@RequestBody
@RequestBody 允许request的参数在request体中,而不是在直接链接在地址后面。此注解放置在参数前。
(5).@PathVariable
@PathVariable 用来接收路径参数,如/news/001 可接收001 作为参数,此注解放置在参数前。
(6).@RestController
@RestController 是一个组合注解,组合了@Controller和@ResponseBody, 这就意味着当你只开发一个和页面交互数据的控制参数时,需要使用此注解。若没有 此注解,要实现上述功能,则需在代码中加@Controller和@ResponseBody两个注解。
代码演示:
ABCDE五个方面
@Controller //注解声明此类是一个控制器
@RequestMapping("/anno") //映射此类的访问路径是/anno
public class AnnoController {
A:接受httpServletRequest作为参数
@RequestMapping(produces="text/plain;charset=UTF-8")
//此方法未标注路径,则使用类路径/anno;produces可定制返回的response媒体类型和字符集,
//若返回值是json对象,则设置为 produces="application/json;charset=UTF-8".
public @ResponseBody String index(HttpServletRequest request){
//可接收httpServletRequest作为参数
return "url"+request.getRequestURL()+"can access";
}
B:接受路径参数,@PathVariable使用。
// 访问 http://localhost:8080/MySpringMvc/anno/pathvar/name
@RequestMapping(value="/pathvar/{str}",produces="text/plain;charset=UTF-8")
//接受路径参数,并在方法参数前结合@PathVariable使用,访问路径/anno/pathvar/xx
public @ResponseBody String login(@PathVariable String str,HttpServletRequest request){
return "str"+str;
}
C:接受路径请求参数
//访问 http://localhost:8080/MySpringMvc/anno/param?id=12
@RequestMapping(value="/param",produces="text/plain;charset=UTF-8")
//若返回值是json对象,则设置为 produces="application/json;charset=UTF-8".
public @ResponseBody String getParam(Long id,HttpServletRequest request){//可获取访问参数Id的值,可接收httpServletRequest作为参数
return "id="+id+request.getRequestURL();
}
D:接受路径请求参数到对象
请求对象javaBean示例
public class RequestBean {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//访问 http://localhost:8080/MySpringMvc/anno/obj?id=12&name="cc"
@RequestMapping(value="/obj",produces="text/plain;charset=UTF-8")
//若返回值是json对象,则设置为 produces="application/json;charset=UTF-8".
public @ResponseBody String test(RequestBean bean,HttpServletRequest request){
//可获取访问javaBean的值,可接收httpServletRequest作为参数
return "id="+bean.getId()+"name="+bean.getName()+request.getRequestURL();
}
E:接受不同路径进入相同方法
//演示不同路径进入相同的方法
// http://localhost:8080/MySpringMvc/anno/name1
// http://localhost:8080/MySpringMvc/anno/name2
@RequestMapping(value={"/name1","/name2"},produces="text/plain;charset=UTF-8")
//若返回值是json对象,则设置为 produces="application/json;charset=UTF-8".
public @ResponseBody String testValue(HttpServletRequest request){
return "url="+request.getRequestURL();
}
}
文章标题:SpringMVC基础
发布时间:2019-11-15, 14:38:17
最后更新:2019-11-15, 14:38:17