雨过天青云破处,这般颜色做将来
时间不等人
此目录下的动态资源,不可直接访问,只能通过请求转发的方式进行访问
@RequestMapping("/showIndex")
public String showIndex(){
System.out.println("访问index.jsp");
return "index";
}
@RequestMapping("/showMain")
public String showMain(){
System.out.println("访问main.jsp");
return "main";
}
修改视图解析器的前缀
针对请求和响应进行的额外的处理,在请求和响应的过程中添加预处理,后处理和最终处理
@RequestMapping("/login")
public String login(String name, String pwd, HttpServletRequest request){
if("zar".equalsIgnoreCase(name) && "123".equalsIgnoreCase(pwd)){
//在session中存储用户信息,用于进行权限验证
request.getSession().setAttribute("users",name);
return "main";
}else {
request.setAttribute("msg","用户名或密码不正确!");
return "login";
}
}
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//是否登录过的判断
if(request.getSession().getAttribute("users") == null){
//此时就是没有登录,打回到登录页面,并给出提示
request.setAttribute("msg","您还没有登录,请先去登录!");
request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
return HandlerInterceptor.super.preHandle(request, response, handler);//放行请求
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/showLogin"/>
<mvc:exclude-mapping path="/login"/>
<bean class="com.bjpowernode.interceptor.LoginInterceptor">bean>
mvc:interceptor>
mvc:interceptors>
到了运用时再看代码即可