zoukankan      html  css  js  c++  java
  • SpringMVC注解开发方式

    环境准备

    springmvc版本:spring3.2

    需要spring3.2所有jar(一定包括spring-webmvc-3.2.0.RELEASE.jar

    工程结构

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

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <!-- 配置前端控制器 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
    </web-app>

    load-on-startup:表示servlet随服务启动;

    url-pattern:*.action的请交给DispatcherServlet处理。

    contextConfigLocation:指定springmvc配置的加载位置,如果不指定则默认加

    载WEB-INF/[DispatcherServlet 的Servlet 名字]-servlet.xml。

    创建springmvc.xml

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

    配置注解处理器适配器(配置在springmvc.xml)

    <!-- 注解处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    配置注解处理器映射器(配置在springmvc.xml)

    <!-- 注解处理器映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    配置视图解析器(配置在springmvc.xml)

     <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

    创建controller

    注意:需要在类上标记@Controller注解,在方法上标注@RequestMapping(value="")来配置映射路径

    /**
     * 商品信息contronller
     * 注解开发
     */
    @Controller
    public class ItemsController3 {
    
        @RequestMapping(value="/query.action")
        public ModelAndView query() throws Exception {
    
            //调用service查找 数据库,查询商品列表,这里使用静态数据模拟
            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  = new ModelAndView();
            modelAndView.addObject("itemsList", itemsList);
            modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
            return modelAndView;
        }
    }

    把controller注册到spring容器

    <bean class="com.company.springmvc.controller.ItemsController3" />

    当然也可以通过配置类扫描方式把controller注册到spring容器中

    <context:component-scan base-package="com.company.springmvc.controller"></context:component-scan>

    jsp页面

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

    部署访问

  • 相关阅读:
    php 注册与登录
    php 多条件查询
    php(ajax)异步刷新(转)
    PHP中的6种加密方式
    ajax 异步刷新,需要填写的参数
    php+Mysql 页面登录代码
    php+Mysql页面注册代码
    jquery入门知识点总结(转)
    php+Mysql中网页出现乱码的解决办法详解
    php代码常见错误详解整理
  • 原文地址:https://www.cnblogs.com/chunguang-yao/p/10666406.html
Copyright © 2011-2022 走看看