SpringMVC快速入门

SpringMVC快速入门

1.创建web项目工程
2.导入springmvc所需要的jar包
3.编写HelloControll.java
4.创建hello.jsp页面
5.创建与配置Springmvc.xml的核心控制器
6.在web.xml中配置前端控制器
7.在tomcat中发布并在浏览器中测试

1.创建动态工程,目录如下

在这里插入图片描述

3.HelloControl.java的内容
package springMVC.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller  //表明把这个类交给Spring管理
public class HelloControll {
//绑定请求地址,其中.action可以加也可以不加
	@RequestMapping("hello")
	public ModelAndView hello() {
		System.out.println("hello SpringMVC1...");
		//创建ModelAndView对象,用来存放数据和视图
		ModelAndView mav = new ModelAndView();
		//设置模型数据,用于传递到jsp
		mav.addObject("msg", "hello SpringMVC2....");
		//设置视图名字,需要设置视图的物理地址,用于响应用户
		mav.setViewName("/WEB-INF/jsp/hello.jsp");
		return mav;
	}
}
hello.jsp的代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title here</title>
</head>
<body>${msg }
</body>
</html>
5.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处理器,包扫描器 ,这是Spring的功能-->
	<context:component-scan base-package="springMVC.controller" />
</beans>
6.web.xml中配置前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<!--配置SpringMVC的核心前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 指定SpringMVC的配置文件位置,加载Springmvc核心配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>
	<!-- 配置拦截器 ,是所有.action的请求才能进入SpringMVC-->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>

接下来打开浏览器测试即可
在这里插入图片描述
到此入门案例结束。

二、案例商品列表
  • 1.Item的POJO
package springMVC.pojo;

import java.util.Date;

public class Item {

	private int id;
	private String name;
	private double price;
	private Date createtime;
	private String 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 double getPrice() {
		return price;
	}

	public void setPrice(double 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 + "]";
	}

	public Item() {
		super();
	}

	public Item(int id, String name, double price, Date createtime, String detail) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.createtime = createtime;
		this.detail = detail;
	}
}
  • 2.Item的控制层ItemController
package springMVC.controller;

import java.util.Arrays;
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 springMVC.pojo.Item;
//控制器,模拟查询商品列表
@Controller
public class ItemController {
	@RequestMapping("itemList")
	public ModelAndView itemList() {
		ModelAndView mav = new ModelAndView();
		List<Item> list = Arrays.asList(
				new Item(1, "冰箱1", 1999, new Date(), "冰箱质量还可以的哦"),
				new Item(2, "冰箱2", 1999, new Date(), "冰箱质量还可以的哦"), 
				new Item(3, "冰箱3", 1999, new Date(), "冰箱质量还可以的哦"),
				new Item(4, "冰箱5", 1999, new Date(), "冰箱质量还可以的哦"));
		mav.addObject("itemList", list);
		mav.setViewName("itemList");
		return mav;
	}
}
  • 3.配置视图层
<%@ 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 }/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="${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>
  • 4.配置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="springMVC.controller" />
	<!-- 配置处理器映射器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> -->
	<!--配置处理器适配器  -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> -->
	<!--配置注解驱动,相当于同时使用最新的处理器,映射器和适配器 ,对json数据响应提供支持 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!--视图解析器的配置  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/jsp/"></property>
	<property name="suffix" value=".jsp"></property>
	</bean>	
</beans>
  • 5.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<!--核心控制器的配置 -->
	<servlet>
		<servlet-name>springmvc</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>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>
  • 6.访问:http://localhost:8080/springMVC-1/itemList.action进行访问。
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值