访问Web元素可以有四种方法,下面依次列出,三个视图文件源码:
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=ISO-8859-1">
<title>Access Web Elements</title>

</head>
<body>
<form name="f" action="" method="post">
<p>用户名:</p>
<input type="text" name="name" />
<p>密码:</p>
<input type="text" name="password" /> <br>
<input type="button" value="submit1"
    onclick
="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2"
    onclick
="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3"
    onclick
="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4"
    onclick
="javascript:document.f.action='login/login4';document.f.submit();" />


</form>


</body>
</html>
user_login_success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8"
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>success</title>
<%
    
String path = request.getContextPath();
    
String basePath = request.getScheme() + "://"
            
+ request.getServerName() + ":" + request.getServerPort()
            
+ path + "/";
%>
<base href="<%=basePath%>" />
<!-- 使用base标签指定本页面所有链接的参照路径 -->
</head>
<body>

<p>Login success.</p>
<s:property value="#request.r1" />
|
<%=request.getAttribute("r1")%>
<br>
<s:property value="#session.s1" />
|
<%=session.getAttribute("s1")%>
<br>
<s:property value="#application.a1" />
|
<%=application.getAttribute("a1")%><br>

<s:debug></s:debug>
</body>
</html>
user_login_error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8"
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>error</title>
<%
    
String path = request.getContextPath();
    
String basePath = request.getScheme() + "://"
            
+ request.getServerName() + ":" + request.getServerPort()
            
+ path + "/";
%>
<base href="<%=basePath%>" />
<!-- 使用base标签指定本页面所有链接的参照路径 -->
</head>
<body>

<p>User add error.</p>
<s:fielderror fieldName="name"></s:fielderror>
<br>
<s:property value="errors.name[0]"></s:property>
<s:debug></s:debug>
</body>
</html>
Struts2配置文件struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>
    
    
<constant name="struts.devMode" value="true" />

    
<package name="login" namespace="/login" extends="struts-default">
        
<action name="login*" class="com.bebig.struts2.user.action.LoginAction{1}">
            
<result>
                /user_login_success.jsp
            
</result>
            
<result name="error">
                /user_login_error.jsp
            
</result>
        
</action>
    
</package>

</struts>
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  
<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>
  
<welcome-file-list>
    
<welcome-file>index.html</welcome-file>
    
<welcome-file>index.jsp</welcome-file>
  
</welcome-file-list>
</web-app>
第一种方法:
LoginAction1.java
package com.bebig.struts2.user.action;

import java.util.Map;

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

public class LoginAction1 extends ActionSupport {
    
//取得Map类型的request、session、application
    private Map request;
    
private Map session;
    
private Map application;

    
public LoginAction1() {
        request 
= (Map) ActionContext.getContext().get("request");
        session 
= ActionContext.getContext().getSession();
        application 
= ActionContext.getContext().getApplication();
    }

    
    
public String execute()
    
{
        request.put(
"r1""r1");
        session.put(
"s1""s1");
        application.put(
"a1""a1");
        
return SUCCESS;
    }


}

第二种方法:
LoginAction2.java
package com.bebig.struts2.user.action;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction2 extends ActionSupport implements RequestAware,
        SessionAware, ApplicationAware 
{
    
//三个对象被容器注入
    private Map<String, Object> request;
    
private Map<String, Object> session;
    
private Map<String, Object> application;

    
public String execute() {
        request.put(
"r1""r1");
        session.put(
"s1""s1");
        application.put(
"a1""a1");
        
return SUCCESS;
    }


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


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


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


}

第三种方法:
LoginAction3.java
package com.bebig.struts2.user.action;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport {
    
private HttpServletRequest request;
    
private HttpSession session;
    
private ServletContext application;

    
public LoginAction3() {
        request 
= ServletActionContext.getRequest();
        session 
= request.getSession();
        application 
= session.getServletContext();
    }

    
    
public String execute()
    
{
        request.setAttribute(
"r1""r1");
        session.setAttribute(
"s1""s1");
        application.setAttribute(
"a1""a1");
        
return SUCCESS;
    }


}

第四种方法:
LoginAction4.java
package com.bebig.struts2.user.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction4 extends ActionSupport implements ServletRequestAware {
    
private HttpServletRequest request;
    
private HttpSession session;
    
private ServletContext application;

    
public String execute() {
        request.setAttribute(
"r1""r1");
        session.setAttribute(
"s1""s1");
        application.setAttribute(
"a1""a1");
        
return SUCCESS;
    }


    @Override
    
public void setServletRequest(HttpServletRequest request) {
        
this.request = request;
        session 
= request.getSession();
        application 
= session.getServletContext();
    }


}

总结:
第一种方法依赖于Struts2容器,取得相关对象数据;
第二种方法,依赖注入DI(dependency injection),控制反转IoC(Inverse of control),对象数据被容器注入;
第三种方法,依赖于容器取得相关对象数据;
第四种方法,依赖注入一个对象的数据后,再通过这个对象获取到其它对象数据。

推荐使用第二种方法。
另:取得Map类型request,session,application,真实类型HttpServletRequest,HttpSession,ServletContext的引用:
在第一种方法中,前面三个依赖于容器;
在第二种方法中,前面三个控制反转;
在第三种方法中,后面三个依赖于容器;
在第四种方法中,后面三个控制反转。