SpringMVC学习笔记——第一个Demo

一、创建web项目,并配置工程目录

lib中存放所需的包

二、编写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_3_0.xsd" 
		 id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_First</display-name>
  
  <!-- 配置springmvc前端控制器 -->
  <servlet>
  		<servlet-name>springmvc</servlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		<!-- 配置springmvc加载的配置文件contextConfigLocation(处理器映射器,处理器适配器等)
  			如果不配置,默认路径/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>
  	<url-pattern>*.action </url-pattern>
  </servlet-mapping>
  
  
  
  <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>
</web-app>

三、创建Handler

这是一个类,实现了Controller接口,用来根据HandlerAdapter的需求,编写相应程序,也是程序员需要编写两个文件之一,类似之前web开发时的Servlet。

package cn.zhao.ssm.controller;

import java.util.ArrayList;
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 cn.zhao.ssm.pojo.Items;

public class ItemController1 implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		
		//这里没有使用数据库,所以使用List集合来模拟数据库操作
		List<Items> itemsList = new ArrayList<>();
		//集合中填充数据
		Items items_1 = new Items();
		items_1.setName("联想笔记本");
		items_1.setPrice(6000f);
		items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
		
		Items items_2 = new Items();
		items_2.setName("苹果手机");
		items_2.setPrice(5000f);
		items_2.setDetail("iphone6苹果手机!");
		
		itemsList.add(items_1);
		itemsList.add(items_2);

		//返回ModelAndView
		ModelAndView moderAndView = new ModelAndView();
		//相当于Servlet中request.setAttribut(),向域中存储数据,在jsp中使用
		moderAndView.addObject("itemsList", itemsList);
		//转发到jsp页面
		moderAndView.setViewName("/WEB-INF/jsp/order/itemsList.jsp");
		
        //返回ModelAndView,给视图解析器解析
		return moderAndView;
	}

}

四、创建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 }/item/queryItem.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="${itemsList }" 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 }/item/editItem.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

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

</html>

五、配置springmvc.xml

这个文件就是spring学习时的ApplicationContext.xml配置文件,在里面配置处理器映射器,处理器适配器,处理器和视图解析器

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
		
		<!-- 注册Handler -->
		<bean name="/queryItems.action" class="cn.zhao.ssm.controller.ItemController1"/>
		
		
		<!-- 处理器适配器 
			 所有适配器实习HandlerAdapter接口,根据supports()方法实习Controller接口的Handler -->
		<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
		
		<!-- 处理器映射器
			使用beanname作为url进行查找,需要在handler配置时指定url -->
		<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
		
		<!-- 视图解析器
			解析jsp页面,自带jstl,需要加入jstl的jar包 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>

这样就全部配置完毕了,需要程序员编写的只有视图View和处理器Handler

六、发布调试

发布到tomcat中,在浏览器地址栏输入url:http://localhost:8080/Springmvc_begin/queryItems.action

这样,SpringMVC的第一个简单Demo就完成了,可以开始下面更深度的学习了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值