• Spring MVC 的环境搭建和入门小程序


    1.1.下载spring框架包。

    1.1.1百度搜索Spring Framework

    进入spring官网,在网页右边选择想要下载的版本。如图

    1.1.2进入页面按Ctrl+F搜索Distribution Zip Files

     

    找到如图页面,点击地址。来到下载面

    链接地址为:http://repo.spring.io/release/org/springframework/spring/

    Spring源码下载地址:https://github.com/spring-projects/spring-framework

    如下图:

     

    然后选择想要下载的版本就可以了。

    1.2.添加jar包到工程项目中

    解压下载的zip包,得到如下包结构

     

    libs目录下的jar文件复制一份到工程目录,WEB-INF/lib目录下。最重要的别忘了把commons-logging-1.2.jar的包倒进去,这个包需要另外下载,没有这个包的话,项目部署到Tomcat中会起不起来的,

    在我云盘里面有,提供给大家:链接:http://pan.baidu.com/s/1eSMdt2U 密码:vsfc

    如下图:

     

    1.3.配置web.xml文件

    Web.xml配置内容如下:

     1 <!-- 配置DispatcherServlet 拦截所有的用户请求 -->
     2 <servlet>
     3     <servlet-name>springDispatcherServlet</servlet-name>
     4     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     5 <!-- 配置DispatcherServlet 的初始化参数:配置Spring MVC配置文件的位置和名称 -->
     6     <init-param>
     7         <param-name>contextConfigLocation</param-name>
     8         <!--这是配置文件的路径,是放在src文件下的-->
     9         <param-value>classpath:springmvc.xml</param-value>
    10     </init-param>
    11     <load-on-startup>1</load-on-startup>
    12 </servlet>
    13 <servlet-mapping>
    14         <servlet-name>springDispatcherServlet</servlet-name>
    15         <url-pattern>/</url-pattern>
    16 </servlet-mapping>

    eclipse中配置了SpringIDE插件。按Alt+/可以自动添加dispatcher的相关配置。以上就是自动生成的。我们只需要修改url和配置文件为准和名称即可。

    <init-param.../>标签对中,定义了Spring mvc配置文件的位置和名称。在不声明此标签对的情况下,spring的默认配置文件路径是/WEB-INF/<servlet-name>-servlet.xml。此处的代码中的<servlet-name>名为springDispatcherServlet,所有配置文件的名称就应该为springDispatcherServlet-servlet.xml。并且应该放在WEB-INF目录下。

    1.4.配置Spring MVC配置文件

    Spring的配置文件名称和存放路径应该和web.xml配置文件中声明的一致。未声明文件目录的,一般放于src目录下。

    Spring配置文件如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
     8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
     9     <!-- 配置扫描的自定义的包 -->
    10     <context:component-scan base-package="org.cjh.Demo"></context:component-scan>
    11     <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->
    12     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    13         <property name="prefix" value="/WEB-INF/content/"></property>
    14         <property name="suffix" value=".jsp"></property>
    15     </bean>
    16 </beans>

    此处配置文件中,用到了<context:.../>标签对,我们在建xml文件的时候需要把beans文件路径添加进去。

    下面的property参数,分别声明的了物理页面的前缀(文件路径)和后缀(后缀名.jsp

    1.使用@RequestMapping()注解映射请求的URL

    2.返回值会通过视图解析器为实际的物理视图,对于 InternalResourceViewResolver内部资源视图解析器,会做如下解析:

    3.在配置文件中:prefix的值+RequestMapping()的传入值+suffix 就等于物理页面的路径。

    解析出来后的路径就是/WEB-INF/content/hello.jsp

    1.5.编写处理请求的处理器

    处理器需要在开头类的开头声明@Controller来告诉配置文件,我是一个处理器。然后在接受请求的方法前面声明@RequestMapping("/action")做请求映射,当请求接受到了action请求就会执行该方法。

    控制器代码如下代码如下:

     1 @Controller
     2 public class hello {
     3     /**
     4      * 1.使用@RequestMapping()注解映射请求的URL
     5      * 2.返回值会通过视图解析器为实际的物理视图,对于InternalResourceViewResolver内部资源视图解析器,会做如下解析:
     6      * 在配置文件中:prefix的值+RequestMapping()的传入值+suffix 就等于物理页面的路径。
     7      * /WEB-INF/content/hello.jsp
     8      * @return
     9      */
    10     @RequestMapping("/action")
    11     public String helloworld(){
    12         System.out.println("Hello World");
    13         return "hello";
    14     }
    15 }

    这个类声明了controller控制器,并且为方法声明了requestionmapping来处理请求信息。这个信息返回的“hello”字符串代表的是物理视图名称。详见代码中的注释。

    1.6.编写视图页面

    新建一个jsp页面然后在<body>...</body>标签对内填写如下链接。

    1 <a href="action">验证登录</a>

    index.jsp页面中声明的页面请求参数为action,对应了处理器处理的请求参数,所有这个页面会交个相应处理器处理。并且跳转相应页面。

    工程目录如下图:

    访问路径为:http://localhost:8080/Spring_test_01/action

    访问路径格式为:服务器名称/工程路径名/请求参数

  • 相关阅读:
    手工测试
    测试理论
    MySQL常用语法
    Linux设置静态ip
    设计模式
    Shiro
    TreeSet和TreeMap
    UDP和反射
    Linux归纳
    Spring+SpringMVC+Mybatis整合
  • 原文地址:https://www.cnblogs.com/caijh/p/6407240.html
Copyright © 2020-2023  润新知