Spring MVC的使用1

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就会被执行

原文地址:https://www.cnblogs.com/menchao/p/6007177.html