1、eclipse,mysql,apache tomcat的安装和配置见前面几篇文章
建一个Dynamic Web Project,取名叫OATest
2、struts2配置
下载struts2需要的的完整包,将其中的以下文件复制到eclipse的对应web工程的WebContent/WEB-INF/lib的lib目录下:
1)xwork-core- 2)struts2-core- 3)ognl- 4)freemarker- 5)commons-io-6)commons-fileupload-7)javassist-8)commons-lang3-
3、struts2修改web.xml
修改web.xml增加过滤器filter,最终配置文件如下所示
<?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>
<constant name="struts.i18n.encoding" value="GBK"></constant>
<package name="default" extends="struts-default">
<action name="loginAction" class="com.oa.action.LoginAction">
<result name="success">welcome.jsp</result>
<result name="error">err.jsp</result>
</action>
</package>
</struts>
4、定义表单的JSP页面
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:form action="loginAction">
<c:textarea name="username" key="username"></c:textarea>
<c:textarea name="password" key="password"></c:textarea>
<input type="submit" value="提交"/>
</c:form>
</body>
</html>
5、定义拦截类com.oa.action.LoginAction
注意,包为com.oa.action
package com.oa.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String execute() {
if (username.equals("a") && password.equals("a"))
return SUCCESS;
return ERROR;
}
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;
}
}