SpringMvc的入门案例

本文介绍如何配置SpringMVC框架的基本环境,包括前端控制器web.xml和核心配置文件springmvc.xml的设置,并通过示例展示了处理器的开发过程及视图的呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发环境

配置前端控制器web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="3.0"

   xmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <!-- SpringMvc前端控制器 -->

  <servlet>

  <servlet-name>springmvc</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器,适配器)

      如果不配置,则默认加载:/WEB-INF/springmvc_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.*.action.action

  2./ 全部

   -->

  <url-pattern>*.action</url-pattern>

  </servlet-mapping>

</web-app>

配置springmvc.xml核心文件

<beansxmlns="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 ">

      <!-- 配置Handle -->

     <beanname="/queryItems.action"class="com.cloud.ctroller.ItemController1"/>

      <!-- 处理器映射器 -->

     <beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

      <!-- 处理器适配器

        必须实现:HandlerAdapter接口

      -->

     <beanclass="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

      <!-- 试图解析器

        默认使用jstl标签解析JSP页面,所以要导入jstl

      -->

     <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>


编写一个PO类

package com.cloud.po;

import java.util.Date;

publicclass Items {

    private Integerid;

    private Stringname;

    private Floatprice;

    private Stringpic;

    private Datecreatetime;

    private Stringdetail;

    //此处省略get()和set()方法

}


开发处理器

package com.cloud.ctroller;

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 com.cloud.po.Items;

/**

 * 实现Controller接口的处理器

 * @author Administrator

 *

 */

publicclass ItemController1 implements Controller{

   @Override

   public ModelAndView handleRequest(HttpServletRequest request,

         HttpServletResponse response)throws Exception {

      //创建几条静态数据

      List<Items> itemsList = new ArrayList<Items>();

      //list中填充静态数据    

      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 modelAndView = new ModelAndView();

      modelAndView.addObject("itemsList", itemsList);

      //指定试图

      modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

      return modelAndView;

   }

}


编写一个jsp显示页面

<%@pagelanguage="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>查询商品列表</title>

</head>

<body>

<formaction="${pageContext.request.contextPath }/item/queryItem.action"method="post">

查询条件:

<tablewidth="100%"border=1>

<tr>

<td><inputtype="submit"value="查询"/></td>

</tr>

</table>

商品列表:

<tablewidth="100%"border=1>

<tr>

   <td>商品名称</td>

   <td>商品价格</td>

   <td>生产日期</td>

   <td>商品描述</td>

   <td>操作</td>

</tr>

<c:forEachitems="${itemsList }"var="item">

<tr>

   <td>${item.name }</td>

   <td>${item.price }</td>

   <td><fmt:formatDatevalue="${item.createtime}"pattern="yyyy-MM-dd HH:mm:ss"/></td>

   <td>${item.detail }</td>

  

   <td><ahref="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>

</tr>

</c:forEach>

</table>

</form>

</body>

</html>


参考文章:

http://blog.youkuaiyun.com/dzy21/article/details/51864486    //springmvc框架原理图解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值