1.SpringMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.tcc.controller"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>//拦截/user下的所有方法
<mvc:exclude-mapping path="/user/login"/>//可以选择不拦截登录和添加以及其他的方法,根据情况自己定
<mvc:exclude-mapping path="/user/add"/>
<bean class="com.tcc.interceptor.interceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.tcc.controller"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>//拦截/user下的所有方法
<mvc:exclude-mapping path="/user/login"/>//可以选择不拦截登录和添加以及其他的方法,根据情况自己定
<mvc:exclude-mapping path="/user/add"/>
<bean class="com.tcc.interceptor.interceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
2.interceptor
package com.tcc.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.tcc.bean.user;
public class interceptor implements HandlerInterceptor{ //这里实现一下HandlerInterceptor会有三个方法,只需要编写boolean类型的preHandle方法
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2) throws Exception {
HttpSession session = request.getSession();
user user=(user)session.getAttribute("user");
if (user==null) {
response.sendRedirect(request.getContextPath()+"/login.jsp");
return false;
}
return true;
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.tcc.bean.user;
public class interceptor implements HandlerInterceptor{ //这里实现一下HandlerInterceptor会有三个方法,只需要编写boolean类型的preHandle方法
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2) throws Exception {
HttpSession session = request.getSession();
user user=(user)session.getAttribute("user");
if (user==null) {
response.sendRedirect(request.getContextPath()+"/login.jsp");
return false;
}
return true;
}
}
3.controller
package com.tcc.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.tcc.bean.type;
import com.tcc.bean.user;
import com.tcc.service.userservice;
@Controller
@RequestMapping("user")
public class controller {
private List list;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Autowired
private userservice us;
@RequestMapping("login")
public String login(user user,HttpSession session){
user login = us.login(user);
session.setAttribute("user", login);
if (login!=null) {
return "redirect:/user/list";
}
return "index";
}
}
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.tcc.bean.type;
import com.tcc.bean.user;
import com.tcc.service.userservice;
@Controller
@RequestMapping("user")
public class controller {
private List list;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Autowired
private userservice us;
@RequestMapping("login")
public String login(user user,HttpSession session){
user login = us.login(user);
session.setAttribute("user", login);
if (login!=null) {
return "redirect:/user/list";
}
return "index";
}
}