使用Maven添加依赖的jar包

第一个还没用上

刚开始没加spring-context,@Controller没法用

web.xml配置

1.       配置DispatcherServlet

<servlet>
        <description>Spring MVC Servlet</description>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>Spring MVC 配置文件</description>
            <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>/</url-pattern>
    </servlet-mapping>

代码如上,就不多解释了

还是说一点吧

注意:<load-on-startup>1</load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动~

<url-pattern>/</url-pattern>,请求映射配置为“/”,框架能够捕获所有URL请求,同时又将静态资源的请求转交给web容器处理(之后将进一步说明)

2.      过滤器

1 <!-- 过滤器 -->

 2 <filter>
 3         <description>
 4         </description>
 5         <display-name>CharacterEncodingFilter</display-name>
 6         <filter-name>CharacterEncodingFilter</filter-name>
 7         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 8         <init-param>
 9             <param-name>encoding</param-name>
             <param-value>utf-8</param-value>
         </init-param>
     </filter>
     <filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

解决Post提交中文乱码问题,也不必自己写过滤器啦

springMVC配置

1.xml schema配置

刚开始忽好忽坏的,忘记报啥错了……查了查是这个的问题,缺一不可呀

 1 <!-- xml schema -->
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="
 6  http://www.springframework.org/schema/beans
 7  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 8  http://www.springframework.org/schema/context
 9  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/mvc  
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

2.自动扫描包名

让其中的注解生效

1 <!-- 自动扫描的包名 -->

 <context:component-scan base-package="controller" />

3.视图解析器

JSP模板页面用到了JSTL标签库

 <!-- 视图解析器 -->
     <bean
         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="viewClass"
             value="org.springframework.web.servlet.view.JstlView" />
         <property name="prefix" value="/WEB-INF/views/" />
         <property name="suffix" value=".jsp" />
     </bean>

4.自动注册

这个好用啦,会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean

一开始没加也能用呢Q^Q,时好时坏报错 No mapping found for HTTP request with URI [xxx] in DispatcherServlet with name 'springMVC' 就查到缺这个了。为什么呢?

 <mvc:annotation-driven />

(写到这里我已经心累了)

5.静态资源映射

本来可以介绍一番,但是截图上传图片太麻烦了(可能会上传PPT,可以参考

 <!-- 对静态资源文件的访问 -->
     <mvc:resources location="/resources/" mapping="/resources/**" />

6.静态资源处理?

也可以把静态资源转交给web容器处理。

泪奔,添加他是因为忽然又报错,No mapping found for HTTP request with URI……

然而刚刚试着删掉还是能用呢,不能理解

1 <mvc:default-servlet-handler/>

7.拦截器

1 <!-- 拦截器 -->

 2     <mvc:interceptors>
 3         <mvc:interceptor>
 4             <mvc:mapping path="/user/**" />
 5             <bean class="interceptor.UserLoginInterceptor" />
 6         </mvc:interceptor>
 7         <mvc:interceptor>
 8             <mvc:mapping path="/admin/**" />
 9             <bean class="interceptor.AdminLoginInterceptor" />
         </mvc:interceptor>
     </mvc:interceptors>

以上代码用来对未登录用户做权限管理,没啥好说的,由于只用到了预处理方法,也可以用过滤器来实现。但是拦截器和过滤器还是不一样的,感觉它的postHandle方法是过滤器做不到的。(没有用过,瞎猜的。

忘记了来源的图片

(二)spring MVC配置的更多相关文章

  1. Tomcat配置和Spring MVC配置

    Tomcat启动时,先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOME.然后找这个变量所指的目录下的conf文件夹,从中读取配置文件.最重要的配置文件:server.xm ...

  2. Spring MVC配置详解(3)

    一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...

  3. Spring MVC(二)--Spring MVC登陆实例

    本文通过一个简单的登陆实例实现Spring MVC的流程,同时整合 MyBatis使用,流程是这样的: 1.访问一个URL进入登陆界面 2.输入正确的用户名和密码,成功则进入index页面,否则留在登 ...

  4. spring MVC配置详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  5. Spring mvc 配置详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  6. spring MVC配置详解(转)

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  7. Spring MVC配置静态资源和资源包

    Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...

  8. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

  9. Spring MVC 配置类 WebMvcConfigurerAdapter

    WebMvcConfigurerAdapter配置类是spring提供的一种配置方式,采用JavaBean的方式替代传统的基于xml的配置来对spring框架进行自定义的配置.因此,在spring b ...

  10. MQTT 4 ——MQTT的Spring Mvc 配置接收字节流数据

    本篇记录一下MQTT整合Spring Mvc配置直接收发字节流数据 设备方是纯C开发,并且为了交互数据的安全,将传送的数据用了AES CBC进行了加密. 接下来正常方便做法应该是 将加密后的字节流转换 ...

随机推荐

  1. CC2530存储空间——Code

    硬件平台:CC2530-F256 开发环境:IAR 8051(版本号 8.10) 參考: .<CC2530 User's Guide.pdf>(swru191c) .<IAR C/C ...

  2. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第7章节--打包并部署SP2013 Apps 打包并公布App

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第7章节--打包并部署SP2013 Apps 打包并公布App         如今既然你理解了一个app的四个主要部分, ...

  3. [LeetCode160]Intersection of Two Linked Lists

    题目: Write a program to find the node at which the intersection of two singly linked lists begins.   ...

  4. SQL server 2005 PIVOT运算符的使用

    原文:SQL server 2005 PIVOT运算符的使用 PIVOT,UNPIVOT运算符是SQL server 2005支持的新功能之一,主要用来实现行到列的转换.本文主要介绍PIVOT运算符的 ...

  5. 采用ToolRunner执行Hadoop基本面分析程序

    为了简化执行作业的命令行.Hadoop它配备了一些辅助类.GenericOptionsParser它是一类.经常用来解释Hadoop命令行选项,并根据需要.至Configuration采取相应的对象设 ...

  6. MySQL InnoDB数据库备份与还原

    备份 进入cm黑窗口 输入下列命令 mysqldump -u 用户名 -p 数据库名称> c:\11.sql 回车执行 恢复 进入cm黑窗口 输入下列命令 mysql>use dbtest ...

  7. Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock’

    今天服务器遇到了一个很熟悉的问题 输入 #mysql -u root -p ERROR 2002 (HY000): Can't connect to local MySQL server throug ...

  8. UVa 514 Rails(经典栈)

     Rails  There is a famous railway station in PopPush City. Country there is incredibly hilly. The st ...

  9. LeetCode——ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  10. MapReduce在实际编程“I/O”

    通过本篇MapReduce分析模型.深化MapReduce理解模型:和演示MapReduc进入编程模型是常用格类型和输出格公式,在这些经常使用格外公式,我们能够扩大他们的投入格公式,实例:们须要把Mo ...