01.创建一个登录界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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 '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" method="post"> <input type="text" name="user.name"/> <input type="submit" value="登录"/> </form> </body> </html>
02.创建对应的struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!--设置开发模式 --> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/user" extends="struts-default"> <action name="login" class="cn.bdqn.action.UserAction" method="login"> <!-- 重定向到一个指定命名空间下面的 action--> <result type="redirectAction"> <param name="namespace">/interceptor</param> <param name="actionName">login</param> </result> </action> </package> <include file="user.xml"/> </struts>
03.创建对应的UserAction
public class UserAction extends ActionSupport{ private User user; //验证的对象 //登录的方法 public String login(){ System.out.println("进入UserAction的login()"); //把前台登录的用户信息保存到session Map<String, Object> session = ActionContext.getContext().getSession(); if (user.getName()!=null&&!user.getName().equals("")) { session.put("user", user.getName()); }else { session.put("user", ""); } return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
04.创建对应的user.xml文件
应该是先创建loginAction,之后创建对应的拦截器! 最后把拦截器配置在xml文件中!
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="interceptor" namespace="/interceptor" extends="default"> <!-- 配置自定义的拦截器 --> <interceptors> <interceptor name="myInterceptor" class="cn.bdqn.interceptor.LoginInterceptor"/> <!--自定义拦截器栈 --> <interceptor-stack name="myStack"> <!-- 必须先引用struts2默认的拦截器栈 --> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myInterceptor"/> </interceptor-stack> </interceptors> <!-- 让程序 执行我们自定义的拦截器栈 --> <default-interceptor-ref name="myStack"/> <action name="login" class="cn.bdqn.action.LoginAction" method="login"> <result name="error">/error.jsp</result> <result>/success.jsp</result> </action> </package> </struts>
05.创建对应的LoginAction
public class LoginAction extends ActionSupport { public String login(){ System.out.println("这是LoginAction中的login()"); return SUCCESS; } }
06.创建对应的拦截器
public class LoginInterceptor implements Interceptor { /** * 在整个拦截器的生命周期中! * 服务器启动的时候执行一次! */ @Override public void init() { System.out.println("拦截器的init()...................."); } /** * intercept相当于我们filter中的doFilter() * 拦截一次请求,就会执行一次! */ @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("拦截器的intercept()...................."); System.out.println("当期的执行Action====:"+invocation.getAction()); Map<String, Object> session = ActionContext.getContext().getSession(); //从session中获取用户的姓名 String name=(String) session.get("user"); if (name!=null&&!name.equals("")) { //说明前台用户有输入 //放行 return invocation.invoke(); }else { return "error"; } } /** * 在整个拦截器的生命周期中! * 服务器关闭的时候执行一次! */ @Override public void destroy() { System.out.println("拦截器的destroy()................."); } }
07.错误界面 ! 省略!