<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="pers.river.interceptor.LogInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
public class LogInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("afterCompletion");
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
Log log = new Log();
log.setLoginTime(dateFormat.format(date).toString());
log.setName(request.getSession().getAttribute("username").toString());
log.setId(Integer.parseInt(request.getSession().getAttribute("id").toString()));
LogDaoImpl dao = new LogDaoImpl();
System.out.println(log.getName() + log.getId());
dao.insertLog(log);
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("postHandle");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
// TODO Auto-generated method stub
System.out.println("preHandle");
return true;
}
}
@Controller
@RequestMapping("/admin")
public class UserController {
UserService service = new UserServiceImpl();
// 全浏览
@RequestMapping("/looklist.action")
public String look(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
Integer grade = Integer.parseInt(session.getAttribute("grade")
.toString());
String user = (String) session.getAttribute("username");
if (grade > 2) {
ArrayList<User_reg> looklist = service.findAllService();
request.setAttribute("lookList", looklist);
} else {
ArrayList<User_reg> looklist = service.findUserService(user);
request.setAttribute("lookList", looklist);
}
return "look";
}
// 删除
@RequestMapping("/delete.action")
public String del(Integer id) {
service.delService(id);
return "redirect:looklist.action";
}
// 批量删除
@RequestMapping("/delSelecte.action")
public String delSelect(Integer[] ids) {
if (ids != null) {
for (Integer id : ids) {
service.delService(id);
}
}
return "redirect:looklist.action";
}
// 通过ID找到指定要修改的用户
@RequestMapping("/findUser.action")
public String findUser(HttpServletRequest request,
HttpServletResponse response, Integer id) {
User_reg user = service.findUserById(id);
request.setAttribute("user", user);
return "modify";
}
// 提交修改的用户信息
@RequestMapping(value="/modify.action")
public void modify(HttpServletRequest request,
HttpServletResponse response, PrintWriter out, User_reg user) {
DateUtil today = new DateUtil();
String reg_time = today.getDate();
String reg_ip = request.getRemoteAddr();
User_reg u = service.getService(user.getId());
u.setPw(user.getPw());
u.setAge(user.getAge());
u.setEmail(user.getEmail());
u.setReg_time(reg_time);
u.setReg_ip(reg_ip);
service.modifyService(u);
out.print("修改成功!3秒后自动跳转浏览数据。");
response.setHeader("refresh", "3; URL=looklist.action");
}
// 指定后台管理页面首页
@RequestMapping("welcome.action")
public String welcome() {
return "welcome";
}
}
这里登陆后控制台没有显示拦截器的信息,为什么没有拦截到/admin/welcome.action
<mvc:mapping path="/**/."/>
不行呀
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.springmvc.intercepter.MyInterceptor"/> </mvc:interceptor> </mvc:interceptors>
删除修改能能拦截成功的话,你就改成这个试试@RequestMapping("/welcome.action")
你的拦截器更我写的不一样,我一般会在prehandle 写拦截逻辑: after 里是后置处理写在那里面没用
你用的spring哪个版本?
– 。淑女范erり 4年前@。淑女范erり: 4.3.6
– 《这是记忆中的一本书!》 4年前@《这是记忆中的一本书!》: 4.X 这么写没错,至于没生效就可能是其他地方有配置错误了、
– 。淑女范erり 4年前@《这是记忆中的一本书!》: 看了你的代码,配置没问题,代码有错误。拦截器正常工作,但是出错了。这段代码session设置应该取user的值,而不是u的值。
– 。淑女范erり 4年前@。淑女范erり: 非常感谢,
– 《这是记忆中的一本书!》 4年前id
这里确实有点问题@《这是记忆中的一本书!》: 从GitHub看你代码了,写的还是有问题,建议在仔细研究interceptor,你只是拦截了,每次登陆,如果报错情况是不是可以在拦截后处理一下
– DouglasKey 4年前