SpringMVC应用

一、SpringMVC的框架概述

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts 2(一般老项目使用)等。

二、编写第一个SpringMVC的小程序

1、创建WEB项目,引入SpringMVC框架jar包

在这里插入图片描述

2、定义Controller

指的是服务器端的控制层

package web;

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

@Controller
@RequestMapping(LoginController.BASE_CONTROLLER)
public class LoginController {
    // 字符串的常量提取
    public static final String BASE_CONTROLLER = "/loginController";
    public static final String INDEX_PAGE = "../index.jsp";

    // 模拟登录
    @RequestMapping("/login")
    public String doLogin(){

        // 服务器的处理
        System.out.println("正在登录------------登录完成");

        // 给出响应
        return INDEX_PAGE;
    }
}
3、对外提供了Controller层的访问地址

通过注解在类中提供、并基于Spring容器的,实现扫描与注解支持

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 引用context组件
        使用ioc的注解扫描与支持: base-package的属性指定是: 父路径
        DI也支持
      -->
    <context:component-scan base-package="web" />

    <!--  支持springmvc的注解   -->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>
4、基于web.xml提供

核心过滤器(DispatchServlet),用于加载匹配映射地址

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

    <!--  指定DispatchServlet 核心servlet处理器: 指定SpringMVC的容器位置   -->
    <servlet>
        <servlet-name>servlet</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>
    // (/*)将所有的客户端请求,都将与@RequestMapping映射地址进行匹配
    // (/)将根据后缀文件名直接匹配静态资源,默认无后缀则匹配映射地址
    // (*.do) : 根据后缀名用于区分映射请求,和静态资源的请求:.html/.js/.css
                
        
        <servlet-name>servlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>
5、响应view页面视图

使用ModelAndView

6、基于浏览器发起页面请求,测试处理与响应
public class StudentController {
    // 字符串的常量提取
    public static final String BASE_CONTROLLER = "/studentController";
    public static final String INDEX_PAGE = "../index.jsp";

    // 基本类型参数传递封装
    // 400 - 请求错误,原因:由于参数不匹配
    @RequestMapping(value = "getStuById", method = RequestMethod.GET)
    public String doLogin(Integer id, @RequestParam(value = "id3") Integer id2){

        // 服务器的处理
        System.out.println("正在查询------------查询完成,编号为" + id);
        System.out.println("正在查询------------查询完成,编号为" + id2);

        // 给出响应
        return INDEX_PAGE;

    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值