1. 下载 Struts 2 , http://struts.apache.org/download.cgi
2. 将 Struts2 的 lib 文件夹下的
commons-fileupload-1.3.1jar,
commons-io-2.2.jar,
commons-lang3-3.2.jar,
commons-logging-1.1.3.jar,
freemarker-2.3.19.jar,
javassist-3.11.0.GA.jar,
ognl-3.0.6.jar,
struts2-core-2.3.20.jar,
xwork-core-2.3.20.jar"
必需类库复制到 Web 应用的 WEB-INF/lib 路径下。
如果需要在 DOS 或者 Shell 窗口手动编译 Struts2 相关的程序,则还应该将 struts2-core-2.2.1.jar 和 xwork-core-2.2.1.jar 添加到系统的 CLASSPATH 环境变量里。
3.编辑 Web 应用的 web.xml 配置文件,增加 Struts2 的核心 Filter 。
web.xml 代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_3_0.xsd"
version="3.0"
metadata-complete="false">
<!-- 定义 Struts 2 的核心 Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 让 Struts 2 的核心 Filter 拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4. Eclipse 或 myEclipse 中使用 Struts2
经过第2、3步的操作后,该 Web 应用完全具备了 Struts 2 框架的支持。
下面将做个简单的 Demo (登录处理流程):
1. login.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" key="user"/>
<s:textfield name="password" key="pass"/>
<s:submit key="login"/>
</s:form>
</body>
</html>
注:以上 form 表单代码使用了 Struts 2 标签库定义了一个表单和三个简单表单域
2. welcome.jsp (登录成功)代码如下:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><s:text name="succPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
<s:text name="succTip">
<s:param>${sessionScope.user}</s:param>
</s:text><br/>
</body>
</html>
3. error.jsp (登录失败)代码如下:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><s:text name="errorPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
<s:text name="failTip"/>
</body>
</html>
4. 为了让 Struts 2 应用运行起来,还必须为 Struts 2 框架提供一个配置文件:struts.xml 文件。该文件应该放在 Web 应用的类加载路径下 (一般在 src 下) 代码如下:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- 指定Struts 2配置文件的根元素 -->
<struts>
<!-- 指定全局国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="mess"/>
<!-- 指定国际化编码所使用的字符集 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<!-- 所有的Action定义都应该放在package下 -->
<package name="lee" extends="struts-default">
<action name="login" class="hsl.action.LoginAction">
<!-- 定义三个逻辑视图和物理资源之间的映射 -->
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
5. 资源文件 mess.properties 该文件放在 src 下 代码如下:
loginPage=登录页面
errorPage=错误页面
succPage=成功页面
failTip=对不起,您不能登录!
succTip=欢迎,{0},您已经登录!
user=用户名
pass=密 码
login=登录
注意:以上 资源文件必须用 native2ascii 命令来处理该国际化资源文件。如下:
要使用 native2ascii 命令必须先将 JDK 安装目录下的 bin 路径 添加到系统环境变量 path 里边才能使用
汉字转换成 Unicode 编码
把转码后的 mess.properties 文件放入 src 文件夹
Unicode编码 转换成 汉字命令如下:
native2ascii -reverse mess.properties aa.txt
6. 定义 Struts2 的 Action . Action 通常应该继承 ActionSupport 基类。
src\hsl\action\LoginAction.java 代码如下:
package hsl.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
// 定义封装请求参数的username和password属性
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;
}
// 定义处理用户请求的execute方法
public String execute() throws Exception {
// 当username为hsl,password为hslhe时即登录成功
if (getUsername().equals("hsl") && getPassword().equals("hslhe")) {
ActionContext.getContext().getSession().put("user", getUsername());
return SUCCESS;
} else {
return ERROR;
}
}
}
7. 至此,整个 Struts 2 应用完全建立成功,测试运行结果如下: