SpringMvc注解方式开发入门

编写java文件

package com.cloud.po;

import java.util.Date;

public class Items {

    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

    public Integer getId() {

        returnid;

    }

    public void setId(Integer id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name == null ? null : name.trim();

    }

    public Float getPrice() {

        return price;

    }

    public void setPrice(Float price) {

        this.price = price;

    }

    public String getPic() {

        return pic;

    }

    public void setPic(String pic) {

        this.pic = pic == null ? null : pic.trim();

    }

    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 == null ? null : detail.trim();

    }

}

编写springmvc配置文件

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

      <!-- 注解开发SpringMvc模式 -->

      <!-- 注解映射器 -->

      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

      <!-- 注解适配器 -->

      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

      <!-- 可以代替上面2个配置 :还会默认加载许多参数绑定的方法,实际开发中的选择-->

      <!--

      <mvc:annotation-driven></mvc:annotation-driven>

       -->

      <!-- 可以扫描controllerservice...

         这里让扫描controller,指定controller的包

      -->

      <context:component-scan base-package="com.cloud.ctroller"></context:component-scan>

</beans>

编写Handler

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.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.cloud.po.Items;

@Controller

public class ItemController2{

   @RequestMapping("/queryItems")

   public ModelAndView queryItems(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;

   }

}

前端展示页面

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

 参考文章

http://blog.csdn.net/dzy21/article/details/51884561  //SpringMvc入门案例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值