struts2前后台传值汇总-(1)ActionContext-map格式

后台调用ActionContext中的getObject(),getSession(),getApplication() 获取request,session,application.

 

package struts.test.action;

import java.sql.SQLException;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 登录练习
 * @author zk
 *
 */
//public class LoginAction implements Action {
public class LoginAction extends ActionSupport {

	//获取request对象
	ActionContext context;
	Map request1;
	Map session1;
	Map application1;
	
	
	@Override
	public String execute() throws Exception {
					
			context = ActionContext.getContext();
			request1 = (Map)context.get("request");
			session1 = context.getSession();
			application1 = context.getApplication();
			
			request1.put("req", "request1属性");
			session1.put("ses", "session1属性");
			application1.put("app", "application1属性");
			
			
			
			
			return SUCCESS;
	}

	
}

 

 

 

前台2种方式获取(EL表达式和控制反转IOC)

<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
    String path = request.getContextPath();
%>
<html>
<head></head>
<body>
	<h1>adminSuc</h1>
	管理员页面	
	<br/><br/>
	requset属性接受:<s:property value="#request.req"/> <br/>
	session属性接受:<s:property value="#session.ses"/><br/>
	application属性接受:<s:property value="#application.app"/><br/>
	<br/>
	=============================
	<br/> 
	<br/><h1> 另外一种获取方法 :</h1><br/>
	requset属性接受:${requestScope.req}<br/>
	session属性接受:${sessionScope.ses}<br/>
	application属性接受:${applicationScope.app}<br/>
	<br/>
	<br/> 
	<a href="<%=path %>/login.jsp">返回</a>
</body>
</html>

 

 

你可能感兴趣的:(ActionContext)