目录结构:

contents structure [-]

MVC的全称是Model View Controller,通过实现MVC框架可以很好的数据、视图、业务逻辑进行分离。Spring MVC也是一种MVC框架,它是SpringFrameWork的后续产品,同样需要Spring的Jar包才能运行起来。

SpringMVC工作原理

SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的就是DispatcherServlet,DispathcerServlet负责将每一个request转发到相应的Controller,Controller处理后再返回模型(Model)和视图(View)。在使用注解的SpringMVC中,处理器Controller是基于@Controller和@RequestMapping进行注解的。

【Spring】SpringMVC之基于注解的实现SpringMVC+MySQL

@Controller和@RequestMapping注解

@Controller注解类型

声明一个控制器类,DispatcherServlet会通过扫描机制找到该类,如下是一个@Controller的例子。

package com.example.controller;

import org.springframework.stereotype.Controller;

@Controller
public class ProductController {

}

为了保证DispatcherServlet能够扫描到该Controller,需要在DispatcherServlet中配置注解驱动和上下文扫描包。

上下文扫描包:

……
    xmlns:context=http://www.springframework.org/schema/context
……

注解驱动包:

……
    xmlns:mvc=http://www.springframework.org/schema/mvc
……

然后应用<mvc:annotation-driven>和<context:component-scan/>元素:

......
<
mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package=""></context:component-scan>
......

@RequestMapping注解类型

该注解类型在控制器类的内部定义每一个动作相应的处理方法,一个采用@RequestMapping注释的方法将成为一个请求处理方法,并由调度程序在接收到对应的URL请求时调用,下面是一个RequestMapping注解方法的控制器类。

@Controller
public class ProductController {
    
    @RequestMapping(value="/productInput")
    public String inputProduct(){
        //do something here
        return "ProductForm";
    }
}

使用requestMapping注解的value属性将URL映射到方法,在上面的例子中通过 http://localhost:8080/SpringMVC/productInput访问inputProduct方法。RquestMapping除了有value属性外,还有许多属性,比如method、consumes、header、path等等。例如当HTPP POST或PUT方法时才调用test()方法。

@RequestMapping(value="/order_process", method={RequestMethod.POST, RequestMethod.PUT})
public String test(){
    //do something here
    return "viewpage";
}

如果method属性只有一个HTTP方法值,则无需{},直接为method=RequestMethod.POST,如果未指定method属性,则请求处理方法可以处理任意HTTP方法。此外RequestMapping注释类型也可以用来注释一个控制器类。

import org.springframework.stereotype.Controller;
...

@Controller
@RequestMapping(value="/customer")
public class CustomerController{
    @RequestMapping(value="/delete", method={RequestMethod.POST, RequestMethod.PUT})
    public String deleteCustomer(){
        //do something here
        return “viewpage”;
    }
}

在这种情况下,所有的方法都将映射为相对于类级别的请求,如例子中的deleteCustomer方法,由于控制器类映射使用"/customer",而deleteCustomer方法映射为"/delete",则需要通过 http://localhost:8081/SpringMVC/customer/delete 访问。

基于注解的SpringMVC+MySQL

项目目录结构:【Spring】SpringMVC之基于注解的实现SpringMVC+MySQL

login.jsp类

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>login</title>
 8 </head>
 9 <body>
10     <form action="select.do" method="post">
11         <input type="submit" value="查询所有信息"/>
12     </form>
13 </body>
14 </html>
login.jsp

相关文章:

  • 2022-01-01
  • 2022-12-23
  • 2021-12-28
  • 2022-01-13
  • 2022-02-06
  • 2021-06-11
  • 2021-07-29
猜你喜欢
  • 2021-12-19
  • 2021-07-08
  • 2021-07-03
  • 2022-12-23
  • 2022-02-19
  • 2021-12-31
相关资源
相似解决方案