SpringMVC--入门案例
一、SpringMVC的介绍
1、springmvc是spring框架体系中的子框架、子模块
2、Springmvc是用来实现和界面交互的(和jsp、html等等视图界面交互),web层的或者表现层的框架
3、Springmvc框架采用了MVC模式(可以分成模型层、视图层、控制层)
二、入门案例
1、创建项目
2、导入jar包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.27.RELEASE</version>
</dependency>
</dependencies>
3、视图界面—index.jsp
在src/main/webapp下新建一个index.jsp
<%@ 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>首页</title>
</head>
<body>
<!-- 入门案例:超链接 -->
<!-- 超链接的href属性用来设置一个路径,此路径就是请求资源路径,通过此路径请求资源,
要有一个地方,这个地方要接收请求,处理请求,做出响应 -->
<!-- 完整路径?http://localhost:8080/07-springmvc-start/first -->
<!-- localhost:8080/07-springmvc-start/xxxxx/first -->
<a href="${pageContext.request.contextPath }/first">入门案例</a>
</body>
</html>
4、处理当前请求的执行单元
用类加方法来实现
public class FirstController implements Controller{
//此方法就是处理某个请求的执行单元
//当前处理的是index.jsp页面中超链接请求
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//给用户响应数据、响应页面,在页面中显示数据
//准备数据---一般是从数据库查回来的
Country cou = new Country();
cou.setCid(1001);
cou.setCname("蜀国");
cou.setCaddress("成都");
ModelAndView mv = new ModelAndView();
mv.addObject("cou", cou);//把数据模型封装到mv中,等价request.setAttribute("cou",cou);
//处理视图 request.getRequestDispatcher("路径").forward();
String viewName = "/WEB-INF/views/cou.jsp";
mv.setViewName(viewName);
return mv;
}
}
5、新的数据展示页面
<%@ 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>展示国家信息</title>
</head>
<body>
${cou.cid }--${cou.cname }--${cou.caddress }
</body>
</html>
6、相关配置
6.1、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 配置前端控制器 -->
<!-- 实际上就是一个servlet配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.2、springmvc.xml配置文件配置
在Java Resource下的src/main/resources下新建一个springmvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 较全面的配置,后期变化过程中某些配置是可以省略的 -->
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!-- 后端控制器 -->
<!-- id是路径名 -->
<!-- 路径和类以及类中方法的精确匹配 -->
<bean id="/first" class="com.offcn.controller.FirstController"></bean>
</beans>