MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是一种软件设计规范;就是将业务逻辑、数据、显示分离的方法来组织代码;MVC主要作用是降低了视图与业务逻辑间的双向偶合,MVC不是一种设计模式,而是一种架构模式。
最典型的MVC就是JSP + servlet + javabean的模式!
1.用户发送请求
2.servlet接收请求数据,并调用对应的业务逻辑方法
3.业务层调用dao持久层,处理完成后返回数据库的数据给servlet
4.servlet转向到JSP,由JSP来显示页面
5.响应给前端更新后的页面
2.model
3.view
servlet控制程序
package online.helloweb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//实现Servlet接口
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//取得参数
String method = req.getParameter("method");
if (method.equals("add")){
req.getSession().setAttribute("msg","执行了add方法");
}
if (method.equals("delete")){
req.getSession().setAttribute("msg","执行了delete方法");
}
//业务逻辑
//视图跳转
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
转发到hello.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello world</title>
</head>
<body>
${msg}
</body>
</html>
编写web.xml注册servlet
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>online.helloweb.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/userurl-pattern>
servlet-mapping>
web-app>
编写index.jsp页面,提交数据到HelloServlet
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index !</title>
</head>
<body>
<h2>Hello World!</h2>
<form action="user" method="get">
<input name="method" value="add"/>
</form>
</body>
</html>
最后,配置tomcat实现访问即可;
localhost:8080/mvc01/user?method=add
localhost:8080/mvc01/user?method=delete
注:localhost:8080是tomcat的域名;mvc01是tomcat发布的项目站点名;user是HelloServlet的url;?method=add 表示从index.jsp页面通过get方式传递到servlet的参数。
Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级web框架;Spring的web框架围绕DispatcherServlet[调度servlet]设计,其作用是将请求分发到不同的处理器。
Spring MVC的特点:
当用户发起请求时,前端控制器(中心控制器)拦截到请求 --> 根据请求参数生成代理请求 --> 找到对应的实际控制器 --> 控制器处理请求,创建数据模型,访问数据库 --> 将模型响应给中心控制器 --> 中心控制器把模型给到视图 --> 视图处理,结果返回给中心控制器 --> 中心控制最终将结果返回给请求者
注:图中实线表示Spring MVC框架提供的技术,不需要开发者实现,虚线表示需要开发者实现
分析执行流程
配置web.xml,注册DispatcherServlet
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
配置Spring MVC的配置文件->springmvc-servlet.xml:[servletname]-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
编写我们要操作业务controller(servlet),要么实现controller接口,要么增加注解;需要返回一个ModelAndView,装数据,封视图。
package online.helloweb.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView 模型和视图
ModelAndView mv = new ModelAndView();
//封装对象,放在ModelAndView中
mv.addObject("msg","HelloSpringMVC!");
//封装要跳转的视图,放在ModelAndView中
mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
return mv;
}
}
把自己的类HelloController交给SpringIOC容器,注册bean;
<bean id="/hello" class="online.helloweb.servlet.HelloController"/>
编写要跳转的hello.jsp页面,显示ModelAndView存放的数据,以及正常页面;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello !</title>
</head>
<body>
${msg}//EL表达式,取得数据
</body>
</html>
最后,配置tomcat,以及项目发布;
1.建立一个maven项目(注:pom文件中需要设置资源过滤,否则会有资源文件打包不成功问题,导致项目无法运行),在pom.xml文件中引入相关的依赖:主要有junit、spring框架核心库、spring MVC、servlet、jstl、mysql-connector、mybatis、mybatis-spring等;
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.24.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
2.修改web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
3.添加Spring MVC配置文件
<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: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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="online.helloweb.servlet"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
bean>
beans>
4.创建controller
编写一个servlet,控制类:online.helloweb.controller.HelloController;
注解为@Controller是为了让Spring IOC容器初始化时自动扫描到;
@RequestMapping是为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/HelloController/hello;
方法中声明Model类型的参数是为了把Action中的数据带到视图中;
方法返回的结果是视图的名称hello,加上配置文件中的前后缀变成WEB-INF/jsp/hello.jsp。
package online.helloweb.servlet;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/HelloController")
public class HelloController {
//真实访问地址 : 项目名/HelloController/hello
@RequestMapping("/hello")
public String sayHello(Model model){
//向模型中添加属性msg与值,可以在JSP页面中取出并渲染
model.addAttribute("msg","hello,SpringMVC");
//web-inf/jsp/hello.jsp
return "hello";
}
}
5.创建视图层
在WEB-INF/ jsp目录中创建hello.jsp , 视图可以直接取出并展示从Controller带回的信息;可以通过EL表示取出Model中存放的值,或者对象;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC</title>
</head>
<body>
${msg}
</body>
</html>
6.配置tomcat运行
配置tomcat,设置项目名,开启服务器,访问对应的请求路径;
本文转自:西部开源-秦疆老师:SpringMVC系列博客 , 秦老师交流Q群号: 664386224
链接: 狂神说Java.