开发者学堂课程【Java Web开发系列课程 - Struts2框架入门:登录拦截器的应用】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/537/detail/7317
登录拦截器的应用
目录:
一.对于登陆权限的控制
二.Struts.xml 的配置
三.jsp 页面
四.action 代码
五.登录成功页面
一.对于登陆权限的控制
拦截器的实现
pub1ic class LoginInterceptor extends AbstractInterceptor{
@Overridepublic String intercept(ActionInvocationinvocation)throwsException{
//判断是否是login.action如果是则直接执行下一个拦截器//如果不是则判断是否登录,如果登录执行下一个拦截器//如果没有则返回登录页面//actionName.没有扩展名String actionName=invocation.getProxy().getActionName();
if("login".equals(actionName)){return invocation.invoke();Object obj =invocation.getInvos ationContext().getSession().get("user");if(obj==nu11){//没有登录return Action.LOGIN;}return invocation.invoke();successjsp S
二.Struts.xml 的配置
<struts><package name="default"extends= "struts-default"namespace="/"><!--配置拦截器--><interceptors><interceptor name= "time"class="cn.sxt.interceptor.TimeInterceptor"/><interceptor name="loginInterceptor"class="cn.sxt.interceptor.LoginInterceptor"/><!--一个拦截器栈中,可以包含多个拦截器的引用拦截器栈的引用和拦截器一致<interceptor-stack name="myStack"><!--引用拦截器--><interceptor-ref name="time"/><!--引用默认的拦截器栈;引用拦截器和引用拦截器栈的方式一样--><interceptor-ref name= "defaultStack"/></interceptor-stack></interceptors><!--定义默认的拦截器/栈-->default-interceptor-ref name="myStack"/><global-results><result name= "login">/1ogin.jsp</result></global-results><action name="hello"class= "cn.sxt.action.HelloAction">
<result>/WEB-INF/index.jsp</resu1t></action><action name= "login" class="cn.sxt.action.LoginAction">
<result>/success.jsp</resu1t></action></package>
<struts>
三.jsp 页面
<%@ page language="java"import="java.util.”pageEncoding="UTF-8"%>
<%String path=request.getContextPath();String basePath=request.getScheme()+"://"+request.getServerName( )+":"
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"〉<html〉<head><base href=”<%=basePath%>”〉<title>My JSP 'index.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= "user/login.action"method= "post">
用户名:
<input type="text"name="name"/><br>
密码:
<input type="password"name="pwd"/><br> <input type="submit"value="登录"/></form></body></html>
四、action 代码
package cn.sxt.action;import com.opensymphony.xwork2.ActionContext;public class LoginAction private String name;private String pwd;//处理方法public String execute()System.out.println(name+"---"+pwd); .if("siggy".equals(name )&&"1111".equals(pwd))ActionContext.getContext().getSession().put("user",name);return"success";elsereturn"login";public String logout()System.out.println("退出");
return "success";public String getName()
return name;public void setName(String name)
this.name=name;public String getPwd()
return pwd;public void setPwd(String pwd)
this.pwd.pwd;
五、登录成功页面
<%@page language= "java"import="java.util.''pageEncontext
<%String path = request.getContextPath();String basePath =request.getScheme()+"://"+request.%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01Transitio
<html><head><base href= "<%=basePlath%>"><title>My JSP'index.jsp'starting page</title><meta http-equiv="keywords"content="keyword1,keyword2,keyword3>
<meta http-equiv="description"content="This is my<!--<link rel="stylesheet" type="text/css" href="style- -></head>
<body>
用户名:
$[name]<br><a href= "hello.action?name=zhangsan">hellok/a>
</body>
</html>