• java——SpringMVC


     

     

    下面做一个简单的SpringMVC例子:

    创建一个比较标准的mvc项目,

     创建后在开发软件中导入该项目即可。

    首先配置pom.xml:

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>imooc-arthur</groupId>
      <artifactId>spring-mvc-study</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>spring-mvc-study Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <!-- 配置参数,为后面${}提供基础,也是为了方便管理,改动时只需改动改配置即可 -->
      <properties>
          <commons-lang.version>2.6</commons-lang.version>
          <slf4j.version>1.7.6</slf4j.version>
          <spring.version>4.1.3.RELEASE</spring.version>
      </properties>
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-framework-bom</artifactId>
                  <version>${spring.version}</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      
      <!-- 依赖,项目所依赖的包都可在这设置 -->
      <dependencies>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
          </dependency>
          <dependency>
              <groupId>commons-lang</groupId>
              <artifactId>commons-lang</artifactId>
              <version>${commons-lang.version}</version>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-log4j12</artifactId>
              <version>${slf4j.version}</version>
              <exclusions>
                  <exclusion>
                      <artifactId>slf4j-api</artifactId>
                      <groupId>org.slf4j</groupId>
                  </exclusion>
              </exclusions>
          </dependency>
      </dependencies>
      
      <!-- 插件:jetty插件是用于运行maven时给项目提供容器环境的 -->
      <build>
          <plugins>
              <plugin>
                  <groupId>org.eclipse.jetty</groupId>
                  <artifactId>jetty-maven-plugin</artifactId>
                  <version>9.2.2.v20140723</version>
              </plugin>
          </plugins>
      </build>
    </project>

    再配置web.xml,声明哪些请求会被送往前端控制器,而前端控制器又是依赖哪个配置文件:

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app id="WebApp_1587969167981">
      <display-name>Archetype Created Web Application</display-name>
      
      
      <servlet>
          <servlet-name>mvc-dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>mvc-dispatcher</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

    再编写前端控制器的配置文件mvc-dispatcher-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:c="http://www.springframework.org/schema/c"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p"
        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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 激活注解 @Required @Autowired @PreDestory and @Resourse等 -->
        <context:annotation-config/>
        
        <!-- DispatcherServlet上下文,只搜索@Controller标注的类 -->
        <context:component-scan base-package="com.imooc.mvcdemo">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        
        <mvc:annotation-driven/>
        <!-- 选择视图,perfix参数是前缀、suffix是后缀 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsps/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

    最后再编写控制器controller类,对其进行@Controller和对应@RequestMapping的注解:

    package com.imooc.mvcdemo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/hello")
    public class HelloMvcController {
        @RequestMapping("/mvc")
        public String helloMvc() {
            return "home";
    }
    }

    这里的RequestMapping表示的就是请求的路径,return表示的就是跳转的页面或servlet,比如上面例子当访问127.0.0.1:8080/hello/mvc时,就会跳转到

    /WEB-INF/jsps/home.jsp的这个jsp文件了;

    总的来说就是:web.xml配置了哪些请求会被前端控制器接收,且前端控制器的依赖配置文件会配置前端控制器去扫描哪些控制器,将请求转到哪些控制器处理,处理后的return又回到前端控制器,结合配置,再选择跳转到哪个页面,最后将页面作为response传回到用户的页面中去。

    做完配置后再项目所在目录内运行:,即可启动项目;

  • 相关阅读:
    js格式化文件大小,单位:Bytes、KB、MB、GB
    Java如何大批量从json数据源中按指定符号隔字符串,并修改、删除数据
    ExtJs定时消息提示框,类似于QQ右下角提示,ExtJs如何定时向后台发出两个请求并刷新数据实例
    Java jxl导入excel文件,导入的数字、身份证号码、手机号变成了科学计数法,解决方案
    Java 防SQL注入过滤器(拦截器)代码
    mysql8源码安装和MGR
    linux基础
    ThreadLocal
    redis缓存雪崩、穿透、击穿概念及解决办法
    ThreadLocal可能引起的内存泄露
  • 原文地址:https://www.cnblogs.com/lzj-learn/p/12786244.html
Copyright © 2020-2023  润新知