实现目的:

      1.点击“Login”的超链接。进入登录页面
      2.填写登录信息。提交表单,将用户信息保存进Session
      3.显示用户名,并计算在线人数
      4.点击“Logout”的超链接。在线人数减一,并使Session失效

Struts2实现:

      1.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2-LoginAndLogout</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

      2.导入Struts2所需的基本jar包

      
      3.编写Action类
package com.lewa.action;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware; public class LoginAction implements SessionAware,ApplicationAware{ private static final String SUCCESS = "success"; private static final String LOGOUT="logout";
//与表单域中的属性相相应
private String username;
//JavaBean-style
public void setUsername(String username){
this.username=username;
} public String execute(){
//将用户信息保存到Session中
//1.获取Session。通过SessionAware接口实现 //2.获取登录信息 //3.把登录信息保存到Session中
session.put("username", username); //在线人数 +1
//1.获取当前在线人数,从Application中获取
Integer count=(Integer) application.get("count");
if(count==null){
count=0;
} //使当前在线人数 +1
count++;
application.put("count", count);
return SUCCESS;
} public String Logout(){
//在线人数 -1
//1.获取在线人数
Integer count=(Integer) application.get("count"); //2.在在线人数>0的情况下 -1
if(count!=null && count!=0){
count--;
application.put("count", count);
} //使Session失效
((SessionMap)session).invalidate(); return LOGOUT;
}
private Map<String,Object> session;
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
} private Map<String,Object> application;
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
}

      4.在struts.xml中配置Action

<?

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>
<!--以常量配置的形式改动default.properties里所配置的action.extension -->
<constant name="struts.action.extension" value="action,do,,"/> <package name="LoginAndLogout" extends="struts-default">
<!--
> Action类默觉得ActionSupport
> 运行的方法默觉得execute
> result默觉得success
以上三者能够省略不写
--> <!-- 进入登录页面的Action配置 -->
<action name="login-ui" class="com.opensymphony.xwork2.ActionSupport" method="execute">
<result name="success">/login.jsp</result>
</action> <!-- 登录提交表单的Action配置 -->
<action name="login" class="com.lewa.action.LoginAction">
<result>/login-success.jsp</result>
</action> <!-- 登出的Action配置 -->
<action name="logout" class="com.lewa.action.LoginAction" method="Logout">
<result name="logout">/logout-success.jsp</result>
</action>
</package>
</struts>

      5.相应的JSP页面

      
        index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
</head>
<body>
<a href="login-ui.do">Login</a>
</body>
</html>

      login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<!-- struts2 UI 表单便签 -->
<s:form action="login.do" method="post">
<s:textfield name="username" label="用户名"/>
<s:submit value="Login"/>
</s:form>
</body>
</html>

      login-success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login-success</title>
</head>
<body>
<h3>使用EL</h3>
欢迎,${sessionScope.username } <br><br> 当前在线人数:${applicationScope.count } <br><br> <h3>使用OGNL和Struts2 标签</h3>
欢迎,<s:property value="#session.username"/> <br><br> 当前在线人数:<s:property value="#application.count"/> <br><br> <a href="logout.action">Logout</a>
</body>
</html>

      logout-success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Log out</title>
</head>
<body>
当前在线人数:${applicationScope.count }
</body>
</html>

      6.用户请求的扩展名问题

      > org/apache/struts2/default.properties: 配置了struts2应用的一些常量 
   > struts.action.extension=action, ,  表示配置struts2受理请求扩展名(默认以.action或者无扩展)
   > 在struts.xml文件里以常量配置的方式改动default.properties所配置的常量:
     <constant name="struts.action.extension" value="action,do,,"></constant>

   7.web应用结构文件夹

     

struts2学习笔记(二)—— 获取登录信息及计算在线人数的更多相关文章

  1. openresty 学习笔记二:获取请求数据

    openresty 学习笔记二:获取请求数据 openresty 获取POST或者GET的请求参数.这个是要用openresty 做接口必须要做的事情.这里分几种类型:GET,POST(urlenco ...

  2. struts2学习笔记二

    一.分文件编写框架配置文件 1.不分文件开发可能产生的问题 就类似于我们在写java类时,所有代码都写在一个类里,甚至写在一个方法里. 当3个人都checkout了struts.xml文件时,第一个人 ...

  3. Struts2学习笔记二 配置详解

    Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...

  4. struts2学习笔记--总结获取servletAPI的几种方式

    struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用ser ...

  5. Struts2学习笔记(二):第一个Struts2应用

    一.创建Action类. 创建工程Struts2Demo struts 2中的Action类并不需要继承struts 2中的某个父类,普遍的java类就可以. 在org.sunny.user.acti ...

  6. Struts2学习笔记(二)——配置详解

    1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...

  7. Struts2学习笔记二:开发流程

    一:创建项目,添加依赖包 二:在web.xml配置核心控制器 <filter> <filter-name>struts2</filter-name> <fil ...

  8. struts2学习笔记 day02 获取参数 访问ServletAPI 结果类型

  9. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

随机推荐

  1. webstorm快捷键大全-webstorm常用快捷键

    默认配置下的常用快捷键,提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. Webstorm预置了其他编辑器的快捷键配置,可以点击 查找/代替 ...

  2. 函数式编程:上线文、包裹、容器-我们可以将一个值用Context(上下文)包裹起来

    Functor,即函子,是 Haskell 中普遍存在的.最基本的类型类.你可以用以下两种方式来理解 Functor: 它代表某种容器,该容器能够将某一函数应用到其每一个元素上. 它代表某种“可计算上 ...

  3. Method Dispatch in Protocol Extensions

    We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different d ...

  4. 導出Excel方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  5. 纯CSS3来自定义单选框radio与复选框checkbox

    单选框(radio)自定义样式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...

  6. 面试之Redis

    面:缓存中间件--Memcached和Redis的区别是什么? 答:Memcached的优点是简单易用,代码层次类似与Hash.支持简单数据类型,但不支持数据持久化存储,也不支持主从同步,也不支持分片 ...

  7. 第3节 hive高级用法:14、hive的数据压缩

    六.hive的数据压缩 在实际工作当中,hive当中处理的数据,一般都需要经过压缩,前期我们在学习hadoop的时候,已经配置过hadoop的压缩,我们这里的hive也是一样的可以使用压缩来节省我们的 ...

  8. 216种Web安全颜色

    216种Web安全颜色 全部 JavaScript HTML5 jQuery CSS EXT Ajax Web综合 界面设计 DWR   锁定老帖子 主题:216种Web安全颜色 精华帖 (0) :: ...

  9. windows mac配置host方法

    配置host方法如下: 1.windows 系统配置host (1)打开电脑的系统盘(一般默认为C盘):C盘 -> Windows -> System32 -> drives -&g ...

  10. Tomcat:Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

    可能原因一: 在本地tomcat启动正常并且访问正常的项目放在服务器上tomcat报以上错误. 本地tomcat为7.0.68,服务器上为7.0.86 错误原因:服务器tomcat版本过高. 解决办法 ...