Struts2学习第六课 实现登录登出功能
Posted alittlecomputer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2学习第六课 实现登录登出功能相关的知识,希望对你有一定的参考价值。
关于Struts2请求的扩展名问题:
1).org.apache.struts2包下的default.properties中配置了struts2应用的一些常量
2).struts.action.extension定义了当前struts2应用可以接受的请求的扩展名。
3).可以在struts.xml文件中以常量配置的方式修改default.properties所配置的常量。
<constant name="struts.action.extension" value="action,do,"></constant>
直接看代码:
package logan.struts.study; import java.util.Map; import org.apache.struts2.dispatcher.SessionMap; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.SessionAware; public class UserAction implements SessionAware,ApplicationAware{ private Map<String, Object> session = null; private Map<String, Object> application = null; private String username; public void setUsername(String username) { this.username = username; } public String logout(){ //数量减一 Integer count = (Integer) application.get("count"); if(count != null && count >0){ count--; application.put("count", count); } count--; //session失效 ((SessionMap)session).invalidate(); return "logout-success"; } public String execute(){ //把用户信息存入Session域中 //1.获取session,通过实现RequestAware接口 //获取登录信息 session.put("username", username); //把用户信息存入Session域中 // Integer count = (Integer) application.get("count"); if(count == null){ count = 0; } //2.使当前在线人数 + 1 count++; application.put("count", count); return "login-success"; } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } @Override public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this.application = application; } }
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> <!-- 配置Struts2可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="login-ui"> <result>/login.jsp</result> </action> <action name="user-login" class="logan.struts.study.UserAction"> <result name="login-success">/login-success.jsp</result> </action> <action name="logout" class="logan.struts.study.UserAction" method="logout"> <result name="logout-success">/login.jsp</result> </action> </package> </struts>
web.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> <!-- 配置Struts2可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="login-ui"> <result>/login.jsp</result> </action> <action name="user-login" class="logan.struts.study.UserAction"> <result name="login-success">/login-success.jsp</result> </action> <action name="logout" class="logan.struts.study.UserAction" method="logout"> <result name="logout-success">/login.jsp</result> </action> </package> </struts>
index.jsp
<?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> <!-- 配置Struts2可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="login-ui"> <result>/login.jsp</result> </action> <action name="user-login" class="logan.struts.study.UserAction"> <result name="login-success">/login-success.jsp</result> </action> <action name="logout" class="logan.struts.study.UserAction" method="logout"> <result name="logout-success">/login.jsp</result> </action> </package> </struts>
login-success.jsp
<?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> <!-- 配置Struts2可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="login-ui"> <result>/login.jsp</result> </action> <action name="user-login" class="logan.struts.study.UserAction"> <result name="login-success">/login-success.jsp</result> </action> <action name="logout" class="logan.struts.study.UserAction" method="logout"> <result name="logout-success">/login.jsp</result> </action> </package> </struts>
login.jsp
<?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> <!-- 配置Struts2可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="login-ui"> <result>/login.jsp</result> </action> <action name="user-login" class="logan.struts.study.UserAction"> <result name="login-success">/login-success.jsp</result> </action> <action name="logout" class="logan.struts.study.UserAction" method="logout"> <result name="logout-success">/login.jsp</result> </action> </package> </struts>
实现了简单的登录登出功能。
以上是关于Struts2学习第六课 实现登录登出功能的主要内容,如果未能解决你的问题,请参考以下文章