package sjxy.struts2.servletapi;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import sjxy.struts2.servletapi.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ServletApiAction extends ActionSupport {
public String test(){
ActionContext context=ServletActionContext.getContext();
Map request=(Map) context.get("request");
System.out.println("eeeeeee");
request.put("user", "張三");//在請求域中防止一個值:
Map<String,Object> session=context.getSession();//獲取回話域對象
User user=new User();
user.setName("李四");
user.setSex("男");
session.put("user", user);
//獲取上下文對象application
Map<String,Object> application=context.getApplication();
Object value=application.get("count");
if(value==null){//說明是第一次被訪問
application.put("count", new Integer(1));
}else{
Integer number=(Integer)value;
number++;
application.put("count", number);
}
return "success";
}
}
<?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>
<package name="servletapi" namespace="/api" extends="struts-default">
<action name="user" class="sjxy.struts2.servletapi.ServletApiAction" >
<result name="success">/WEB-INF/servlet/test.jsp</result>
</action>
</package>
</struts>
<%@ 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>
<center>
<h1>用戶名稱 :${requestScope.user }</h1><br><Hr>
<h2>用戶姓名:${sessionScope.user.name },性別:${sessionScope.user.sex }</h2><br>
當前頁面被訪問: <font color="red" size="30">[${applicationScope.count }]</font>次了!
</center>
</body>
</html>
這是代碼圖,夠不夠?還要什麼?