package com.mvc.action;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 登录认证的控制器
*/
@Controller
public class LoginControl {
/**
* 登录
* @param session
* HttpSession
* @param username
* 用户名
* @param password
* 密码
* @return
*/
@RequestMapping(value="/login")
public String login(HttpSession session,String username,String password) throws Exception{
//在Session里保存信息
session.setAttribute("username", username);
//重定向
return "redirect:hello.action";
}
/**
* 退出系统
* @param session
* Session
* @return
* @throws Exception
*/
@RequestMapping(value="/logout")
public String logout(HttpSession session) throws Exception{
//清除Session
session.invalidate();
return "redirect:hello.action";
}
}
package com.mvc.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;
/**
* 登录认证的拦截器
*/
public class LoginInterceptor implements HandlerInterceptor{
/**
* Handler执行完成之后调用这个方法
*/
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exc)
throws Exception {
}
/**
* Handler执行之后,ModelAndView返回之前调用这个方法
*/
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* Handler执行之前调用这个方法
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
//获取请求的URL
String url = request.getRequestURI();
//URL:login.jsp是公开的;这个demo是除了login.jsp是可以公开访问的,其它的URL都进行拦截控制
if(url.indexOf("login.action")>=0){
return true;
}
//获取Session
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
if(username != null){
return true;
}
//不符合条件的,跳转到登录界面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 使用组件扫描 -->
<!-- 将action扫描出来,在spring容器中进行注册,自动对action在spring容器中进行配置 -->
<context:component-scan base-package="com.mvc.action" />
<!-- 项目的Handler
<bean name="/hello.action" class="com.mvc.action.HelloAction"></bean>
-->
<!-- 处理器映射器HandlerMapping -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 处理器设配器HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- 视图解析器ViewResolver -->
<!-- 解析jsp,默认支持jstl -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 在实际开发中通常都需配置 mvc:annotation-driven标签,这个标签是开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器,顺序执行 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.mvc.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/login.action" method="post">
用户名:<input type="text" name="username" /><br>
密码:<input type="text" name="password" /><br>
<input type="submit" value="登录" />
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
当前用户:${username}
<c:if test="${username!=null}">
<a href="${pageContext.request.contextPath }/logout.action">退出</a>
</c:if>
${message}
</body>
</html>
package com.mvc.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//标记这个类是一个Handler处理器
@Controller
public class HelloAction{
@RequestMapping("/hello")//制定这个控制类对应的url
public String hello(Model model){
String message = "SpringMVC";
//为model添加Attribute
model.addAttribute("message",message);
return "hello";
}
// public ModelAndView handleRequest(HttpServletRequest request,
// HttpServletResponse response) throws Exception {
//
// //在页面上提示一行信息
// String message = "hello world!";
//
// //通过request对象将信息在页面上展示
// //request.setAttribute("message", message);
//
// ModelAndView modelAndView = new ModelAndView();
// // 相当于request.setAttribute(), 将数据传到页面展示
// //model数据
// modelAndView.addObject("message", message);
// //设置视图
// modelAndView.setViewName("hello");
//
// return modelAndView;
// }
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有