zoukankan      html  css  js  c++  java
  • 01-SpringMVC-HelloWorld

    一、什么是SpringMVC

    • SpringMVC是非常优秀的MVC框架
    • SpringMVC比Struct2更优秀。至于原因,我们可以先简单的理解,Spring4MVC是在Struct2后面出现的,SpringMVC占有后发优势。至于其他的原因,学完了这套课程后,我们再做详细的比较。

    二、第一个SpringMVC的项目:HelloWorld

    1、添加jar包

    1.1 spring包

    • spring-aop-4.2.1.RELEASE.jar
    • spring-beans-4.2.1.RELEASE.jar
    • spring-context-4.2.1.RELEASE.jar
    • spring-core-4.2.1.RELEASE.jar
    • spring-expression-4.2.1.RELEASE.jar
    • spring-web-4.2.1.RELEASE.jar
    • spring-webmvc-4.2.1.RELEASE.jar

    1.2 依赖的包

    • commons-logging-1.1.1.jar   spring日志包

    2、在web.xml中配置DispatcherServlet

    <!-- 配置 DispatcherServlet -->
    <servlet>
    <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    3 加入SpringMVC的配置文件

    3.1 springmvc.xml放在src目录下

    • 将beans、context、mvc 三个schema加入进来
    <?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"
    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.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
     
     
    </beans>

    3.2 在springmvc.xml配置扫描包

    <!-- 自动扫描包 -->
    <context:component-scan base-package="cn.imentors.springmvc.handlers"/>

    3.3 配置视图解析器

    <!-- 配置视图解析器:如果把handler方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
    注:InternalResourceViewResoler的解析方式如下
        prefix+returnVal+后缀(suffix)

    4 编写片是请求的处理器,并标识为处理器

    4.1 编写一个类

    public class HelloWrold {
    public String hello(){
    System.out.println("Hello World");
    return "success";
    }
    }

    4.2 将这个类标识为@Controller

    @Controller
    public class HelloWrold {

    4.3通过@RequestMapping 注解来映射请求的url

    /**
    * 通过@RequestMapping来注解请求的URL
    * @return
    */
    @RequestMapping("/helloworld")
    public String hello(){

    5 编写视图

    5.1 index.jsp

    <a href="helloworld">helloworld</a>

    5.2 success.jsp

    4>success</h4>

    6、总结

        springmvc的工作流程。这里只简单的介绍一下,springmvc详细的工作原理,等我们把这门课程学门,再作分析。
    •     用户请求
    •     controller
    •     返回到view

    关注我们
        师享空间的宗旨是分享知识,传播价值。关注我们,及时获得更多信息。



    捐赠我们

        如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
           







  • 相关阅读:
    leetcode——832. 翻转图像
    leetcode——830. 较大分组的位置
    leetcode——1089.复写零
    leetcode——86. 分隔链表
    leetcode——387. 字符串中的第一个唯一字符
    leetcode——389. 找不同
    leetcode——61. 旋转链表
    leetcode——24. 两两交换链表中的节点
    leetcode——817. 链表组件
    leetcode——234. 回文链表
  • 原文地址:https://www.cnblogs.com/imentors/p/4989133.html
Copyright © 2011-2022 走看看