【SpringMVC】---搭建框架步骤

项目如下

一、加入 Jar 包

部分jar包可以不导(第4、9、11个可以不导入)

二、在 Web.xml 中配置 DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC_001_001</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <!-- 配置 DispatcherServlet -->
  <servlet>
      <!-- springDispatcherServlet 在应用启动的时候被创建,不是调用的时候被创建。 -->
      <!-- 实际上也可以不通过 contextConfigLocation 来配置 springmvc 的配置文件,而使用默认的。 -->
    <!-- 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml -->
    <!-- 把 SRC 下的 spring-mvc.xml 移动在/WEB-INF/下并改名为 springDispatcherServlet-servlet.xml的文件,并注释 R20~R23 -->
    <!--配置 DispatcherServlet 初始化参数,作用是配置 SpringMVC 配置文件的位置和名称-->
    
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
      <!-- 将所有请求映射到DispatcherServlet处理 -->
      
  <servlet-mapping>
  
      <!--配置 DispatcherServlet 初始化参数,作用是配置 SpringMVC 配置文件的位置和名称-->
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 配置HiddenHttpMethodFilter,可以把POST请求转换为DELETE或PUT请求 -->
  <filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

三、加入 SpringMVC 的配置文件

在 SRC 下面创建 spring-mvc.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.chinasofti"></context:component-scan>

    <!-- 配置视图解析器:如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

四、编写处理请求的处理器,并标识为处理器

package com.chinasofti.springmvc.handlers;

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


@Controller
public class HelloWorld {
    /**
    * 1、使用 RequestMapping 注解来映射请求的 URL(统一资源定位)
    * 2、返回值会通过视图解析器解析为实际的物理视图,对于 InternalResourceViewResolver 而言,
    * 视图解析器会做如下的解析:
    * 2.1、通过 prefix + returnVal + 后缀这样的方式得到实际的物理视图,然后做转发操作。
    * 如:/WEB-INF/views/success.jsp
    * @return
    */
    
    public static final String SUCCESS="success";
    @RequestMapping("/helloword")
    public String hello(){
        System.out.println("hello word");
        return SUCCESS;
        }
}

五、在 spring-mvc.xml 中配置自动扫描的包(见步骤三)

六、给 HelloWord 注解为控制器(SpringMVC 里面叫 handler,也叫请求处理器)(见步骤四)

七、在 Spring-mvc.xml 中配置视图解析器(见步骤三)

success.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功?
</body>
</html>

 

八、编写请求视图

创建 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="helloword">跳转</a>
</body>
</html>

九、运行项目

 

 

总结

 添加 Jar 包
 配置 Web.xml 文件
 配置 DispatcherServlet
 引入 contextConfigLocation 配置 spring 文件初始化参数
 配置那些请求可以请求 DispatcherServlet
 配置 springmvc 的配置文件
 配置注解扫描包
 配置视图解析器
 编写控制器 handler
 通过@RequestMapping 注解映射请求
 配置视图

posted @ 2017-09-04 18:03  Angelye  阅读(528)  评论(0编辑  收藏  举报