Web应用中通常需要访问的ServletAPI就是HttpServletRequest,HttpSession,ServletContext,这三个类分别代表jsp内置对象中的request,session, application。Struts2提供了一个ActionContext类,action可以通过这个类来访问ServletAPI。
具体看以下例子,实现的是一个网站访问计数器的功能:
1、登录页面:输入用户名、密码后提交
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:form action="loginAction">
<s:textfield name="username" key="user"></s:textfield>
<s:textfield name="password" key="pass"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
</body>
</html>
2、web.xml配置文件
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 定义struts2的FIlterDispatcher的FIlter -->
<filter>
<!-- 定义核心filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 定义核心filter的实现类 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- FilterDispatcherY哦你过来初始化Struts2并且处理所有的请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、Struts.xml配置文件:
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.0.dtd">
<!-- 指定struts2配置文件的根元素 -->
<struts>
<!-- 指定全局国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
<!-- 指定国际化编码所使用的字符集 -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<!-- 所有的action定义都应该放在package内部 -->
<package name="struts2" extends="struts-default">
<action name="loginAction" class="com.struts2.LoginAction">
<!-- 定义3个逻辑视图和物理资源之间的映射 -->
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
4、LoginAction.java类:
public class LoginAction extends ActionSupport{
private String username;
private String password;
private String result;
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;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public String execute() throws Exception {
ActionContext context = ActionContext.getContext();
//通过ActionContext访问application范围内的属性
Integer count = (Integer)context.getApplication().get("counter");
if(count == null){
count = 1;
}else {
count ++;
}
//通过ActionContext设置application范围内的属性
context.getApplication().put("counter", count);
//通过ActionContext设置session范围内的属性
context.getSession().put("user", getUsername());
if(getUsername().equals("scott") && getPassword().equals("tiger")){
//通过ActionContext设置request范围内的属性
context.put("tip", "服务器提示:您已经成功登陆");
this.result = this.getText("succTip", new String[]{this.username});
return SUCCESS;
}
{
//通过ActionContext设置request范围内的属性
context.put("tip", "服务器提示:登陆失败");
this.result = this.getText("failTip");
return ERROR;
}
}
}
5、错误页面:
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'error.jsp' starting page</title>
</head>
<body>
${sessionScope.user },登陆失败<br/>
${requestScope.tip }<br/>
<s:property value="result"></s:property>
</body>
</html>
6、成功登陆页面:
success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
本站访问次数为:${applicationScope.counter }<br/>
${sessionScope.user },您已经登陆成功<br/>
${requestScope.tip }<br/>
<s:property value="result"></s:property>
</body>
</html>