• 简介及spring mvc初体验




    一、CS和BS

    • C/S

        C/S Client/Server 的简写,简称客户端/服务器模式。例如 QQ 是客户端和服务器模式,首先安装一个客户端到个人电脑,然后登入到腾讯服务器。
        缺点:更新不易,例如 3 亿人在用 QQ 话,如果有新版本的话,需要更新 3 亿个客户端
        优点:速度快,功能强

    • B/S

        Browser/Server 的简写,简称浏览器/服务器模式。这种模式不需要安装特定客户端只要有一个浏览器即可。
        优点:更新容易,例如某个网站有新版了,浏览器不需要更新,就能看到新版本
        缺点:速度慢,功能有限(随着 HTML5 出现,功能问题已经很强
    )

    • MVC分工

          1. Model:负责数据的处理(增删改查等操作)
         2. 前端控制器:负责转发
         3. 后端控制器:负责处理具体请求
           4. 视图:给用户提供使用软件的界面


    二、Spring Mvc 初体验

    • 新建war类型的maven项目

    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.edu.xcu</groupId>
    <artifactId>01springmvcdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
      <properties>
        <!-- 控制乱码问题 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <!-- 修改版本号 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
      <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.1.8.RELEASE</version>
        </dependency>
      </dependencies>
    </project>
    • 这个时间会报错,为了解决该错误,我们需要添加 web.xml

    • web.xml 中配置前端控制器
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" 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_3_1.xsd">
      <!-- 前端控制器:类似迎宾 -->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 因为顾客是否有人来,迎宾都要提前准备好迎客 -->
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 谁来了都迎接 -->
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    • 编写后端控制器,即controller类
    @Controller // 告诉后端控制器
    public class HelloWorldController {
      @RequestMapping("/hw01") // 配置上地址。类似包厢上必须有号
      @ResponseBody
      public String hw01() {
        return "hello world!";
      }
      @RequestMapping("/hw02")
      public String hw02() {
        return "hello world222!";
      }
    • 在WEB-INF下新建spingmvc-servlet.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"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
            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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
        <!-- 用注解配置后端控制器 -->
        <context:component-scan base-package="cn.edu.xcu.ssm"></context:component-scan>
        <!-- 因为能产生动态web技术有: jsp,freemarker,thymeleaf等,因而你需要告诉springmvc你视图放置在那里,是什么技 
            术:视图解析器 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <!-- 告诉到那里找 -->
            <property name="prefix" value="/WEB-INF/jsp/" />
            <!-- 告诉后缀名 -->
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    • 添加视图解析器所需要的依赖
                <!--视图解析器所需要的依赖 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>            


  • 相关阅读:
    oracle删除归档日志
    ORA-16014: log 3 sequence# 540 not archived, no available destinations
    rsync
    vmware 虚拟机导入OVF出现路径错误
    RHEL5.X 重启网卡出现./network-functions: line 78: .: ifcfg-eth0: file not found
    AIX 6.1记录
    CentOS网卡显示为__tmpxxxxxxxx
    PowerPiggy 博客
    利用谷歌浏览器插件获得更好的浏览体验
    C# 的 MSIL 简介
  • 原文地址:https://www.cnblogs.com/yuanshuai1026/p/11734526.html
Copyright © 2020-2023  润新知