,步骤如下1,在Eclipse中建立tomcat项目2,拷贝struts2需要的jar文件需要copy的jar文件如下
struts2-core-2.1.6.jar
xwork-2.1.2.jar
ognl-2.6.11.jar
freemarker-2.3.13.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar以上的文件可以从apache网站下载的struts2的压缩包中得到
把以上的文件copy到WEB-INF/lib目录下3,建立login页面
文件名:index.html
文件位置:work目录下
文件内容:<meta http-equiv="content-type" content="text/html;charset=gb2312">
<title>login</title><form action="login.action" method="post">
username:<input type="input" name="username"><br>
password:<input type="input" name="password"><br>
<input type="submit" value="登录">
</form>4,建立login成功页面和login失败页面
文件名:loginSuccess.html
文件位置:work目录下
文件内容:
<meta http-equiv="content-type" content="text/html;charset=gb2312">
欢迎您,登录成功。文件名:loginFailure.html
文件位置:work目录下
<meta http-equiv="content-type" content="text/html;charset=gb2312">
登录失败,<a href="http://michelle0620.blog.163.com/blog/index.html">点击这里重新登录。</a>5。建立action
文件名:LoginAction.java
包名:struts2.login
文件位置:WEB-INF/src目录+包的目录的下面
文件内容:
package struts2.login;public class LoginAction {
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;
}
public String execute() {
System.out.println (LoginAction.class.hashCode());
if (username.equalsIgnoreCase("aaa") &&
password.equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}}
6,设定struts.xml文件
文件位置:WEB-INF/src目录下
文件内容:
<?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> <package name="default" extends="struts-default">
<action name="login" class="struts2.login.LoginAction">
<result name="loginSuc">loginSuccess.html</result>
<result name="loginFail">loginFailure.html</result>
</action>
</package></struts>7,设定web.xml文件
文件位置:WEB-INF目录下
文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list></web-app>8,启动,测试全部OK了,点击小猫启动tomcat,

打开浏览器,输入如下地址http://localhost:8080/struts2/work/

用户输入aaa,密码输入aaaaaa,登陆成功出现ok画面

用户输入aaa,密码输入aaaaaaaaa,登陆失败出现ng画面

本文出自 “点点滴滴” 博客,请务必保留此出处http://kin111.blog.51cto.com/738881/163523