springMVC_day01_入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35537301/article/details/82455284

springMVC是什么?

概念

  • 表现层框架作用
    • 接收参数
    • 返回响应
  • Sprig Web MVC和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来,如下图:

springMVC执行流程

 用户请求——>前端控制器DispatcherServlet——>处理器映射器:判断你这个映射器是什么形式开发的(注解形式)——>根据URL请求找映射器——>返回方法和类——>前端控制器DispatcherServlet——>处理器适配器:判断类是什么形式开发的(注解形式开发的)——>Handler处理器,执行方法,返回ModelAndView——>处理器适配器:作用不认识ModelAndView——>DispatcherServlet(ModelAndView)——>视图解析器,根据View地址找到jsp页面,解析jsp填充数据,整合页面返回View对象(完整的一个jsp页面)——>DispatcherServlet(View对象)——>tomcat显示

 struts2和springMVC执行流程的区别

SpringMVC的架构

SpringMVC中的三大组件

  • 处理器映射器
  • 处理器适配器
  • 视图解析器

入门案例

 导入jar包

XML形式配置

配置前端控制器(web.xml)

url-pattern参数配置说明

  • /*:所有的都拦截,包括jsp页面 (springmvc中坚决不能使用)
  • / :不拦截jsp,但是会拦截js,css等渲染文件,使用此配置时需要设置不拦截某些资源
  • *.action:只拦截以.action结尾的请求路径,企业中推荐使用
<?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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>springmvc_day01</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!-- 配置SpringMVC前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 指定SpringMVC核心配置文件 -->
		<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- 设置所有以action结尾的请求进入SpringMVC -->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

</web-app>

加载配置文件(springmvc.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:p="http://www.springframework.org/schema/p"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 基于配置式的springmvc开发 -->
	<!-- 配置处理器映射器 -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/items.action">itemsController</prop>
			</props>
		</property>
	</bean>

	<!-- 配置处理器适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

	<!-- 配置视图解析器 -->
	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 实例化controller类 -->
	<bean id="itemsController" class="com.itheima.controller.ItemController"></bean>

</beans>

实体类

package com.itheima.pojo;

import java.util.Date;

public class Item {
	// 商品id
	private int id;
	// 商品名称
	private String name;
	// 商品价格
	private Float price;
	// 商品创建时间
	private Date createtime;
	// 商品描述
	private String detail;

	public Item() {
		super();
	}

	public Item(int id, String name, Float price, Date createtime, String detail) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.createtime = createtime;
		this.detail = detail;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	@Override
	public String toString() {
		return "Item [id=" + id + ", name=" + name + ", price=" + price + ", createtime=" + createtime + ", detail="
				+ detail + "]";
	}

}

controller

package com.itheima.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.itheima.pojo.Item;

public class ItemController implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 创建页面需要显示的商品数据
		List<Item> list = new ArrayList<>();
		list.add(new Item(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
		list.add(new Item(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
		list.add(new Item(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
		list.add(new Item(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
		list.add(new Item(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
		list.add(new Item(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));

		// 创建ModelAndView,用来存放数据和视图
		ModelAndView modelAndView = new ModelAndView();
		// 设置数据到模型中
		modelAndView.addObject("itemList", list);
		// 设置视图jsp,需要设置视图的物理地址
		modelAndView.setViewName("itemList");

		return modelAndView;

	}

}

itemList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!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> 
<form action="${pageContext.request.contextPath }/itemList.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
	<td>${item.name }</td>
	<td>${item.price }</td>
	<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${item.detail }</td>
	
	<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>

访问路径

http://localhost:8080/springmvc_day01/items.action

结果

注解形式配置

springmvc.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:p="http://www.springframework.org/schema/p"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 配置扫描@Controller注解 -->
	<context:component-scan base-package="com.itheima.controller"/>

	<!-- 注解驱动:自动把当前系统最新版本的注解形式的处理器映射器和处理器适配器加载 -->
	<mvc:annotation-driven/>
	
	<!-- 配置视图解析器 
		帮我们简化代码:
		前缀 + 视图名称 + 后缀
	-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 设置前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<!-- 设置后缀 -->
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>

controller

package com.itheima.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.itheima.pojo.Item;

@Controller
public class ItemController{

	@RequestMapping("items.action")
	public ModelAndView list(){
		// 创建页面需要显示的商品数据
		List<Item> list = new ArrayList<>();
		list.add(new Item(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
		list.add(new Item(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
		list.add(new Item(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
		list.add(new Item(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
		list.add(new Item(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
		list.add(new Item(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));

		// 创建ModelAndView,用来存放数据和视图
		ModelAndView modelAndView = new ModelAndView();
		// 设置数据到模型中
		modelAndView.addObject("itemList", list);
		// 设置视图jsp,需要设置视图的物理地址
		modelAndView.setViewName("itemList");

		return modelAndView;

	}

}

访问路径

http://localhost:8080/springmvc_day01/items.action

结果

猜你喜欢

转载自blog.csdn.net/qq_35537301/article/details/82455284