亲宝软件园·资讯

展开

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

CodeHuba 人气:0
想了解SpringMVC MVC架构原理及实现方法详解的相关内容吗,CodeHuba在本文为您仔细讲解SpringMVC架构的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring,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框架要做哪些事情?

加载全部内容

相关教程
猜你喜欢
用户评论