struts2学习笔记(二)—— 获取登录信息及计算在线人数

实现目的:

      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应用结构文件夹
     






转载于:https://www.cnblogs.com/zhchoutai/p/8551074.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值