一、首先新建一个Dynamic Web Project,准备好需要的Struts的包,我使用的包有这些,仅供参考,可能少了一些包也是可以运行的。工程建立好之后,将这些包复制进lib目录下,同时右击选中所有的包,选择bulid path->add to bulid path
这个是我的web.xml配置文件
<?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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三、接下来配置Struts的核心文件Struts.xml,在src目录下创建Struts.xml文件,此文件处理action和视图之间的映射关系
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="test" extends="struts-default">
<!-- 配置默认首页 -->
<default-action-ref name="index"/>
<action name="index">
<result type="dispatcher">/login.jsp</result>
</action>
<!-- 定义action的名字和实现类 -->
<action name="login" class="com.login.LoginAction">
<!-- 定义action返回值对应的视图 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
四、创建处理用户请求的类LoginAction,该类继承了com.opensymphony.xwork2.ActionSupport,其中excute方法为默认的处理请求的方法
package com.login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 4757787836938397352L;
private String username;
private String password;
@Override
public String execute() throws Exception {
System.out.println("username=" + username + " password=" + password);
if ("admin".equals(username) && "123".equals(password)) {
return SUCCESS;
} else {
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;
}
}
最后贴上页面的代码
登录成功界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>登录成功界面</title>
</head>
<body>
hello, welcome success page!
</body>
</html>
登录失败界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>登录错误界面</title>
</head>
<body>
failed!!!!
</body>
</html>
登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>登录界面</title>
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<td><label>登录名</label></td>
<td><input type="text" id="username" name="username"/></td>
</tr>
<tr>
<td><label>密 码</label></td>
<td><input type="password" id="password" name="password"/></td>
</tr>
<tr><td><input type="submit" value="提交"/></td></tr>
</table>
</form>
</body>
</html>
PS,我在搭建环境中遇到的大部分问题都是配置错了,或者变量名字错误,少导入包之类的错误,引以为鉴