美文网首页
SpringMVC HelloWorld

SpringMVC HelloWorld

作者: 桑鱼nicoo | 来源:发表于2020-02-09 12:33 被阅读0次
    1. 创建Maven工程:可以参考这个链接

    2. pom注入依赖的jar包

    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>5.1.12.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.12.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>5.1.12.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>5.1.12.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.1.12.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.1.12.RELEASE</version>
        </dependency>
      </dependencies>
    
    1. 配置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>
      <display-name>Archetype Created Web Application</display-name>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置和名称-->
            <!-- 实际上也可以不通过contextConfigLocation 来配置SpringMVC的配置文件,而使用默认的-->
            <!-- 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml -->
    <!--        <init-param>-->
    <!--            <param-name>contextConfigLocation</param-name>-->
    <!--            <param-value>classpath:springmvc:xml</param-value>-->
    <!--        </init-param>-->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    
    1. 配置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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 配置自动扫描的包-->
        <context:component-scan base-package="com.sangyu.springmvc"></context:component-scan>
        <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>
    
    
    1. 编写HelloWorld.java文件
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloWorld {
        /**
         * 1. 使用@RequestMapping 注解来映射请求的URL
         * 2. 返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver
         * 视图解析器,会做如下的解析:
         * 通过prefix + returnVal + suffix,得到实际的物理视图,然后做转发操作
         * /WEB-INF/views/success.jsp
         * @return
         */
        @RequestMapping("/helloworld")
        public String hello(){
            System.out.println("hello world");
            return "success";
        }
    }
    
    1. 编写index.jsp
    <html>
    <body>
    <h2>Hello World!</h2>
    <a href="helloworld">Hello World!</a>
    </body>
    </html>
    
    1. 创建success.jsp文件
    <body>
    <h4> success page</h4>
    </body>
    
    1. 配置tomcat,可以参考这个链接,tomcat配置完成后如下图:
    1. 整个项目的目录结构
    1. 执行tomcat


    1. HelloWorld 运行流程图

    相关文章

      网友评论

          本文标题:SpringMVC HelloWorld

          本文链接:https://www.haomeiwen.com/subject/yinuxhtx.html