<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描我们需要进行创建的bean对象的包 这些类:加@componet @service ...
注意:扫描controller的包
不能扫描service和dao包
-->
<context:component-scan base-package="com.cdsxt.controller" />
<!-- 让spring mvc的注解生效 -->
<mvc:annotation-driven />
<!--
视图解析器 jsp,freemaker
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<!-- 前缀 -->
<property name="prefix" value="/page/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 负责文件上传的解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 字符集设置 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小的上限=字节 3m左右 -->
<property name="maxUploadSize" value="3000000" />
<!-- 内存缓存的文件大小 8kb -->
<property name="maxInMemorySize" value="8192" />
</bean>
<!-- spring mvc 的拦截器 -->
<mvc:interceptors>
<!-- /* 拦截 /a / b /c /** 拦截 /a /a/a1 /b /c /c/c1/c2 -->
<!-- 拦截spring mvc请求里所有请求,除了/admin/下的所有请求 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.cdsxt.controller.t9.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
//拦截器
public class LoginInterceptor extends HandlerInterceptorAdapter{
/**
* 登录检查
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//登录操作不检查
String uri=request.getRequestURI();
if (uri.contains("/login.do")) {
return true;
}
String curUser= (String) request.getSession().getAttribute("curUser");
if (curUser==null) {
//返回没有登录
response.setCharacterEncoding("utf-8");
response.getWriter().print("没有登录,不能访问!!!");
//不执行后面的业务处理
return false;
}
//继续后面的业务处理
return true;
}
}