使用ActionContext的getContext()方法获取ActionContext()实例,表示Action运行时的周边的环境。ActionContext().get(“request”);/ActionContext().getSession();/ActionContext().getAplication();分别返回一个Map对象。在web中的request/session/application获取以上3个Map中的值,可以直接使用JSP页面上的元素获取:request.getAttribute();/session.getAttribute();/application.getAttribute();也可以说使用struts2 的标签获取以上Map中的的值:<s:property value=”#......>
A、依赖于容器的方式获取web中的元素
(1)编写index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘index.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
取得Map类型的request/session/application/HttpServletRequest/HttpSession/ServletContext的引用:
<form name="f" action="" method="post">
用户名:<input typt="text" name="name"/><br />
密码:<input typt="text" name="password"/><br />
<input type="button" value="submit1" onclick="javascript:document.f.action=‘login/login1‘;document.f.submit();"/>
<input type="button" value="submit1" onclick="javascript:document.f.action=‘login/login2‘;document.f.submit();"/>
<input type="button" value="submit1" onclick="javascript:document.f.action=‘login/login3‘;document.f.submit();"/>
<input type="button" value="submit1" onclick="javascript:document.f.action=‘login/login4‘;document.f.submit();"/>
</form>
</body>
</html>
(2)编写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" extends="struts-default" namespace="/login">
<action name="login*" class="com.zgy.login.LoginAction{1}">
<result>/login{1}_success.jsp</result>
</action>
</package>
</struts>
(3)编写LoginAction1.java
package com.zgy.login;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction1 extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private Map<String,Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
private String name;
private String password;
@SuppressWarnings("unchecked")
public LoginAction1(){
request = (Map<String,Object>)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute(){
request.put("r1", "request1");
session.put("s1", "session1");
application.put("a1","application1");
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(4)编写login1_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘login1_success.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
login1 success <br>
<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:property value="#parameters"/><br />
<s:property value="#session"/><br />
<s:property value="#attribute"/><br />
<s:debug></s:debug>
</body>
</html>
(5)浏览器端访问
http://localhost:8080/AccessElements/login/login1
B、控制反转IOC或依赖注入DI方式获取Web元素
编写LoginAction2.java
package com.zgy.login;
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;
//DI dependency injection
//IoC inverse of control 只用这种方式。
public String execute(){
request.put("r2", "request2");
session.put("s2", "session2");
application.put("a2", "application2");
return SUCCESS;
}
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
}
这种方式最常用,需要记住
C、其他方式获取web元素
package com.zgy.login;
import java.util.Map;
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("r3","request3");
session.setAttribute("s3", "session3");
application.setAttribute("a3", "application3");
return SUCCESS;
}
}
package com.zgy.login;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
public class LoginAction4 implements ServletRequestAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
范例:
在struts.xml中包含login.xml
<include file=”login.xml”/>
十四、默认Action
编写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="default" extends="struts-default" namespace="/">
<default-action-ref name="default"></default-action-ref>
<action name="default">
<result>/default.jsp</result>
</action>
</package>
</struts>
1、实现Action的最常用的方法:继承ActionSupport
2、DMI动态方法调用
3、通配符的配置
4、接受参数是方法:使用属性或者使用Domain Model来接受
5、简单的参数校验addFieldError
A)一般不实用Struts 2的UI标签
6、访问Web元素
A)Map类型:IOC 依赖Struts 2
B)原始类型:IOC 依赖Struts 2
7、包文件配置
8、默认action处理
原文地址:http://blog.csdn.net/yaguanzhou2014/article/details/41991045