SpringMVC基础(一)
来源:互联网 发布:mysql 如何使用 编辑:程序博客网 时间:2024/06/01 09:56
1.SpringMVC概述
(1)SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器无需实现任何借口
(2)支持REST风格的URI请求,
(3)采用松散耦合可拔插主键结构,比其他MVC结构更加灵活可扩展。
2.基本步骤
(1)导入jar包
(2)在web.xml里面配置DispatcherServlet
(3)加入SpringMVC的配置文件
(4)编写处理请求的处理器,并表标识为处理器
(5)编写视图
【1】jar包:除了基本的6个spring需要的包(日志,aop,bean,context,核心core,expression)以外还需导入
Spring-web-release.jar,spring-webmvc-release.jar(这里没有指定版本,但版本最好一致)。
纯java的方式配置代替web.xml的SpringMVC的配置文件
@Configuration//加入配置@ComponentScan("com.andy") //扫描注解@EnableTransactionManagement// 配置事务管理@PropertySource("classpath:jdbc.properties")@EnableWebMvc// extends WebMvcConfigurerAdapter 开启常用的支持:包括静态资源public class AppConfig extends WebMvcConfigurerAdapter {@Bean// 负责将控制器方法返回的字符串映射到某个JSPpublic ViewResolver viewResolver() {InternalResourceViewResolver resource = new InternalResourceViewResolver();resource.setViewClass(JstlView.class);// 支持标准jsp和taglibSystem.out.println("ds");// 设置头和尾/WEB-INF/jsp/customer.jspresource.setPrefix("/WEB-INF/jsp/");resource.setSuffix(".jsp");return resource;}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// /assets/css/app.css -> /WebContent/public/css/app.css// /assets/js/app.js -> /WebContent/public/js/app.js// /WebContent/public/registry.addResourceHandler("/assets/**").addResourceLocations("/public/");//将/public替换为/"/assets/**" 用于匹配资源}@Bean// 配置事务管理public PlatformTransactionManager transactionalManager(SessionFactory sessionFactory) {HibernateTransactionManager transactionalManager = new HibernateTransactionManager();transactionalManager.setSessionFactory(sessionFactory);return transactionalManager;}@Bean// 配置SessionFactorypublic LocalSessionFactoryBean sessionFactory(DataSource dataSource) {LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();sessionFactory.setDataSource(dataSource); //加载数据源sessionFactory.setPackagesToScan("com.andy.entity");//注册实体return sessionFactory;}@Bean// 配置数据源、public DataSource dataSource(Environment env) {DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();driverManagerDataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));driverManagerDataSource.setUrl(env.getProperty("jdbc.url"));driverManagerDataSource.setUsername(env.getProperty("jdbc.username"));driverManagerDataSource.setPassword(env.getProperty("jdbc.password"));return driverManagerDataSource;}}
1.这里配置一个基础的处理请求示例:(xml 方式实现)
@Controller@RequestMapping(value="/restservice")public class RestService { public final static String SUCCEEDD="show"; /** * get请求 * url: http://localhost:8080/springmvc/restservice/testRestGet/12 * @param id * 查询的参数 * @return */ @RequestMapping(value="/testRestGet/{id}",method=RequestMethod.GET) public String testRestGet(@PathVariable("id") Integer id){ System.out.println("rest 风格的GET请求..........id=" +id); return SUCCEEDD; } /** * post新增 * url: http://localhost:8080/springmvc/restservice/testRestPost * @return */ @RequestMapping(value="/testRestPost",method=RequestMethod.POST) public String testRestPost(){ System.out.println("rest 风格的POST请求.......... "); return SUCCEEDD; } /** * PUT 修改操作 * url: http://localhost:8080/springmvc/restservice/testRestPut/put123 * @param name * @return */ @RequestMapping(value="/testRestPut/{name}",method=RequestMethod.PUT) public String testRestPut(@PathVariable("name") String name){ System.out.println("rest 风格的PUT请求..........name="+name); return SUCCEEDD; } /** * DELETE删除操作 * url: http://localhost:8080/springmvc/restservice/testRestDelete/11 * @param id * @return */ @RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id){ System.out.println("rest 风格的DELETE请求..........id="+id); return SUCCEEDD; } }
4 编写接口的响应页面 -对应着接口的return
<%@ 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>Insert title here</title></head><body> <h2>show this is succeedd ? yes </h2></body></html>
5 发布和配置rest接口 dispatcherServlet-servlet.xml:
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: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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定扫描的包 --> <context:component-scan base-package="cn.bean.demo"></context:component-scan> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>纯java方式实现视图转发、事务管理、数据源连接、SessionFactory的配置
@Controller@RequestMapping(value="/restservice")public class RestService { public final static String SUCCEEDD="show"; /** * get请求 * url: http://localhost:8080/springmvc/restservice/testRestGet/12 * @param id * 查询的参数 * @return */ @RequestMapping(value="/testRestGet/{id}",method=RequestMethod.GET) public String testRestGet(@PathVariable("id") Integer id){ System.out.println("rest 风格的GET请求..........id=" +id); return SUCCEEDD; } /** * post新增 * url: http://localhost:8080/springmvc/restservice/testRestPost * @return */ @RequestMapping(value="/testRestPost",method=RequestMethod.POST) public String testRestPost(){ System.out.println("rest 风格的POST请求.......... "); return SUCCEEDD; } /** * PUT 修改操作 * url: http://localhost:8080/springmvc/restservice/testRestPut/put123 * @param name * @return */ @RequestMapping(value="/testRestPut/{name}",method=RequestMethod.PUT) public String testRestPut(@PathVariable("name") String name){ System.out.println("rest 风格的PUT请求..........name="+name); return SUCCEEDD; } /** * DELETE删除操作 * url: http://localhost:8080/springmvc/restservice/testRestDelete/11 * @param id * @return */ @RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id){ System.out.println("rest 风格的DELETE请求..........id="+id); return SUCCEEDD; } }
4 编写接口的响应页面 -对应着接口的return
<%@ 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>Insert title here</title></head><body> <h2>show this is succeedd ? yes </h2></body></html>
5 发布和配置rest接口 dispatcherServlet-servlet.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: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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定扫描的包 --> <context:component-scan base-package="cn.bean.demo"></context:component-scan> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean></bean>
@Controller@RequestMapping(value="/restservice")public class RestService { public final static String SUCCEEDD="show"; /** * get请求 * url: http://localhost:8080/springmvc/restservice/testRestGet/12 * @param id * 查询的参数 * @return */ @RequestMapping(value="/testRestGet/{id}",method=RequestMethod.GET) public String testRestGet(@PathVariable("id") Integer id){ System.out.println("rest 风格的GET请求..........id=" +id); return SUCCEEDD; } /** * post新增 * url: http://localhost:8080/springmvc/restservice/testRestPost * @return */ @RequestMapping(value="/testRestPost",method=RequestMethod.POST) public String testRestPost(){ System.out.println("rest 风格的POST请求.......... "); return SUCCEEDD; } /** * PUT 修改操作 * url: http://localhost:8080/springmvc/restservice/testRestPut/put123 * @param name * @return */ @RequestMapping(value="/testRestPut/{name}",method=RequestMethod.PUT) public String testRestPut(@PathVariable("name") String name){ System.out.println("rest 风格的PUT请求..........name="+name); return SUCCEEDD; } /** * DELETE删除操作 * url: http://localhost:8080/springmvc/restservice/testRestDelete/11 * @param id * @return */ @RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id){ System.out.println("rest 风格的DELETE请求..........id="+id); return SUCCEEDD; } }
4 编写接口的响应页面 -对应着接口的return
<%@ 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>Insert title here</title></head><body> <h2>show this is succeedd ? yes </h2></body></html>
5 发布和配置rest接口 dispatcherServlet-servlet.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: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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定扫描的包 --> <context:component-scan base-package="cn.bean.demo"></context:component-scan> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
阅读全文
0 0
- SpringMVC(一、SpringMVC基础)
- SpringMVC基础(一)
- SpringMVC 基础笔记(一)
- SpringMVC基础(一)开发环境搭建
- SpringMVC之http协议基础(一)
- SpringMVC-基础知识点(一)
- 一、SpringMvc基础
- 一、springMVC基础实例
- springmvc基础一
- springmvc+hibernate基础学习一
- SpringMVC学习:一、基础概念
- 注解型SpringMvc为基础的框架设计(一)
- maven+springMVC+hibernate项目搭建(一基础环境配置)
- 搭建springmvc项目(一)——基础搭建
- SpringMVC核心一(Spring web mvc基础)
- SpringMVC入门(一)
- springMVC学习(一)
- SpringMVC(一)
- 代码理解json.dumps(config, sort_keys=True, indent=4, separators=(',', ':')
- 互联网产品常用英语单词
- python正则表达式(二)
- Spring RMI java.rmi.NoSuchObjectException: no such object in table
- 用类描述计算机中CPU的速度和硬盘的容量。要求Java的应用程序有4个类,名字分别是PC、CPU、HardDisk和Test,其中Test是主类。
- SpringMVC基础(一)
- 线程2
- webmagic总结
- rethinkdb踩坑记录
- 设计模式之单例模式
- Python中进程和线程的区别详解
- 二叉树——如何使用三种遍历中的两种推断出二叉树
- [LeetCode] 92. Reverse Linked List II
- VS2015配置EasyX图形库