`
yu979735777
  • 浏览: 2261 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring boot+Spring MVC+Mybatis 问题收集

 
阅读更多

1.重定向参数传递

问题场景:列表态选择第3页,然后编辑数据,提交后重定向到列表页,回到了第一页而不是想要的第三页

 

问题解决:

 

 @RequestMapping("/edit")
    public String edit(RedirectAttributes model,TrialuserExtendinfoVO trialuserExtendinfoVO,String pageNum) throws BusinessException {
    	try {
			trialuserService.saveOrUpdateTUExtendInfo(trialuserExtendinfoVO);
		} catch (BusinessException e) {
			throw new BusinessException("更新数据失败!");
		}
    	model.addFlashAttribute("pageNum", pageNum);
        return "redirect:/trialuser/toList";
    }

 1. 在要发起重定向的方法中加入参数RedirectAttributes model 用它来保存你要保存的参数

 

model.addFlashAttribute("pageNum", pageNum);
2. 在接收重定向方法
    public String toList(Model model,@ModelAttribute("pageNum") String pageNum) {
    	model.addAttribute("pageNum", pageNum);
        return "trialuser/trialuserList";
    }
 加入@ModelAttribute("pageNum") 来接收你的参数即可

2、未登录禁止访问非登录界面的其他页面

1、 编写一个自己的拦截器继承 HandlerInterceptorAdapter 并实现 preHandle 方法(请求前处理)

 

public class AuthenticationInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		// 判断是否已有该用户登录的session
		if (session.getAttribute(WebRequestConfigurer.SESSION_KEY) != null) {
			return true;
		}
		// 跳转到登录页
		String url = "/";
		response.sendRedirect(url);
		return false;
	}
}

 2、 将拦截器注入配置

 

@Configuration
public class WebRequestConfigurer implements WebMvcConfigurer{
	
	/**
     * 登录session key
     */
    public static final String SESSION_KEY = "user";
    
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		InterceptorRegistration addInterceptor = registry.addInterceptor(authenticationInterceptor());
		// 排除配置
		addInterceptor.excludePathPatterns("/css/**");
		addInterceptor.excludePathPatterns("/js/**");
		addInterceptor.excludePathPatterns("/img/**");
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/login/checkUser");
        addInterceptor.excludePathPatterns("/");
        // 拦截配置
        addInterceptor.addPathPatterns("/**");
	}
	@Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }
}

* 注意:因为可能会拦截css,js,img 等静态资源,所有要将不拦截的路径配置到excludePathPatterns 中

3、在登录校验成功的地方保存session值

 

		public String checkUser(@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "password", required = false) String password, HttpServletRequest request,HttpSession session) {

		try {
			LoginUserVO user = userService.selectByUserNameAndPassword(userName, password);
			if (user == null) {
				throw new BusinessException();
			}
		} catch (BusinessException e) {
			log.error("登陆失败");
			return "error/error";
		}
		// 设置session
        session.setAttribute(WebRequestConfigurer.SESSION_KEY, userName);
		return "redirect:/login/main";

	}

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics