SSM笔记-SpringMVC基本使用

1、使用步骤:
(0)添加jar包,jar下载地址:http://repo.spring.io/release/org/springframework/spring/
(1)在javaWeb项目的web.xml文件中配置DispatcherServlet和请求拦截的规则
(2)在web.xml中配置spring配置文件的读取路径
(3)创建spring配置文件并且配置自动扫描包和配置视图解析器
(4)创建handler类,并且用@Controller修饰该类,如果需要定义hanlder的路径则使用@RequestMapping修饰该类
(5)在handler类里面写handler方法,并且使用@RequestMapping修饰该方法,并使该方法返回对应视图的文件名
(6)编写发送请求的页面和结果页面

2、web.xml配置
(1)通过在servlet中配置org.springframework.web.servlet.DispatcherServlet来提供Spring Web MVC的集中访问点,而且负责职责的分派
(2)在init-param中配置contextConfigLocation,来指定使用哪个springmvc配置文件
(3)在servlet-mapping中对之前配置的DispatcherServlet指定请求的地址类型

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>SpringMVC_1_test</display-name>
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3、springmvc配置
(1)配置自动扫描包,以此来告诉web引用需要扫描哪个包下的类
(2)配置视图解析器,通过这个配置把handler返回值解析为实际视图
(3)视图解析器中的bean类为org.springframework.web.servlet.view.InternalResourceViewResolver
(4)property中prefix值为视图文件所在文件夹的名字
(5)property中suffix值为视图文件的名字
(6)如果配置了mvc:default-servlet-handler,则程序会在项目中找目标资源

<?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.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

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

    <!-- 配置视图解析器,把handler返回值解析为实际视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:default-servlet-handler/>

</beans>

4、Handler写法
(1)首先要用@Controller来修饰类
(2)如果需要在访问的时候根据类来定位handler方法的话则需要使用@RequestMapping(“/xxx”)来修饰该类
(3)编写hanlder方法并且使用@RequestMapping(“/xxx”)修饰该方法
(4)handler方法的返回值为视图文件文件名
(5)配置方法和写法注意代码的注释

package com.test.springmvc.handlers;

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

@RequestMapping("/test")
@Controller
public class Handler {

    //1、使用@RequestMapping注释来映射请求的url
    //2、返回值通过视图解析器解析为实际的视图
    //  InternalResourceViewResolver视图解析器会通过prefix+returnVal+suffix,得到实际视图,然后转发
    //  如:/views/success.jsp
    //3、@RequestMapping可以注解在类上或者方法上
    //  ①注解在类上:提供初步请求映射信息,相当于WEB应用根目录
    //  ②注解在方法上:提供进一步映射信息
    //  ③如果类上不注解,则方法上注解就默认相当于WEB应用根目录上
    //  ④如果注解类上面之后,运行报404,重新导入jar包就好了

    //简单测试@RequestMapping
    @RequestMapping("/testHandler")
    public String handlerTest(){
        System.out.println("handlerTest");
        return "result";
    }

    //value:请求url,映射地址
    //method:请求方法, GET/POST
    //params:请求参数, 请求的值必须符合params的规定
    //headers:请求头的值必须符合headers的规定

    //测试@RequestMapping的属性值
    @RequestMapping(value="/testrequestMapping",
                    method=RequestMethod.GET,
                    params={"id","id!=0"},
                    headers={"Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}
                    )
    public String requestMappingTest(){
        System.out.println("requestMappingTest");
        return "result";
    }

    //@PathVariable作用:将URL绑定的占位符绑定到控制器处理方法的入参中,为之后的REST思想提供基础
    //测试@PathVariable映射URL绑定的占位符
    @RequestMapping("/testPathVariable/{str}")
    public String testPathVariable(@PathVariable("str") String str){
        System.out.println("testPathVariable:"+str);
        return "result";
    }
}

5、发送访问请求页面及结果页面代码
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="index2.jsp">Test mvc:default-servlet-handler</a>
    <br><br>
    <a href="test/testPathVariable/xxxx">Test PathVariable</a>
    <br><br>
    <a href="test/testHandler">Test Handler</a>
    <br><br>
    <a href="test/testrequestMapping?id=1">Test requestMapping</a>
    <br><br><br>
    基本配置步骤:<br>
    1、加入jar(用Spring的jar)<br>
    2、web.xml配置DispatcherServlet<br>
    3、编写处理逻辑类,并为该类添加@Controller标签<br>
    4、创建spring配置文件,配置自动扫描包<br>
    5、在spring配置文件文件中,配置视图解析器<br>
    6、创建调用页面(指定调用的请求url)和结果页面<br>
    7、在处理逻辑类的方法中,使用@RequestMapping注释来映射请求的url<br>
    8、测试<br>

    SpringMVC不存在对应映射的时候(如上面的index2.jsp请求),如果配置了mvc:default-servlet-handler,则程序会在项目中找目标资源(即在项目中寻找index2.jsp)

</body>
</html>

结果页面
/views/result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
result page
</body>
</html>

测试mvc:default-servlet-handler的结果页面
index2.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>
    test mvc:default-servlet-handler
</body>
</html>

6、项目目录
项目目录

7、demo
https://download.csdn.net/download/qq_22778717/10598982

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/81607535
今日推荐