用struts2.0做登陆怎么做!谁能帮住我一下谢谢!不用通过数据库!

解决方案 »

  1.   

    楼主你要的是strtus2的登录范例?我是这么理解的,代码如下:
    login.jsp:<%@ page language="java" contentType="text/html; charset=GBK"%>
    <html>
    <head>
    <title>登录页面</title>
    </head>
    <body>
    <form action="Login.action" method="post">
        <table align="center">
        <caption><h3>用户登录</h3></caption>
            <tr>
                <td>用户名:<input type="text" name="username"/></td>
            </tr>
            <tr>
                <td>密&nbsp;&nbsp;码:<input type="text" name="password"/></td>
            </tr>
            <tr align="center">
                <td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
            </tr>
        </table>
    </form>
    </body>
    </html>
    welcome.jsp:<%@ page language="java" contentType="text/html; charset=GBK"%>
    <html>
        <head>
            <title>成功页面</title>
        </head>
        <body>
            欢迎,${sessionScope.user},您已经登录!<br>
        </body>
    </html>
    error.jsp:<%@ page language="java" contentType="text/html; charset=GBK"%>
    <html>
        <head>
            <title>错误页面</title>
        </head>
        <body>
            您不能登录!
        </body>
    </html>
    web.xml:<?xml version="1.0" encoding="GBK"?>
    <web-app 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">
    <!-- 定义Struts2的FilterDispathcer的Filter -->
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        </filter> <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping></web-app>
    struts.xml:<?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <package name="test" extends="struts-default">
            <action name="Login" class="test.LoginAction">
                <result name="error">/error.jsp</result>
                <result name="success">/welcome.jsp</result>        
            </action>
        </package>
    </struts>
    LoginAction.java:package test;import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;public class LoginAction implements Action
    {
        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() throws Exception
    {
            if (getUsername().equals("scott")
                    && getPassword().equals("tiger") )
    {
    ActionContext.getContext().getSession().put("user" , getUsername());
                return SUCCESS;
            }
    else
    {
                return ERROR;
            }
        }
    }