zoukankan      html  css  js  c++  java
  • SpringMVC -- 第一个简单的程序

    学习springMVC,我们来记录下第一个HelloWord的程序

    首先。我们组织须要的jar包
    commons-logging-1.1.3.jar
    spring-aop-4.1.7.RELEASE.jar
    spring-beans-4.1.7.RELEASE.jar
    spring-context-4.1.7.RELEASE.jar
    spring-core-4.1.7.RELEASE.jar
    spring-expression-4.1.7.RELEASE.jar
    **spring-web-4.1.7.RELEASE.jar
    spring-webmvc-4.1.7.RELEASE.jar**
    主要是引入了2个MVC的jar包。

    以下我们来配置web.xml

      <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:application.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    这里我们配置了DispatcherServlet类,然后< url-pattern>/ < /url-pattern>拦截全部请求。

    再来看下application.xml

        <context:component-scan base-package="com.gp.springmvc"></context:component-scan>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>

    配置扫描注解的路径
    注入InternalResourceViewResolver类。通过此类,我们能够匹配到要展示的JSP页面。通过prefix指定JSP所属文件夹。通过suffix指定文件的后缀名。

    我们来看下后台类

    @Controller
    public class HelloWorld {
    
        @RequestMapping("/sayHello")
        public String sayHello(){
            System.out.println("hello world!!!");
            return "success";
        }
    }

    配置@Controller表示控制器,就是将此类注入到spring的容器中。
    @RequestMapping(“/sayHello”)表示要被拦截的方法。兴许我们会有仔细的分析,这里我们就理解为定义什么名,那我们在訪问的时候就訪问这个名,就能够找到这种方法。

    JSP页面

    <a href="sayHello">sayHello</a>

    在链接中输入sayHello,后台会直接拦截请求,找到配置为@RequestMapping(“/sayHello”)的方法,执行。
    方法return “success”;,后台会依据application.xml的配置找到相应文件夹的JSP

    如图,文件夹结构
    这里写图片描写叙述

    功能跳转到success.jsp

    <body>
    
        <h4>Hello world!!!</h4>
    
    </body>

    部署程序,执行127.0.0.1:8080/springMVC_01/index.jsp。查看结果。

  • 相关阅读:
    [JZOJ3339]【NOI2013模拟】wyl8899和法法塔的游戏
    [JZOJ3337] 【NOI2013模拟】wyl8899的TLE
    UVA 1262 Password
    UVA 10820 Send a Table
    UVA 12716 GCD XOR
    UVA 10791
    UVA 10375 choose and divide (唯一分解定理)
    欧拉函数
    51 Nod 1069 Nim游戏
    51 Nod 1242 矩阵快速幂求斐波那契数列
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7218100.html
Copyright © 2011-2022 走看看