SpringMvc注解方式开发入门

来源:互联网 发布:c语言 libevent 编辑:程序博客网 时间:2024/09/21 08:55

编写java文件

package com.cloud.po;

import java.util.Date;

publicclass Items {

    private Integerid;

    private Stringname;

    private Float price;

    private Stringpic;

    private Datecreatetime;

    private Stringdetail;

    publicInteger getId() {

        returnid;

    }

    publicvoid setId(Integer id) {

        this.id = id;

    }

    public String getName() {

        returnname;

    }

    publicvoid setName(String name) {

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

    }

    public Float getPrice() {

        returnprice;

    }

    publicvoid setPrice(Float price) {

        this.price = price;

    }

    public String getPic() {

        returnpic;

    }

    publicvoid setPic(String pic) {

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

    }

    public Date getCreatetime() {

        returncreatetime;

    }

    publicvoid setCreatetime(Date createtime) {

        this.createtime = createtime;

    }

    public String getDetail() {

        returndetail;

    }

    publicvoid setDetail(String detail) {

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

    }

}

编写springmvc配置文件

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

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

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

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

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

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

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

      <!--

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

       -->

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

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

      -->

      <context:component-scanbase-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

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

<!DOCTYPEhtml PUBLIC "-//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.csdn.net/dzy21/article/details/51884561  //SpringMvc入门案例

0 0