spring-mvc不拦截静态资源的配置

spring-mvc不拦截静态资源的配置

标签: spring


 

 "mvc:annotation-driven" 的前缀 "mvc"未绑定
办法:这是我在spring-servlet.xml文件里使用<mvc>开头的标签时,忘记引入了命名空间。在xml的beans里面加入如下代码即可
xmlns:mvc="http://www.springframework.org/schema/mvc"  
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  


 静态文件访问,主要是让DispatcherServlet不拦截以下静态资源
<mvc:annotation-driven /> 
<mvc:resources location="/image/" mapping="/image/**"/> 
<mvc:resources location="/css/" mapping="/css/**"/> 
<mvc:resources location="/js/" mapping="/js/**"/>




 <!-- 启动扫描所有的controller -->  
<context:component-scan base-package="com.peidw.web"/>  
 <mvc:annotation-driven/> 
 
<!-- 对特定路径拦截-->  
<mvc:interceptors>  
        <mvc:interceptor>  
            <mvc:mapping path="/user/MyHome"/>  
            <mvc:mapping path="/um/*"/>  
            <bean  class="com.peidw.web.interceptor.MyInterceptor"></bean>  
        </mvc:interceptor>  
 </mvc:interceptors>  
 
Java代码里实现拦截
package com.peidw.web.interceptors;




import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.*;
/**
 * Created by peidw on 2015/3/6.
 */


@Component
public class HelloInterceptor implements HandlerInterceptor{
    private static Logger logger=Logger.getLogger(HelloInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.info("执行Action前先判断权限");
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        logger.info("生成视图前,可以改model和视图");
    }


    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        logger.info("生成视图前,可以改model和视图");
    }
}


package com.peidw.web;


import com.peidw.web.interceptors.HelloInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 * Created by peidw on 2015/3/6.
 */


@Configuration
@EnableWebMvc
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HelloInterceptor()).addPathPatterns("/hello");;


    }
}


 
 <!-- 全局拦截 --> 
 <mvc:interceptors>  
     <bean class="com.peidw.web.interceptor.MyInterceptor"></bean>  
</mvc:interceptors>  
 
 
一些常用注解
@Autowired和@Qualifier  自动注入[根据类型注入]  
@Autowired 可以对成员变量、方法以及构造函数进行注释,  
@Qualifier 的标注对象是成员变量、方法入参、构造函数入参。  
ps:两者结合使用相当于@Resourcede效果。  
@Resource   自动注入[根据名称注入],可写参数name=""  
@Controller 表示控制器  
@Service    表示业务处理层[一般在serviceImpl]  
@Repository 表示持久层[一般在daoImpl]  
@Component  当你的类不清楚是哪一层的时候使用该注解  
@ResponseBody  异步返回数据类型为json  
@RequestMapping  路径,请求类型等设置  
@InitBinder   数据绑定  
@RequestBody、@ModeleAttributes、@SessionAttributes等

posted @ 2016-04-21 10:44  我不会游泳  阅读(1834)  评论(0编辑  收藏  举报