(二)spring MVC配置

使用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>
10             <param-value>utf-8</param-value>
11         </init-param>
12     </filter>
13     <filter-mapping>
14         <filter-name>CharacterEncodingFilter</filter-name>
15         <url-pattern>/*</url-pattern>
16     </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
10  http://www.springframework.org/schema/mvc  
11  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


2.自动扫描包名

 

让其中的注解生效

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

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

 

3.视图解析器

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

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


 4.自动注册

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

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

1 <mvc:annotation-driven />


 

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

 

 

5.静态资源映射

 

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

1 <!-- 对静态资源文件的访问 -->
2     <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" />
10         </mvc:interceptor>
11     </mvc:interceptors>

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

 

忘记了来源的图片 

 

posted @ 2015-06-06 17:31  自燃物  阅读(318)  评论(0编辑  收藏  举报