SpringMVC(1) 搭建环境

4 篇文章 1 订阅

 

  1. 使用IDEA搭建环境还是比较简单的,可以让IDEA自己去下载包,可以使用maven,也可以自己手动导入,我就用手动导入吧,随便咯~
  2. 新建工程,然后导入jar包,除了下图之外其他就没有啥要特别注意了的。                                                                              
  3. 项目结构图 ,jar包链接:在此   提取码:6r4w 
                                                                                                                 
  4. 在web.xml中配置前端控制器,其实吧IDEA新建这些项目和eclipse道理是一样的,给出web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <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*:springmvc.xml</param-value>
                </init-param>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
    </web-app>

    需要指出的是classpath就是src目录啦~~

  5. 配置springmvc.xml文件,也就是核心配置文件

    <?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:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.2.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
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.2.xsd">
    
    
        <!-- 开启组件扫描 -->
        <context:component-scan base-package="com.springmvc"/>
    
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

     

  6. 接下来是写相应请求的方法,首先在web目录下写index.jsp,注意不能在WEB-INF下写,因为WEB-INF是服务器内部路径,它是不可以由浏览器直接访问的,但是可以通过服务器内部转发。然后写经由转发后得到的success.jsp,项目结构如下 

        index.jsp和succes.jspn内容如下

<%--
  Created by IntelliJ IDEA.
  User: 11955
  Date: 2019/3/14
  Time: 0:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>index.jsp</title>
  </head>
  <body>
      <a href="hello SpringMVC">SpringMVC</a>
  <%--这里只是写了一个简单的请求,它会给服务器发http://localhost:8080/hello SpringMVC,这样的请求--%>
  </body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 11955
  Date: 2019/3/14
  Time: 9:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success.jsp</title>
</head>
<body>
    success!
</body>
</html>

8.然后就是写方法了,在这里方法名并不是很重要的,重要的是对方法的注解,用的是RequestMapping,它要有一个参数,就是jsp请求中位于Demo1/后边那些,这样springMVC才会知道把这个请求交给他去处理,这个方法位于sec下的com.springmvc.helloWorld包里,在springmvc.xml中配置的自动扫描包就是com.springmvc,所以在这里写的方法会被扫描到,然后它的返回值是success.jsp,因为在视图解析器中给定的前缀是/WEB-INF,后缀给的是jsp,它的工作机制是将方法的返回值拼接上前缀和后缀,得到物理路径,在这里得到的是http://localhost:8080/hello%20SpringMVC.也就会跳转到success.jsp去了。

package com.springmvc.helloWorld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 请求处理器/控制器
* */
@Controller
public class SpringMVCHandle {

    /**
     * 这个方法就是处理http:localhost:8080/Demo1/hello SpringMVC
    * */
    @RequestMapping("hello SpringMVC")
    public String handleHelloSpringMVC(){
        System.out.println("hello SpringMVC");
        return "success";
    }

}

 

9、最后梳理一下真个请求的工作机制:

   1、服务器发送请求,先到web.xml下被拦截,将请求交给springDispatcherServlet,

   2、springDispatcherServlet根据请求的标识将请求交给@RequestMapping("hello SpringMVC")注解的方法

   3、方法的返回值经过视图解析器解析后得到相应页面,服务器将相应的页面返回

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值