Java日记之SpringMVC基础

SpringMVC是spring框架族的一个子框架,对应的是web层的功能

一.核心组件

1.DispatcherServlet : 中央控制器,负责把请求分发到具体的控制类(注意不是控制器是控制类,概念比控制器大)
2.Controller : 具体处理请求的控制器,那么问题来了,它与handler的区别在哪呢? Controller是一个类 , handler是控制器的处理器方法
3.HanderMapping : 处理器映射器,负责映射中央控制器转发给controller时映射的策略(或方式),简单点讲就是解决如何映射的问题
4.ModelAndView : 设置返回的数据和视图,也就是跳转页面(相当于请求转发和setAttribute())
5.ViewResolver : 视图解析器,渲染视图的作用
6.HandlerAdapter : 处理器适配器,去寻找合适的处理器在这里插入图片描述
(白嫖一张图)

二.搭建框架

1.拷jar包,约束并在web.xml下配置中央控制器,相关概念在之前的servlet的博客中有提过,传送门
其中,<init-param></~>是初始化时传入的参数,<param-name></~>是key,<param-value>是值 ,<servlet-class></~>的值要在web.servlet包下找到dispatcherservlet.class , 右键复制路径后去掉.class后缀
在这里插入图片描述
为什么我找不到这个包!? 解决方案:点这里看看你的包展示方式是不是改了
在这里插入图片描述

<servlet>
	  	<servlet-name>springmvc</servlet-name>
	  	<servlet-class>org.springframework.web.servlet
	  	.DispatcherServlet</servlet-class>
	 	<init-param>
	 		<param-name>contextConfigLocation</param-name>
	 		<param-value>classpath:springmvc.xml</param-value>
	 	</init-param>
	 	<load-on-startup>1</load-on-startup>
	  </servlet>
	  
	  <servlet-mapping>
	  	<servlet-name>springmvc</servlet-name>
	  	<url-pattern>*.action</url-pattern>
	  </servlet-mapping>
约束
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">

这里的url-pattern要说一下,后缀 .xx是任意命名的,代表所有以xx为后缀的请求都会被spring中央控制器分发. 但是不要取js,jsp,class,java这种后缀,浏览器无法区分是前端页面还是其它
2.创建springmvc的配置文件 : 配置处理器映射器,处理器配置器,视图解析器 ,前二者可以通过标签自动注册<mvc:annotation-driven></mvc:annotation-driven> 不要忘了加组件扫描标签哦
3.实现跳转页面 : @RequestMapping(方法.后缀)标签,setViewName()方法可以实现在访问页面时跳转

@Controller
public class GoodController {
	@RequestMapping("delete.action")
	public ModelAndView delete() {  //这样才能跳转页面
		System.out.println("删除商品");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("test.jsp");
		return mav;
	}
}

仅仅用一个return也可以实现页面跳转

@RequestMapping("update.action")
	public String update() {
		System.out.println("update..."); 
		//这样就不需要ModelAndView了 
		return "test.jsp";
	}

想要更加灵活(偷懒),当然也可以不加后缀,不过需要配置一个视图解析器
在这里插入图片描述

	<bean class="org.springframework.web.servlet.view.
		InternalResourceViewResolver">
			<property name="prefix" value=""></property>
			<!-- 前缀  -->
			<property name="suffix" value=".jsp"></property>
	</bean>
	@RequestMapping("update.action")
	public String update() {
		System.out.println("update...");
		return "test";
	}

三.如何在springMVC中使用Ajax

使用Ajax的时候,我们不希望页面发生跳转
前端的代码跟web阶段一样,后端需要直接返回数据结构json
思路 : 将方法返回值设为List<HashMap<String,String>>
打注解@RequestMapping(方法.后缀)@Response
在方法中定义list和hashmap,向hashmap传值,并将hashmap打入list中
最后返回list

@RequestMapping("select.action")
	@ResponseBody
	public List<HashMap<String,String>> select(){
		List<HashMap<String,String>> list = 
		new ArrayList<HashMap<String,String>>();
		HashMap<String, String> hm = new HashMap<String, String>();
		
		hm.put("name", "zs");
		hm.put("age", "1");
		list.add(hm);
		
		return list;
	}

最后,因为我的jar包可能有问题,使用非注解方式服务器根本打不开,就直接跳过了非注解配置,直接用自动配置 …

猜你喜欢

转载自blog.csdn.net/qq_45596525/article/details/107948854