版权声明:本文为博主原创文章,未经博主允许不得转载。
(1)配置web.xml,让xml加载struts2框架
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(2)新建一个User实体类
package models; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
(3)新建一个LoginAction类,用来验证登录操作
package action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import models.User; public class LoginAction { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login(){ //用户名或密码错误 if(!"admin".equals(this.username) || !"123456".equals(this.password)){ ActionContext.getContext().put("errorMsg", "用户名或密码错误!"); return Action.LOGIN; } //用户名和密码正确 else{ User user=new User(); user.setUsername(this.username); user.setPassword(this.password); ActionContext.getContext().getSession().put("user",user); return Action.SUCCESS; } } }
(4)新建一个HomeAction类
package action; import com.opensymphony.xwork2.Action; public class HomeAction { public String index(){ return Action.SUCCESS; } }
(5)新建一个LogoutAction类,用于登出操作
package action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class LogoutAction { public String logout(){ ActionContext.getContext().getSession().remove("user"); return Action.SUCCESS; } }
(6)创建一个登录拦截器
package interceptor; import models.User; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class LoginInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { //从Session中获取user Object user=arg0.getInvocationContext().getSession().get("user"); if(user==null){ //如果没有user对象 return Action.LOGIN; //返回login }else{ //如果有登录 return arg0.invoke(); //返回Action中method返回的字符串 } } }
(7)配置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.configuration.xml.reload" value="true"></constant> <package extends="struts-default" name="default" namespace="/"> <!-- 配置拦截器 --> <interceptors> <!-- 配置登录拦截器 --> <interceptor name="loginInterceptor" class="interceptor.LoginInterceptor"></interceptor> <interceptor-stack name="myInterceptor-stack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 修改默认拦截器 --> <default-interceptor-ref name="myInterceptor-stack"></default-interceptor-ref> <!-- 设置全局返回值 --> <global-results> <result name="login">/login.jsp</result> </global-results> <!-- 配置登录、登出不需要拦截 ,设置拦截器为defaultStack --> <action name="Login_login" class="action.LoginAction" method="login"> <result type="redirectAction">Home_index</result> <interceptor-ref name="defaultStack"></interceptor-ref> </action> <action name="Logout_logout" class="action.LogoutAction" method="logout"> <result type="redirect">/login.jsp</result> <interceptor-ref name="defaultStack"></interceptor-ref> </action> <action name="*_*" class="action.{1}Action" method="{2}"> <result>/WEB-INF/{1}/{2}.jsp</result> </action> </package> </struts>
(8)在WebRoot下新建一个login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <s:form method="post" action="/Login_login"> <s:textfield name="username" label="用户名"/> <s:password name="password" label="密码"/> <s:submit></s:submit> </s:form> <s:property value="#errorMsg"/> </body> </html>
(9)在WebRoot/WEB-INF/Home下新建一个index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> 欢迎【<s:property value="#session.user.username"/>】登录 <a href="Logout_logout">退出</a> </body> </html>
时间: 2024-10-12 12:53:31