Springmvc学习笔记(一)--什么是springmvc,springmvc入门

版权声明:版权归JansonLin所有,转载请标明出处。 https://blog.csdn.net/Janson_Lin/article/details/84338715

一.什么是Springmvc?

Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来,如下图:

二.springmvc处理流程

三.springmvc入门程序

3.1导入jar包

3.2编写配置文件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">
    	
    	<!-- 包扫描 -->
		<context:component-scan base-package="com.janson"/>        
        
        <!-- 处理器映射器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
        <!-- 处理器适配器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
        <!-- 
        	注解驱动 
        	这里配置的注解驱动,相当于上面配置的处理映射器和处理适配器一样的
        -->
    	<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>

3.3配置前端控制器

在web.xml中,配置SpringMVC的前端控制器DispatcherServlet。

<?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</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>
  
  
  <!-- 前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 默认加载资源 -->
  	<!-- 默认找 /WEB-INF/[servlet的名称]-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>
  	<!-- 
  		1. /*  拦截所有   jsp  js png .css  真的全拦截   建议不使用
  		2. *.action *.do 拦截以do action 结尾的请求     肯定能使用   ERP  oa  后端系统使用
  		3. /  拦截所有 (不包括jsp) (包含.js .png.css)  强烈建议使用     前台 面向消费者  www.jd.com/search   /对静态资源放行
  	 -->
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
 
</web-app>

3.4创建pojo类

这里的pojo类是一Items为例子。(这里的创建pojo类可以使用逆向工程来生成)

package cn.janson.po;

import java.util.Date;

public class Items {
    private Integer id;
    private String name;
    private Float price;
    private String pic;
    private Date createtime;
    private String detail;
    public Items() {}
    public Items(Integer 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;
	}
...省略get/set
}

3.5编写controller.java

package com.janson.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 cn.janson.po.Items;

@Controller
public class ItemController {
	@RequestMapping("/item/itemlist.action")
	public ModelAndView listItem() {
		// 创建页面需要显示的商品数据
		List<Items> list = new ArrayList<Items>();
		list.add(new Items(1, "1华为 荣耀p20", 2399f, new Date(), "质量好!1"));
		list.add(new Items(2, "2华为 荣耀p20", 2399f, new Date(), "质量好!2"));
		list.add(new Items(3, "3华为 荣耀p20", 2399f, new Date(), "质量好!3"));
		list.add(new Items(4, "4华为 荣耀p20", 2399f, new Date(), "质量好!4"));
		list.add(new Items(5, "5华为 荣耀p20", 2399f, new Date(), "质量好!5"));
		list.add(new Items(6, "6华为 荣耀p20", 2399f, new Date(), "质量好!6"));
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemList", list);
		modelAndView.setViewName("itemList");
		return modelAndView;
	}
}

3.5 访问数据页面

http://127.0.0.1:8080/springMvc/itemList.action

 测试数据的效果页面(没有做页面 ,只是展示数据, 很丑!!)

猜你喜欢

转载自blog.csdn.net/Janson_Lin/article/details/84338715