上文书说了Spring相关的知识,对Spring来了个浅入浅出,大家应该了解到,Spring在三层架构中主做Service层,那还有Web层,也就是Controller层,这个就由SpringMVC来解决了。SpringMVC是Web层的一个框架,...
上文书说了Spring相关的知识,对Spring来了个浅入浅出,大家应该了解到,Spring在三层架构中主做Service层,那还有Web层,也就是Controller层,这个就由SpringMVC来解决了。SpringMVC是Web层的一个框架,它是Spring的组成部分,可以先看看下面这张图:
MVC模式在之前我已经写过博客了《Web开发模式》,学SpringMVC之前有必要先看一下MVC模式。
SpringMVC框架主要由DispatcherServlet、处理器映射、控制器、视图解析器、视图组成,其工作流程如下:
客户端请求提交到DispatcherServlet;
由DispatcherServlet控制器寻找一个或多个HandlerMapping,找到处理请求的Controller;
DispatcherServlet将请求提交到Controller;
Controller调用业务逻辑处理后返回ModelAndView;
DispatcherServlet寻找一个或多个ViewResolver视图解析器,找到ModelAndView指定的视图;
视图负责将结果显示到客户端。
在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接口的类。传统风格的控制器不仅需要在配置文件中部署映射,而且只能编写一个处理方法,不够灵活。
基于注解的控制器具有以下两个优点:
在基于注解的控制器类中可以编写多个处理方法,进而可以处理多个请求(动作),这就允许将相关的操作编写在同一个控制器类中,从而减少控制器类的数量,方便以后的维护。
基于注解的控制器不需要在配置文件中部署映射,仅需要使用RequestMapping注释类型注解一个方法进行请求处理。
在SpringMVC中使用扫描机制找到应用中所有基于注解的控制器类,所以,为了让控制器类被SpringMVC框架扫描到,需要在配置文件中声明spring-context,并使用<context:component-scan/>元素指定控制器类的基本包(确保所有控制类都在基本包及其子包下)。示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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> |
说这么多,用实例说话吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | <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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | <?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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | <?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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 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 + '\ '' + '}' ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.my.dao; public interface UserDao { public void add(); public void delete(); public void update(); public void query(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 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); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.my.service; public interface UserService { public void add(); public void delete(); public void update(); public void query(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 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" ; } } |
来源:本文内容搜集或转自各大网络平台,并已注明来源、出处,如果转载侵犯您的版权或非授权发布,请联系小编,我们会及时审核处理。
声明:江苏教育黄页对文中观点保持中立,对所包含内容的准确性、可靠性或者完整性不提供任何明示或暗示的保证,不对文章观点负责,仅作分享之用,文章版权及插图属于原作者。
Copyright©2013-2024 JSedu114 All Rights Reserved. 江苏教育信息综合发布查询平台保留所有权利
苏公网安备32010402000125 苏ICP备14051488号-3技术支持:南京博盛蓝睿网络科技有限公司
南京思必达教育科技有限公司版权所有 百度统计