struts2学习笔记(二)—— 获取登录信息及计算在线人数
实现目的:
Struts2实现:
<?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包
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页面
<%@ 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.用户请求的扩展名问题
7.web应用结构文件夹
struts2学习笔记(二)—— 获取登录信息及计算在线人数的更多相关文章
- openresty 学习笔记二:获取请求数据
openresty 学习笔记二:获取请求数据 openresty 获取POST或者GET的请求参数.这个是要用openresty 做接口必须要做的事情.这里分几种类型:GET,POST(urlenco ...
- struts2学习笔记二
一.分文件编写框架配置文件 1.不分文件开发可能产生的问题 就类似于我们在写java类时,所有代码都写在一个类里,甚至写在一个方法里. 当3个人都checkout了struts.xml文件时,第一个人 ...
- Struts2学习笔记二 配置详解
Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...
- struts2学习笔记--总结获取servletAPI的几种方式
struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用ser ...
- Struts2学习笔记(二):第一个Struts2应用
一.创建Action类. 创建工程Struts2Demo struts 2中的Action类并不需要继承struts 2中的某个父类,普遍的java类就可以. 在org.sunny.user.acti ...
- Struts2学习笔记(二)——配置详解
1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...
- Struts2学习笔记二:开发流程
一:创建项目,添加依赖包 二:在web.xml配置核心控制器 <filter> <filter-name>struts2</filter-name> <fil ...
- struts2学习笔记 day02 获取参数 访问ServletAPI 结果类型
- python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码
python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...
随机推荐
- swiper移动端下不能正常轮播的解决方案-----此坑没躺过估计很难找到正确姿势
<script> var mySwiper = new Swiper('.swiper-container', { direction: 'vertical', //horizontal横 ...
- bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?
echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...
- Sublime Text 2/3 输入法修复[Ubuntu(Debian)]
一直想找一个可以替代sublime的IDE主要还是hi因为没有好的方法解决中文输入的问题, 今天在网上找到一个非常不错的方法,亲自实验是可行的,就记录下来了,我的系统是ubuntu16.04 Subl ...
- 安卓 Android 简单数据库(增删改查)
<Button android:id="@+id/delete_btn" android:layout_width="wrap_content" andr ...
- android中ListView的定位:使用setSelectionFromTop
如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题: 解决的办法如下: 1 2 3 4 5 6 7 // 保存 ...
- Javascript中的For循环
在开发的过程中,遍历是一个经常遇到的.而for循环则是Javascript工具箱里一个好用的,也常用的工具.每个人的习惯不同,for循环的写法也不尽相同. 1.不写声明变量的写法: for(var i ...
- MFC模拟鼠标点击
MFC 工程 把以下代码放到你想要响应的函数里面就行 CPoint pt; GetCursorPos(&pt);//获取鼠标在屏幕的当前位置 SetCursorPos(100,200);//移 ...
- 大项目之网上书城(七)——书页面以及加入购物车Servlet
目录 大项目之网上书城(七)--书页面以及加入购物车Servlet 主要改动 1.shu.jsp 代码 效果图 2.shu.js 代码 3.index.jsp 代码 效果图 4.FindBookByC ...
- PHP:GD库 图片水印处理
文章来源:http://www.cnblogs.com/hello-tl/p/7592974.html <?php /** * 处理图片类 * 1.添加文字水印 * 2.添加图片水印 * 3.压 ...
- day21 03 异常处理
day21 03 异常处理 1.什么是异常 异常:程序运行时发生错误的信号 错误:语法错误(一般是不能处理的异常) 逻辑错误(可处理的异常) 特点:程序一旦发生错误,就从错误的位置停下来,不再继续执行 ...