使用ActionContext类访问Servlet API
2015-05-08 19:43
295 查看
Struts2的action并未与Servlet API进行耦合,这是Struts2的一个改良,从而方便单独对Action进行测试。
但对于Web控制器而言,不访问Action是不行的,Struts提供了一种比较简单的方式访问Servlet API。通常
我们需要访问的Servlet API是HttpSession,HTTPservletRequest,ServletContext,分别对应JSP中内置
对象session,request,application。
Struts2提供了ActionContext类,可以通过该类来进行对Servlet API的访问,该类有如下方法:
Map getApplication() 返回一个Map对象,模拟了ServletContext实例
static ActionContext getContext() 静态方法,获取当前系统的ActionContext实例
Map getParameters() 返回一个Map对象,获取所有请求参数
Map getSession() 返回一个Map对象,获取HttpSession对象
我们看以在Action通过ActionContext来访问server API的实例
login.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<body>
<form action="login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
该表单提交数据后,Struts2会将数据交给LoginAction.java的Action处理,
LoginAction.java代码:
package com;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
ActionContext ac = ActionContext.getContext();
if("admin".equals(username)&&"admin".equals(password)){
ac.put("success", "登陆成功");
return Action.SUCCESS;
}else{
ac.put("error","用户名或密码错误");
return Action.ERROR;
}
}
}
这是处理用户请求的Action,该Action首先会获得ActionContext,然后判断请求的参数的username和密码是否正确,如果全正确则返回success,如果错误则返回error,并把提示信息返回到视图上
struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="org" extends="struts-default" namespace="/">
<action name="login" class="com.LoginAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
errot.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
${requestScope.error}
</body>
</html>
但对于Web控制器而言,不访问Action是不行的,Struts提供了一种比较简单的方式访问Servlet API。通常
我们需要访问的Servlet API是HttpSession,HTTPservletRequest,ServletContext,分别对应JSP中内置
对象session,request,application。
Struts2提供了ActionContext类,可以通过该类来进行对Servlet API的访问,该类有如下方法:
Map getApplication() 返回一个Map对象,模拟了ServletContext实例
static ActionContext getContext() 静态方法,获取当前系统的ActionContext实例
Map getParameters() 返回一个Map对象,获取所有请求参数
Map getSession() 返回一个Map对象,获取HttpSession对象
我们看以在Action通过ActionContext来访问server API的实例
login.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<body>
<form action="login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
该表单提交数据后,Struts2会将数据交给LoginAction.java的Action处理,
LoginAction.java代码:
package com;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
ActionContext ac = ActionContext.getContext();
if("admin".equals(username)&&"admin".equals(password)){
ac.put("success", "登陆成功");
return Action.SUCCESS;
}else{
ac.put("error","用户名或密码错误");
return Action.ERROR;
}
}
}
这是处理用户请求的Action,该Action首先会获得ActionContext,然后判断请求的参数的username和密码是否正确,如果全正确则返回success,如果错误则返回error,并把提示信息返回到视图上
struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="org" extends="struts-default" namespace="/">
<action name="login" class="com.LoginAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
errot.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
${requestScope.error}
</body>
</html>
相关文章推荐
- 使用ActionContext类访问Servlet API
- 3.5.4: 使用ServletActionContext访问Servlet API
- Action使用ActionContext访问Servlet API
- 使用ServletActionContext访问Servlet API
- Action访问Servlet API(通过ActionContext类访问)
- Struts2(6):使用ServletActionContext访问Servlet API
- 3.5.3: Action直接访问Servlet API
- 在Action中使用Servlet API
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- 详解Struts2中Action访问Servlet API的几种方法
- Action 访问Servlet API的三种方法
- 3、Action中访问Servlet API
- Struts2 访问Servlet API(ActionContext)以及校验框架
- Struts2中Action访问Servlet API的两种方法
- Struts2的Action访问Servlet API的方式
- Structs2 Action访问Servlet API的三种方式
- 使用IoC方法访问Servlet API
- Struts 2读书笔记-----Action访问Servlet API
- Action访问Servlet API