简介Spring-MVCurl的路径映射数据绑定
@requestparam@pathvariableMap,ModelMap,Model 代码呈现 简介Spring-MVC
Spring Web MVC的前端控制器是DispatcherServlet。
DispatcherServlet的任务是将请求发送给SpringMVC控制器controller
具体工作过程见下两图:
普通url路径映射
模板模式映射
ANT风格
正则风格
最重要的两个注解@requestparam和@pathvariable
@requestparam 在 https:url ?键值对 情况下使用
@RequestParam用于获取参数,可获取?username="sss"这种?后面的参数值
如:访问路径为:http://localhost:8080/pay/paySerial?pay=2019
@pathvariable 在 https:url /参数 情况下使用
@PathVariable接收参数,参数值需要在url进行占位, 前端传参的URL:url = “/main/mm/am/ I d / {Id}/ Id/{name}”
如://localhost:8080/pay/paySerial/2019
Map,ModelMap,Model 虽然是不同的三种类型,但是是同一个对象
Web初始化阶段:
WebInit:
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[0];//非web部分的配置文件区域 } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfiger.class};//web的配置文件区域 含这些配置文件 } @Override protected String[] getServletMappings() { return new String[]{"/"};//从域名的根部分进行截取URL地址,进行地址解析 }}
WebConfiger:
@Configuration//声明此类是个配置器@EnableWebMvc//允许使用MVC@ComponentScan("com.weikun")//声明作用区域public class WebConfiger implements WebMvcConfigurer { @Bean public ViewResolver viewResolver(){//视图解析器 完成对视图的前缀和后缀部分 InternalResourceViewResolver viewResolver=new InternalResourceViewResolver(); //视图的前缀---路径部分 viewResolver.setPrefix("/WEB-INF/views/"); //视图的后缀---视图的文件的属性 viewResolver.setSuffix(".jsp"); return viewResolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable();//允许 html js css部分通过tomcat }}
model层:
@Data//采用lambok的方式@AllArgsConstructor@NoArgsConstructorpublic class User implements Serializable { private String password; private int age; private String username; private float salary;}
controller层:
前端向后端传送数据:
@Controller@RequestMapping(value = {"/hello"})public class HelloController { @RequestMapping(value = {"/ok"},method = {RequestMethod.POST,RequestMethod.GET}) public String ok(){ System.out.println("处理部分!"); return "success.html"; } @RequestMapping(value = {"/ver"}) public String verify(@RequestParam(value = "username")String name,@RequestParam(value = "password")String pwd){ System.out.println(name+"-->"+pwd); return "success.html"; } @RequestMapping(value = {"/login/{username}/{password}"}) public String login(@PathVariable(value = "username") String name,@PathVariable(value = "password") String pwd){ System.out.println(name+"-->"+pwd); return "success.html"; } @RequestMapping(value = {"/product*{id}"}) public String product(@PathVariable(value = "id") String id){ System.out.println(id); return "success.html"; } @RequestMapping(value = {"/product/{low:\d+}-{high:\d+}"}) public String price(@PathVariable(value = "low") double low,@PathVariable(value = "high") double high){ System.out.println(low+"..."+high); return "success.html"; } @RequestMapping(value = {"/ver1"}) public String verify(User user){ System.out.println(user.getUsername()+"-->"+user.getPassword()); return "success.html"; } @RequestMapping(value = "/ok") public String ok(Model model, Map model2, ModelMap model3, User user){ model.addAttribute("username",user.getUsername()); model2.put("password",user.getPassword()); model3.addAttribute("pwd",user.getPassword()); return "ok.jsp"; } @GetMapping(value = "/list") public String citys(@RequestParam(value = "city") List
后端向前端传送数据:
@Controller@RequestMapping(value = "/my1")public class MyController { @GetMapping(value = "/list1") //@ModelAttribute("citys") public List
后端传给前端时返回字符串或数组或集合采用契约式编程