zoukankan      html  css  js  c++  java
  • SpringMVC_简单小结

    SpringMVC是一个简单的、优秀的框架。应了那句话简单就是美,而且他强大不失灵活,性能也很优秀。

    机制:spring mvc的入口是servlet,而struts2是filter(这里要指出,filter和servlet是不同的。以前认为filter是 servlet的一种特殊),这样就导致了二者的机制不同,这里就牵涉到servlet和filter的区别了。

    SpringMVC 简单案例

    1.导入jar

    导入的架包是在原来的Spring jar基础添加两个jar包(上图红框圈的)

    spring-webmvc-4.2.0.RELEASE.jar                对Spring mvc的实现

    spring-context-support-4.2.0.RELEASE.jar      包含支持UI模板,邮件服务,缓存Cache等方面的类

    2.在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>	
      
    <!--  1. 配置中央调度器(前端控制器) -->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 3.1指定配置文件applicationContext.xml -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <!-- 3.2Tomcat启动的时候,Service对象已经到内存    下面该数字要大于等于1,0或者是负数,跟没设置是一样的-->
    <load-on-startup>1</load-on-startup>
    
    </servlet>
    <!--  2. --> 
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
    <!-- <url-pattern>*.do</url-pattern> -->
    </servlet-mapping> 
    
     
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            ">
            <!-- 视图解析器 -->
             <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    			<property name="prefix" value="/WEB-INF/jsp"></property>
    			<property name="suffix" value=".jsp"></property>
            </bean> 
            
            <!-- 注册处理器 -->
         <bean id="/hello.do" class="cn.happy.controller.Y2Controller"></bean> 
         
          
    </beans>        
    

    定制处理器(Y2Controller.java)

    public class Y2Controller implements Controller{//处理器
        //handleRequest 处理请求
    	//ModelAndView  返回值类型
    	public ModelAndView handleRequest(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		ModelAndView mv=new ModelAndView();
    		mv.addObject("msg", "明天放假一天");
    		//处理一道
    		mv.setViewName("index");
    		
    		return mv;
    	}
    }
    

    jsp页面

     <body>
        This is my SpringMVC 简单案例. <br>
        <!-- 图片 -->
        <img src="image/1.jpg" width="500px" height="400px">
      </body>

    关于urlpattern说法

    最好配成*.do  不能配成/*  

    无法访问*.jsp等动态资源  最好也不要配成/    

    无法访问静态资源 不得不配成/  Restful编程 rest

    不能配置为/*

    DispatcherServlet会将向动态页面请求,即向jsp页面的跳转请求也当做是一个普通的Controller请求。中央调度器会调用处理器映射器为其查找相应的处理器。当然是找不到的。jsp页面会报404错误 结论:/*会拦截动态资源

    不得配成/

    使用Tomcat默认的Service解决

    方案二:使用MVC的default-servlet-handler

    会将对静态资源的访问请求通过HandlerMapping映射到默认Servlet请求处理器DefaultServletRequestHandler对象。而该处理器调用了Tomcat的DefaultServlet来处理静态资源的访问请求。当然需要引入mvc约束

     

    方式三:使用MVC的resource解决

    在Spring3.0.4之后,Spring定义了专门用于处理静态资源请求的处理器ResourceHttpRequestHandler。并且添加了<mvc:resources/>标签,专门用于解决静态资源无法访问问题。

    Location:静态资源所在目录 mapping: 对资源的请求 注意:需要Tomcat7支持

  • 相关阅读:
    【译】常用网络端口号列表
    使用Simian进行重复代码检测
    使用GCOV进行代码覆盖率统计
    AFL Fuzz安装及完成一次简单的模糊测试
    数据可视化概述
    完成下方的 which_date() 函数,并返回某一起始时间后特定一段时间的日期
    linux用户不在sudoers文件中
    linux /lib64/libc.so.6: version `GLIBC_2.17′ not found
    web api 2.0 上传文件超过4M时,出现404错误
    Centos7 编译安装 Nginx Mariadb Asp.net Core2 (实测 笔记 Centos 7.7 + Openssl 1.1.1d + Mariadb 10.3.7 + Nginx 1.16.1 + Asp.net. Core 2 )
  • 原文地址:https://www.cnblogs.com/baixingqiang/p/6197982.html
Copyright © 2011-2022 走看看