zoukankan      html  css  js  c++  java
  • springMVC入门程序-非注解开发环境配置

    springMVC是一个轻量级的MVC框架,现将入门程序的步骤记录如下:

    前期准备:创建web项目,导入jar包:

    1、在web.xml中配置过滤器:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     3   <display-name>springmvcfirst</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   <servlet>
    13       <servlet-name>springmvc</servlet-name>
    14       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    15       <init-param>
    16           <param-name>contextConfigLocation</param-name>
    17           <param-value>classpath:springmvc.xml</param-value>
    18       </init-param>
    19   </servlet>
    20   
    21   <servlet-mapping>
    22       <servlet-name>springmvc</servlet-name>
    23       <url-pattern>*.action</url-pattern>
    24   </servlet-mapping>
    25 </web-app>

    注意:a、servlet的class路径是在spring-webmvc-3.2.0.RELEAS jar包中复制DispatcherServlet的全限定名。 b、init-param参数的配置,是DispatcherServlet中需要的。否则会加载默认的配置文件,所以我们配置自己的配置文件springmvc.xml。

    2、配置springmvc.xml文件,在源代码根目录下新建source folder名称为config,然后新建配置文件。进行配置。头部信息可以进行拷贝。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     8         http://www.springframework.org/schema/mvc 
     9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    10         http://www.springframework.org/schema/context 
    11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    12         http://www.springframework.org/schema/aop 
    13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    14         http://www.springframework.org/schema/tx 
    15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    16 </beans>

    3、我们可以在springmvc.xml中配置处理器映射器、处理器适配器、视图解析器。

    1 <!-- 处理器映射器 将bean的name作为url进行查找 ,需要在配置Handler时指定beanname(就是url) 所有的映射器都实现 HandlerMapping接口。-->
    2     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    3 
    4 <!-- 处理器适配器 所有处理器适配器都实现 HandlerAdapter接口 -->
    5     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    6 
    7 <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->
    8     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>    

    4、编写处理器;处理器要实现Controller接口,重写handleRequest方法。

     1 package com.yy.ssm.controller;
     2 
     3 import java.sql.Date;
     4 import java.util.ArrayList;
     5 import java.util.List;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 import org.springframework.web.servlet.ModelAndView;
    11 import org.springframework.web.servlet.mvc.Controller;
    12 
    13 import com.yy.po.Items;
    14 
    15 /**
    16  * 实现contorller接口的处理器
    17  * @author YUAN
    18  *
    19  */
    20 public class ItemsController1 implements Controller{
    21 
    22     @Override
    23     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    24         //调用service查询数据
    25         List<Items> itemsList=new ArrayList<Items>();
    26         Items items1=new Items("小浣熊", 2f,new Date(System.currentTimeMillis()), "好吃又实惠");
    27         Items items2=new Items("咖啡", 5f,new Date(System.currentTimeMillis()), "香浓更好心情");
    28         Items items3=new Items("雪碧加冰", 3f,new Date(System.currentTimeMillis()), "清新冰凉");
    29         itemsList.add(items1);
    30         itemsList.add(items2);
    31         itemsList.add(items3);
    32         //返回modeAndView
    33         ModelAndView modelAndView=new ModelAndView();
    34         //相当于request的setAttribute,可以在JSP页面中取出数据
    35         modelAndView.addObject("itemsList", itemsList);
    36         //指定视图
    37         modelAndView.setViewName("itemsList.jsp");
    38         return modelAndView;
    39     }
    40 
    41 }

    5、配置处理器的URL映射

    1 <!-- 配置处理器handler 【注意:路径最前面一定要加上/反斜杠,否则报错路径找不到,映射错误】-->    
    2     <bean name="/queryItems.action" class="com.yy.ssm.controller.ItemsController1"/>

    6、至此,spingmvc的入门程序就开发完成了,我们可以部署到tomcat进行访问了。

  • 相关阅读:
    bzoj 4197 寿司晚宴
    Codeforces Round #429 (Div. 2)ABC
    Codeforces Round #386 (Div. 2) E
    UESTC 电子科大专题训练 数论 L
    UESTC 电子科大专题训练 数论 E
    Codeforces Round #396 D
    UESTC 电子科大专题训练 DP-E
    UESTC 电子科大专题训练 数据结构 L
    UESTC 电子科大专题训练 数据结构 K
    UESTC 电子科大专题训练 数据结构-E
  • 原文地址:https://www.cnblogs.com/javayuan/p/4959210.html
Copyright © 2011-2022 走看看