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
// 负责将控制器方法返回的字符串映射到某个JSP
public ViewResolver viewResolver() {
InternalResourceViewResolver resource = new InternalResourceViewResolver();
resource.setViewClass(JstlView.class);// 支持标准jsp和taglib
System.out.println("ds");
// 设置头和尾/WEB-INF/jsp/customer.jsp
resource.setPrefix("/WEB-INF/jsp/");
resource.setSuffix(".jsp");
return resource;
}
@Override
public 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
// 配置SessionFactory
public 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>