在写该实例前先来说个
<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>
<?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查看