Spring MVC框架学习笔记(一)之小程序的实现

Spring MVC框架

 spring MVC包含了一个dispatcher servlet的MVC框架,开箱即用,全名为org.springframework.web.servlet.DispatcherServlet。要使用这个Servlet,需要把它配置在web.xml(部署描述符)文件中。

web.xml文件(部署配置符)

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
         
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-name>是spring MVC的配置文件的*,*-servlet.xml,对应在应用程序目录的WEB-INF下的对应文件。

<init-param>可选,可以将下面的spring MVC的配置文件放到应用程序目录的任何地方。

<load-on-startup>可选,若存在,将在应用程序启动时装载servlet并调用它的init方法;若不存在,则在servlet的第一个请求时加载。

<!-- 统一编码 -->
<filter>
    <filter-name>charsetEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>charsetEncoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

*-servlet.xml(spring MVC的配置文件)

<?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">

    <!-- controller扫描路径 -->
    <context:component-scan base-package="com.test.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


	<!-- 视图层配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 配置根视图 -->
    <mvc:view-controller path="/" view-name="hello"/>

    <!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,
    @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->
    <mvc:annotation-driven />

    <!-- 静态资源配置 -->
    <mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources>

</beans>

Spring使用扫描机制来找到应用程序中所有基于注解的控制器类,就需要在配置文件中做两件事情:

<!-- 1.声明spring-context -->
<beans 
    xmlns:context="http://www.springframework.org/schema/context" 
>

    <!-- 2.指定控制器类的基本包 -->
    <context:component-scan base-package="basePackage" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

<component-scan/>(spring MVC的配置文件中最主要的)要指示扫描目标包中的类

如果没有<annotation-driven />和<resources/>会阻止任意控制器被调用。若不需要resources,则不需要<annotation-driven />

加入相关的jar包

jar包下载地址:http://repo.spring.io/release/org/springframework/

编写控制器(hello.java)

package com.test.controller;

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

@Controller
@RequestMapping(value="/hello")
public class hello {

	@RequestMapping(value="/world",method=RequestMethod.GET)
	public String hello(Model model){
        model.addAttribute("msg", "你好spring mvc");
        return "hello";
    }
	
}

(一)基于注解的控制器(实现一个控制器类可以包含多个请求处理方法)

有两种类型:@Controller和@RequestMapping

@Controller用来指示该类为一个控制器。

@RequestMapping用来映射一个请求和一个方法,让spring知道用哪一种方法来处理它的动作,可以注解一个控制器类,也可以注解方法。

@RequestMapping(value="/world")
@RequestMapping("/world")
@RequestMapping(value="/world",method={RequestMethod.POST,RequestMethod.PUT})

value属性将URI映射到方法,若只有value这一个属性,可以省略属性名称。

method属性用来指示该方法仅处理哪些HTTP方法,如果没有指定,则处理方法可以处理任何HTTP方法。

params属性用来传入参数。

(二)Model实例

无论是否会使用,Spring MVC都会在每一个请求处理方法被调用时创建一个Model实例,主要目的是添加需要在视图中显示的属性。通过调用model.addAttribute来添加msg,方便之后视图层的访问。

model.addAttribute("msg", "你好spring mvc");

编写展示层(hello.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>Hello</title>
</head>
<body>
	<h1>hello mvc!</h1>
	${msg }
</body>
</html>

程序目录结构:

猜你喜欢

转载自blog.csdn.net/qq_41443301/article/details/81051085