zoukankan      html  css  js  c++  java
  • SpringMVC第一个例子

    暂时先从加载静态数据开始

    第一步:web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 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_2_5.xsd">
        <display-name></display-name>
    
        <!-- springmvc的前端控制器的配置 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通过ContextConfigLocation配置需要加载的配置文件 配置文件需要配置适配器映射器 等等 
        如果不指定则默认加载WEB-INF/Servlet 名字-servlet.xml。
        
        -->
            <init-param>
                <param-name>ContextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
        </servlet>
        
        <!-- 
        第一种 *.action 访问以action结尾的由dispatcherServlet来解析
                第二种:/所有访问的地址都有dispatcherServlet来解析  静态的文件解析需要配置不让dispatcherServlet来解析
                使用此方式可以实现restful风格的url
                第三种:/*这样配置不对        
         -->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </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 ">
            
            <!-- 配置handler -->
            <bean name="/queryBooks.action" class="cn.byr.ssm.controller.ItemsQueryController1"></bean>
    
    
    <!-- 处理器映射器 将beanname作为url进行查找 需要在配置 handler指定beanname(就是url)-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    
    <!-- 处理器 适配器-->
    <bean        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> 
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    </bean>
        
    </beans>

    第三步 handler的开发

    package cn.byr.ssm.controller;
    
    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 cn.byr.ssm.po.Book;
    
    public class ItemsQueryController1 implements Controller {
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            //调用service来查询数据 查询商品列表返回数据 这里采用静态数据
            
            List<Book> books=new ArrayList<Book>();
            
            Book book1 =new Book();
            book1.setBname("java书籍1");
            book1.setAuthor("a");
            
    
            Book book2 =new Book();
            book2.setBname("java书籍2");
            book2.setAuthor("b");
            
    
            Book book3 =new Book();
            book3.setBname("java书籍3");
            book3.setAuthor("c");
            
            books.add(book3);
            books.add(book2);
            books.add(book2);
            
            //返回modelandview
            
            ModelAndView modelAndView = new ModelAndView();
            //相当于request的setAttribute方法
            modelAndView.addObject("books", books);
            
            //指定数据
            modelAndView.setViewName("/WEB-INF/jsp/book/itemsList.jsp");
            
            return modelAndView;
        }
    
    }

    第四步  itemList.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 }/books/queryBooks.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>
    </tr>
    <c:forEach items="${books}" var="book">
    <tr>
        <td>${book.bname }</td>
        <td>${book.author}</td>
    
    </tr>
    </c:forEach>
    
    </table>
    </form>
    </body>
    
    </html>


    第五步 测试结果

      http://localhost:8080/springmvcfirst/queryBooks.action

  • 相关阅读:
    博客
    参考博客
    KMP
    串匹配
    简单数论
    B
    各种常用函数的模板以及自己的测试数据
    header
    memcached的图形界面监控
    缓存策略
  • 原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4935222.html
Copyright © 2011-2022 走看看