1.什么是MVC?
MVC就是一个分层架构模式:
M即model模型是指模型表示业务规则。在MVC的三个部件中,模型拥有最多的处理任务。被模型返回的数据是中立的,模型与数据格式无关,这样一个模型能为多个视图提供数据,由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。
V即View视图是指用户看到并与之交互的界面。比如由html元素组成的网页界面,或者软件的客户端界面。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操作的方式。
C即controller控制器是指控制器接受用户的输入并调用模型和视图去完成用户的需求,控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,然后再确定用哪个视图来显示返回的数据。
springMVC它是spring框架的一个分支,该springMVC框架主要完成的功能是:==接收浏览器的请求响应,对数据进行处理,然后返回页面进行显示== 可以把它理解为和Servlet干的工作是一样的。
2. springmvc如何搭建。依赖,配置文件,web.xml
1.创建maven-web工程。
用原来的web.xml文件替换现在的web.xml文件
2.在pom.xml中引入springmvc的依赖
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
3.注册DispatcherServlet到web.xml文件上
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--默认DispactherServlet加载的springmvc配置文件:WEB-INF/[servlet-name]-servlet.xml-->
<!--指定自己的配置文件-->
<servlet>
<servlet-name>DispactherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--classpath:表示编译后的路径-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispactherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.创建自己的springmvc.xml配置文件
5.在springmvc.xml文件中加入包扫描
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--包扫描-->
<context:component-scan base-package="com.xdh.controller"/>
</beans>
6.创建一个类,在类上加上@Controller 并且在方法上加上@RequestMapping注解
@Controller //该注解标记该类为处理层类---类似@WebServlet
public class HelloController {
@RequestMapping(value = "/hello01") //把请求路径映射到该方法上。
public String hello01(){
System.out.println("hello!");
return "hello01.jsp"; //响应一个页面
}
}
3.springmvc如何接受请求的参数。
[1]单个参数--方法中生成这个参数名
只需要给定方法的参数,参数必须要求和请求的参数名称一致。
[2]多个参数--封装一个java实体类,方法中声明该实体类即可。
[3]接受的为日期类型参数.
在时间类型的属性上添加一个注解:@DateTimeFormat(pattern = "yyyy-MM-dd")
//yyyy年 MM:月 dd日 24小时制
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthday;//出生日期
在springmvc配置文件上开启注解驱动
<!--开启注解驱动-->
<mvc:annotation-driven/>
4. 处理静态资源
springmvc的前端控制器DispatcherSerlvet也把静态资源拦截器。放行静态资源
<!--放行静态资源:哪些资源为静态资源。css img js html-->
<mvc:default-servlet-handler/>
4. springmvc如何保存数据到网页: request,session ,model
request: 作用范围: 同一个请求内有效。setAttribute(key,value)
session:作用范围: 同一个会话有效,只要会话不关闭会一直有效。setAttribute(key,value)
model:默认是request范围,加@SessionAttributes会增加session范围
package com.xdh.controller;
import com.xdh.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
/**
* @author: xmhz45
* @create: 2022/6/12
*/
@Controller
@SessionAttributes(value = {"user","user2"}) //设置哪些model的key在session范围
public class HelloController02 {
@RequestMapping("/list")
public String list(HttpServletRequest request){
//查询到一个学生信息。
Student student=new Student(1,"张三","110@qq.com",new Date());
//可以保存到request中,同一个请求
request.setAttribute("s",student);
return "list.jsp"; //转发
}
@RequestMapping("/list2")
public String list2(Model model){ //如果你使用request,你和tomcat容器绑定了。 建议使用Model.
//查询到一个学生信息。
Student student=new Student(1,"张三","110@qq.com",new Date());
//可以保存到model中,同一个请求 和request是一