springmvc入门实例

代码执行流程:

 浏览器中访问路径为http://localhost:8080/itemList/itemList.action

.action被web.xml中配置的*.action拦截

*.action这个url-pattern对应servlet-name为springmvc

springmvc对应的servlet-class中配置了springmvc.xml这个配置文件

在springmvc.xml配置文件中配置了controller包

包中的controller类加了@Controller注解,所以会扫描这个类

            类中的hello方法配置的@RequestMapping为itemList,与地址栏访问的一致     

代码 

             jar:

springmvc入门实例_第1张图片

             pojo:                     

public class Item {

	/** 商品id */
	private Integer id;
	/** 商品名称 */
	private String name;
	/** 商品创建时间 */
	private Date createtime;
	/** 商品价格 */
	private Double price;
	/** 商品描述 */
	private String detail;
	
	public Item() {
	}

	public Item(Integer id, String name, Date createtime, String detail) {
		this.id = id;
		this.name = name;
		this.createtime = createtime;
		this.detail = detail;
	}

	
	public Double getPrice() {
		return price;
	}

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

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	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;
	}
}

 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"%>




查询商品列表

 
查询条件:
商品列表:
商品名称 商品价格 生产日期 商品描述 操作
${item.name } ${item.price } ${item.detail } 修改

   controller:

@Controller
public class ItemControll {

	@RequestMapping("itemList")
	public ModelAndView hello(){
	
		ModelAndView modelAndView = new ModelAndView();
		
		List list = Arrays.asList(new Item(1, "海尔", new Date(), "海尔制冷冰箱"),
										new Item(1, "格力", new Date(), "格力变频空调"),
										new Item(1, "美的", new Date(), "美的洗衣机"),
										new Item(1, "老板", new Date(), "老板吸油烟机"));
		modelAndView.addObject("itemList", list);
		//modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
		//springmvc.xml中配置视图解析器后这里就不需要前缀和后缀
		modelAndView.setViewName("itemList");
		
		return modelAndView;
	}
	
}

springmvc.xml配置文件




        
        
        
        
        
        
        
        
        
        
		
		
		
		
			
			
		

web.xml



  
	  springmvc
	  org.springframework.web.servlet.DispatcherServlet
	       
	             contextConfigLocation
	             classpath:springmvc.xml
	       
  
  
	  springmvc
	  *.action
  

springmvc架构 

springmvc入门实例_第2张图片

 

你可能感兴趣的:(springmvc入门实例)