基于Struts2的用户登录程序

基本步骤:

1、新建Java工程,File>New>Project>Web>Dynamic Web Project,并将工程命名为:Struts2_Demo

2、导入struts2的重要jar包到WebContent>lib下

3、新建web.xml,右击WEB-INF>New>Other>XML>XML File,并命名为web.xml

    更改其内容如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <display-name>Struts2_Demo</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>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

4、新建struts.xml,右击Java Resources>src>new>Other>XML>XML File,并命名为struts.xml

5、新建action包,右击Java Resources>src>New>Package,并命名为acion

6、新建LoginAction类,右击action>New>Class,并并名为LoginAction类

    并修改其内容如下:

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String username;
    private String passward;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassward() {
        return passward;
    }
    public void setPassward(String passward) {
        this.passward = passward;
    }
    
    public String execute(){
        if(username.equals("admin") && passward.equals("admin")){
            return "success";
        }
        else{
            return "fail";
        }
    }
}

 

7、新建login页面,右击WebContent>New>JSP File,并命名为login.jsp

    修改其内容如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Welcome To Login</title>
    </head>
    <body>
    <center>
    <h1>用户登录</h1>
        <s:form action="LoginAction">
            <table>  
            <tr>  
                <td>用户名:</td>  
                <td><input type="text" name="username" ></td>  
            </tr>  
            <tr>  
                <td>密码:</td>  
                <td><input type="text" name="passward"></td>  
            </tr>  
            <tr>  
                <td><input type="submit" value="登录"></td>  
                <td><input type="reset" value="重置"></td>  
            </tr>  
        </table>  
        </s:form>
     </center>
    </body>
</html>

 

8、新建loginsuccess页面,右击WebContent>New>JSP File,并命名为loginsuccess.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
登录成功
</body>
</html>

 

9、新建loginfail页面,右击WebContent>New>JSP File,并命名为loginfail.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
登录失败
</body>
</html>


10、配置struts.xml,修改其内容如下:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>
    <package name="action" extends="struts-default">
        <action name="LoginAction" class="action.LoginAction">
            <result name="success">/loginsuccess.jsp</result>
            <result name="fail">/loginfail.jsp</result>
        </action>
    </package>
</struts>

 

11、右击工程Struts_Demo>Run As>Run on Server运行即可,效果如下

 

 

posted @ 2015-04-28 21:28  哈特13  阅读(371)  评论(0编辑  收藏  举报