欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Spring-MVC

时间:2023-06-17
Spring-MVC

简介Spring-MVCurl的路径映射数据绑定

@requestparam@pathvariableMap,ModelMap,Model 代码呈现 简介Spring-MVC

Spring Web MVC的前端控制器是DispatcherServlet。
DispatcherServlet的任务是将请求发送给SpringMVC控制器controller

具体工作过程见下两图:

url的路径映射

普通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 citys){ citys.forEach(System.out::println); return "success.html"; }}

后端向前端传送数据:

@Controller@RequestMapping(value = "/my1")public class MyController { @GetMapping(value = "/list1") //@ModelAttribute("citys") public List citys1(){//契约式编程 List list=new ArrayList(){ { add("上海"); add("哈尔滨"); add("北京"); } }; return list; } @GetMapping(value = "/array") public String[] citys2(){//契约式编程 String[] ss=new String[]{"北京","上海","哈尔滨"}; return ss; } @GetMapping(value = "/list2") //@ModelAttribute("users") public List citys3(){//契约式编程 return Arrays.asList( new User("999",1,"weikun1",9), new User("888",2,"weikun6",99), new User("777",3,"weikun8",999) ); } @GetMapping(value = "/list3") @ModelAttribute("users") public Map citys4(){//契约式编程 Map map=new HashMap(); map.put("A",new User("1",1,"weikun",999)); map.put("B",new User("2",2,"weikun7777",998)); return map; } @GetMapping(value = "/cookie") public String cookie( @cookievalue(value = "hit", defaultValue = "0")Long counter, HttpServletResponse response) {//契约式编程 ++counter; cookie cookie=new cookie("hit",counter.toString()); response.addcookie(cookie); return "cookie"; }}


后端传给前端时返回字符串或数组或集合采用契约式编程

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。