简介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
@NoArgsConstructor
public 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<String> citys){
citys.forEach(System.out::println);
return "success.html";
}
}
后端向前端传送数据:
@Controller
@RequestMapping(value = "/my1")
public class MyController {
@GetMapping(value = "/list1")
//@ModelAttribute("citys")
public List<String> citys1(){//契约式编程
List<String> 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<User> 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<String,User> citys4(){//契约式编程
Map<String,User> 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";
}
}
后端传给前端时返回字符串或数组或集合采用契约式编程