SpringMVC学习(二) SpringMVC搭建

1. 创建web工程

2. 导入相关jar包

       

3. 加入配置文件(web.xml、springmvc.xml)

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

<!--配置前端控制器DispatcherServlet-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!--加载springmvc.xml配置文件,没有配置默认找/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>
    <!--
        /*  拦截所有请求
        /   拦截除jsp以外所有请求
        *.action *.do  拦截以action do结尾的请求
    -->
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

 (2)在classpath目录下创建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">

    <!--
        @RequestMapping(value="url") 配置在方法上,请求对应的处理方法Handler
    -->
	
	<!-- 配置controller扫描包(此处为扫描com.test包及其子包下的所有类) -->
	<context:component-scan base-package="com.test"></context:component-scan>
</beans>

4. 加入jsp页面

<!--部分代码-->
<c:forEach items="${list }" 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>

5. 后台实现(创建pojo和controller)

//类上添加注解@Controller
// @RequestMapping:里面放的是请求的url,和用户请求的url进行匹配
// action可以写也可以不写
@RequestMapping(value = "/item/itemList.action")
public ModelAndView itemList() {
	// 创建页面需要显示的商品数据
	List<Item> list = new ArrayList<Item>();
	list.add(new Item(1, "1华为 荣耀8", 2399, new Date(), "质量好!1"));
	list.add(new Item(2, "2华为 荣耀8", 2399, new Date(), "质量好!2"));
	list.add(new Item(3, "3华为 荣耀8", 2399, new Date(), "质量好!3"));
	// 创建ModelAndView,用来存放数据和视图
	ModelAndView mav = new ModelAndView();
	// 设置数据到模型中,在页面上可以用key值(此处为list)取出对应的数据
	mav.addObject("list", list);
	// 设置视图jsp,需要设置视图的地址
	mav.setViewName("/WEB-INF/itemList.jsp");
	return mav;
}

6.测试

   加入Tomcat服务器,输入url(http://localhost:8080/工程名/item/itemList.action)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值