• Spring MVC的使用11


    1.首先需要下载 spring的框架,下载后导入工程

     http://maven.springframework.org/release/org/springframework/spring/

    2. web.xml进行配置

     

      <!-- 配置DispatchcerServlet -->

        <servlet>

            <servlet-name>springDispatcherServlet</servlet-name>

            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

            <!-- 配置Spring mvc下的配置文件的位置和名称 -->

            <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>springDispatcherServlet</servlet-name>

            <url-pattern>/</url-pattern>

        </servlet-mapping>

      

      <!-- 增加对静态资源的支持 -->

        <servlet-mapping>

         <servlet-name>default</servlet-name>

         <url-pattern>*.css</url-pattern>

    </servlet-mapping>

     

    <servlet-mapping>

          <servlet-name>default</servlet-name>

          <url-pattern>*.gif</url-pattern>

    </servlet-mapping>

     

    <servlet-mapping>

          <servlet-name>default</servlet-name>

          <url-pattern>*.jpg</url-pattern>

    </servlet-mapping>

     

       <servlet-mapping>

         <servlet-name>default</servlet-name>

         <url-pattern>*.js</url-pattern>

       </servlet-mapping>

     

        <servlet-mapping>

          <servlet-name>default</servlet-name>

          <url-pattern>*.html</url-pattern>

        </servlet-mapping>

     

    3. 创建springmvc.xml  

    <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"

        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-4.0.xsd

            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

            

            

            <!-- 配置自动扫描的包 -->

            <context:component-scan base-package="com.jackie.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 = ".html"></property>

            </bean>

    </beans>

     

    4.  开始设置代码

    html中设置

    <a href="testPathVariable/1">testPathVariable</a><br/><br/>

     

    代码中写如下

    @Controller

    public class HelloWorld {

     

    /**

    10      * 1. 使用RequestMapping注解来映射请求的URL

    11      * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析

    12      * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作

    13      * "/WEB-INF/views/success.jsp"

    14      * @return

    15      */

       

          @RequestMapping("/testPathVariable/{id}")

          public String testPathVariable(@PathVariable(value="id") Integer id){

              System.out.println("testPathVariable:" + id);

              return "result";

          }

    }

    浏览器输入 http://localhost:8080/{工程名}/testPathVariable/1 

    testPathVariable就会被执行

  • 相关阅读:
    hdu 2106 decimal system
    00-自测4. Have Fun with Numbers (20)
    07-图4. Saving James Bond
    hdu 2209 翻纸牌游戏
    hdu 1272 小希的迷宫
    1969 Pie
    怎样维护 SQLite
    Navicat使用亮点
    Navicat for MySQL 11 Mac安装教程
    Navicat for PostgreSQL 运算符有哪些类别
  • 原文地址:https://www.cnblogs.com/menchao/p/6007178.html
Copyright © 2020-2023  润新知