Struts2_Action記錄在線人數實例

在寫該實例前先來說個

關於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>


接下來給出實例



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>Insert title here</title>
</head>
<body>
	<form action="loginin.do" method="post">
		用戶名<input type="text"  name="username"/>
		<br><br>
		<input type="submit" value = "submit"/> 
	</form>	
</body>
</html>

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>Insert title here</title>
</head>
<body>
		
		welcome: ${sessionScope.username} 
		<br><br>
		共有   ${applicationScope.count} 位訪問者
		
		<a href="loginout.do">退出</a>
</body>
</html>

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.action.extension" value="action,do,"></constant>
    <package name="default"  extends="struts-default">
    
		<action name="loginin" class="cn.zucc.wul.LoginInAction">
			<result name="loginin-success">/success.jsp</result>
		</action>
		
		<action name="loginout" class="cn.zucc.wul.LoginInAction" method="tuichu" >
			<result name="loginout-success">/index.jsp</result>
		</action>
		
    </package>

</struts>

LoginInAction.java

package cn.zucc.wul;

import java.util.Map;
import java.util.Set;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;


public class LoginInAction implements SessionAware,ApplicationAware{
	
	private String username;
	
	
	private Map<String, Object> session;
	
	private Map<String, Object> application;
	
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String execute(){
		
		//把用戶信息存入Session域中
		//1.獲取session,通過實現SessionAware接口
		
		//2.獲取登錄信息,通過Action中添加的setter方法
		
		//3.把用戶信息存入Session域中
		session.put("username", username);
		
		//在線人數+1
		//1.獲取當前在線人數,從application中獲取
		Integer count = (Integer) application.get("count");
		if(count == null){
			count=0;
		}
		//2.使當前在線人數+1
		count++;
		application.put("count", count);
		
		return "loginin-success";
	}
	
public String tuichu(){
	
		//1.在線人數 -1 :獲取在線人數,若數量 >0 , 則 -1		
		Integer count = (Integer) application.get("count");
		
		if(count != null && count > 0){
			count--;
			application.put("count", count);
		}
		
		//2.session失效,強轉爲SessionMap,調用invalidate方法使其失效
		((SessionMap)session).invalidate();

		
		return "loginout-success";
	}
	

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
		
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
		
	}
}



直接輸入http://localhost:8080/struts2_5/success.jsp查看






發佈了1 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章