java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringMVC架构

SpringMVC MVC架构原理及实现方法详解

作者:CodeHuba

这篇文章主要介绍了SpringMVC MVC架构原理及实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

SpringMVC

ssm:Mybatis+Spring+SpringMVC MVC三层架构

JavaSE:认真学习,老师带,入门快

JavaWeb:认真学习,老师带,入门快

SSM框架:研究官方文档,锻炼自学能力,笔记能力,项目能力

SpringMVC+Vue+SpringBoot+SpringCloud+Linux

SSM = JavaWeb做项目;

Spring:IOC和AOP

SpringMVC:SpringMVC的执行流程!【重点】

SpringMVC:SSM框架整合!

1. 回顾MVC

什么是MVC?

新建一个Maven工程当作父工程!pom依赖!

<!--依赖-->
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.9.RELEASE</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>

</dependencies>

建立一个Moudle:springmvc-01-servlet,添加web app的支持。
导入servlet和jsp依赖:已经包含在父依赖
编写一个Servlet类,用来处理用户的请求

public class HelloServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //1.获取前端参数
    String method = req.getParameter("method");
    if(method.equals("add")){
      req.getSession().setAttribute("msg","执行了add方法");
    }
    if(method.equals("delete")){
      req.getSession().setAttribute("msg","执行了delete方法");
    }
    //2.调用业务层

    //3.页面跳转
    req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req,resp);
  }
}

编写Hello.jsp,在WEB-INF目录下新建一个jsp的文件夹,新建hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
#{msg}
</body>
</html>

在web.xml中注册servlet

<servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/hello</url-pattern>
</servlet-mapping>

配置Tomcat,并启动测试!localhost:8080?hello/method=addlocalhost:8080?hello/method=delete

注意点:

每个模块需要配置到tomcat-deployment,必须部署到服务器上!!!

MVC框架要做哪些事情?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文