SpringMVC入门详解及案例

coding

Spring MVC浅入浅出——不吹牛逼不装逼

前言

上文书说了Spring相关的知识,对Spring来了个浅入浅出,大家应该了解到,Spring在三层架构中主做Service层,那还有Web层,也就是Controller层,这个就由SpringMVC来解决了。SpringMVC是Web层的一个框架,它是Spring的组成部分,可以先看看下面这张图:

 

SpringMVC工作原理

MVC模式在之前我已经写过博客了《Web开发模式》,学SpringMVC之前有必要先看一下MVC模式。

 

SpringMVC框架主要由DispatcherServlet、处理器映射、控制器、视图解析器、视图组成,其工作流程如下:

  1. 客户端请求提交到DispatcherServlet;
  2. 由DispatcherServlet控制器寻找一个或多个HandlerMapping,找到处理请求的Controller;
  3. DispatcherServlet将请求提交到Controller;
  4. Controller调用业务逻辑处理后返回ModelAndView;
  5. DispatcherServlet寻找一个或多个ViewResolver视图解析器,找到ModelAndView指定的视图;
  6. 视图负责将结果显示到客户端。

 

在SpringMVC工作流程中包含4个SpringMVC接口,即DispatcherServlet、HandlerMapping、Controller和ViewResolver。SpringMVC所有的请求都经过DispatcherServlet来统一分发,在DispatcherServlet将请求分发给Controller之前需要借助SpringMVC提供的HandlerMapping定位到具体的Controller。

 

HandlerMapping接口负责完成客户请求到Controller映射。

 

Controller接口将处理用户请求,这和Java 中Servlet扮演的角色是一致的。一旦Controller处理完用户请求,将返回ModelAndView对象给DispatcherServlet前端控制器,ModelAndView中包含了模型(Model)和视图(View)。

 

从宏观角度考虑,DispatcherServlet是整个Web应用的控制器;从微观考虑,Controller是单个Http请求处理过程中的控制器,而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)。

 

 

ViewResolver接口(视图解析器)在Web应用中负责查找View对象,从而将相应结果渲染给客户。

基于注解的控制器

在使用Web应用开发时Controller是Web应用的核心,Controller实现类包含了对用户请求的处理逻辑,是用户请求和业务逻辑之间的桥梁就,是SpringMVC框架的核心部分,负责具体的业务逻辑处理。传统风格的控制器是实现Controller接口的类。传统风格的控制器不仅需要在配置文件中部署映射,而且只能编写一个处理方法,不够灵活。

 

基于注解的控制器具有以下两个优点:

  1. 在基于注解的控制器类中可以编写多个处理方法,进而可以处理多个请求(动作),这就允许将相关的操作编写在同一个控制器类中,从而减少控制器类的数量,方便以后的维护。
  2. 基于注解的控制器不需要在配置文件中部署映射,仅需要使用RequestMapping注释类型注解一个方法进行请求处理。

 

在SpringMVC中使用扫描机制找到应用中所有基于注解的控制器类,所以,为了让控制器类被SpringMVC框架扫描到,需要在配置文件中声明spring-context,并使用<context:component-scan/>元素指定控制器类的基本包(确保所有控制类都在基本包及其子包下)。示例如下:

<?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:tx="http://www.springframework.org/schema/tx"

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.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

">

<!--配置注解要扫描的包-->

<context:component-scan base-package="com.my"></context:component-scan>

<!--配置视图解析器-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--配置前缀-->

<property name="prefix" value="/"></property>

<!--配置后缀-->

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

  

 

说这么多,用实例说话吧

实例解说

Pom.xml

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.38</version>

</dependency>

<!--spring核心依赖-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

</dependencies>

  

spring-config.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:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

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.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

">

<!--配置注解要扫描的包-->

<context:component-scan base-package="com.my"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<!--配置spring-jdbcTemplate-->

<!--配置数据源-->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<!--MySQL数据库驱动-->

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<!--连接数据库的URL-->

<property name="url" value="jdbc:mysql://localhost:3306/bbb?useUnicode=true&characterEncoding=UTF-8"></property>

<!--连接数据库的用户名-->

<property name="username" value="root"></property>

<!--连接数据库的密码-->

<property name="password" value="root"></property>

</bean>

<!--配置JDBC模板-->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!--配置事务-->

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!--注册事务注解驱动-->

<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>

<!--配置视图解析器-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--配置前缀-->

<property name="prefix" value="/"></property>

<!--配置后缀-->

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

  

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app>

<!--部署DispatcherServlet-->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--表示容器在启动时立即加载servlet-->

<load-on-startup>1</load-on-startup>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-config.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<!--处理所有URL-->

<url-pattern>/</url-pattern>

</servlet-mapping>

<!--处理中文乱码-->

<filter>

<filter-name>encodingFilter</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>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!--设置访问静态资源-->

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.css</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.js</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.jpg</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.png</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.gif</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.mp3</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.mp4</url-pattern>

</servlet-mapping>

</web-app>

  

User

package com.my.pojo;

public class User {

private int id;

private String username;

private String password;

public User() {

}

public User(int id, String username, String password) {

this.id = id;

this.username = username;

this.password = password;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Override

public String toString() {

return "User{" +

"id=" + id +

", username='" + username + '\'' +

", password='" + password + '\'' +

'}';

}

}

  

UserDao

package com.my.dao;

public interface UserDao {

public void add();

public void delete();

public void update();

public void query();

}

  

UserDaoImpl

package com.my.dao.impl;

import com.my.dao.UserDao;

import com.my.pojo.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userDao")

public class UserDaoImpl implements UserDao {

@Autowired

//使用配置文件中的JDBC模板

private JdbcTemplate jdbcTemplate;

@Override

public void add() {

String insertSql = "insert into user values(null,?,?)";

for (int i=0;i<15;i++){

Object parem1[] = {"成功","123456"};

jdbcTemplate.update(insertSql,parem1);

}

System.out.println("UserDao中的添加功能实现了");

}

@Override

public void delete() {

String deleteSql = "delete from user where id=?";

Object parem2[] = {5};

jdbcTemplate.update(deleteSql,parem2);

System.out.println("UserDao中的删除功能实现了");

}

@Override

public void update() {

String updateSql = "update user set username=? , password=? where id =?";

Object parem3[] = {"修改","654321",3};

jdbcTemplate.update(updateSql,parem3);

System.out.println("UserDao中的修改功能实现了");

}

@Override

public void query() {

String selectSql = "select * from user";

RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);

List<User> list = jdbcTemplate.query(selectSql,rowMapper,null);

System.out.println("UserDao中的查询功能实现了");

for (User user : list){

System.out.println(user);

}

}

}

  


UserService

package com.my.service;

public interface UserService {

public void add();

public void delete();

public void update();

public void query();

}

  

UserServiceImpl

package com.my.service.impl;

import com.my.dao.UserDao;

import com.my.dao.impl.UserDaoImpl;

import com.my.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserDao userDao ;

@Override

public void add() {

userDao.add();

}

@Override

public void delete() {

userDao.delete();

}

@Override

public void update() {

userDao.update();

}

@Override

public void query() {

userDao.query();

}

}

  

UserController

package com.my.controller;

import com.my.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Controller;

import org.springframework.stereotype.Service;

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

@Controller

@RequestMapping("/user")

public class UserController {

@Autowired

private UserService userService;

@RequestMapping("/add")

public String add(){

userService.add();

return "index";

}

@RequestMapping("/delete")

public String delete(){

userService.delete();

return "index";

}

@RequestMapping("/update")

public String update(){

userService.update();

return "index";

}

@RequestMapping("/query")

public String query(){

userService.query();

return "index";

}

}

  

测试结果

 

 

结束

欲看下文如何,请看下回讲解。

*****************************************************************************************************

我的博客园地址:https://www.cnblogs.com/zyx110/

  本文已独家授权给脚本之家(jb51net)公众号独家发布

原文出处:https://www.cnblogs.com/zyx110/p/11319843.html

以上是 SpringMVC入门详解及案例 的全部内容, 来源链接: utcz.com/z/510239.html

回到顶部