1.新建***页项目Strutsdemo1

2.导入相关jar包,找到F:\jar\struts-2.3.31\apps\struts2-blank\WEB-INF\lib下的所有jar包全部导入即可。

3.在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">
<!-- Struts2的功能的初始化是通过过滤器引入 -->

    <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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>


</web-app>

4.创建formaction.java

package com.broccoli.action;
/** * @author zhanghongkuan * @date 2016-11-30 */
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class formdata extends ActionSupport{

    private static final long serialVersionUID = 1L;

    @Override
    public String execute() throws Exception {

        HttpServletRequest request = ServletActionContext.getRequest();     
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if("admin".equals(username)&&"123456".equals(password)){
            return "success";
        }
        else{
            System.out.println("用户名或密码错误!");
        return "error";
    }
}
}

5.配置struts.xml文件

<?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="demo1" namespace="/" extends="struts-default">
        <action name="formdata" class="com.broccoli.action.formdata">
        <result name="success">/success.jsp</result>
        <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>


6.创建login.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
     <%@taglib uri="/struts-tags" prefix="s" %>

<!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>

   <form action="formdata" method="post">
       用户名:<input name="username" type="text" ><br>
       密    码 :<input name="password" type="text" ><br>
         <input type="submit" value="登录">
   </form>
</body>
</html>

7.创建success.jsp文件

<%@ page language="java" contentType="text/html; charset=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=ISO-8859-1">
<title>成功登陆</title>
</head>
<body>
<h1>成功登陆!</h1>
<h1>Struts2 is running.....</h1>
</body>
</html>

8.创建error.jsp文件

<%@ 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>
<h1>登录失败,请核对用户名和密码!</h1>
</body>
</html>

9.登录页面

10.登录成功页面