0

这是我的第一个 strut 应用程序,但我仍然没有做对。这是一个简单的登录应用程序。如果有人使用管理员/访问者凭据,他/她会进入管理员/访问者的登录页面。尽管在我的情况下,当我输入登录详细信息时,它会再次返回登录页面。请帮忙

1)LoginAction

package com.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.struts.form.LoginForm;;

public final class LoginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

        LoginForm loginForm = (LoginForm) form;

        // validating login
        if (loginForm.getUname().equals("abc")
                && loginForm.getPwd().equals("abc")) {
            return mapping.findForward("admin");
        } else if (loginForm.getUname().equals("sdf")
                && loginForm.getPwd().equals("sdf")) {
            return mapping.findForward("visitor");
        } else {
            return mapping.findForward("error");
        }

    }
}

2)登录表格

package com.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm {

    private static final long serialVersionUID = 1L;
    private String uname = "";
    private String pwd = "";

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUname() {

        return uname;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPwd() {

        return pwd;
    }

    // validating form errors
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if (uname == null || uname == "") {
            errors.add("uname", new ActionError("error.uname.required"));
        }

        if (pwd == null || pwd == "") {
            errors.add("pwd", new ActionError("error.pwd.required"));
        }

        return errors;

    }
}

3)struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <form-beans>
        <form-bean name="LoginForm" type="com.struts.form.LoginForm"/>
    </form-beans>


    <action-mappings>
        <action input="/login.jsp" type="com.struts.action.LoginAction" name="LoginForm" path="/login" scope="request" validate="true">
            <forward name="admin" path="/admin.jsp"/>
            <forward name="visitor" path="/visitor.jsp"/>
            <forward name="error" path="/error.jsp"/>
        </action>
    </action-mappings>

</struts-config>

4)web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!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 >
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>/login.jsp</welcome-file>
</welcome-file-list>

<taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</web-app>

5) 登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>Login Page</title>
    </head>
    <body bgcolor="#f6f7f8">
        <center>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <html:errors/>

        <html:form action="login">
            <table>
                <tr>
                    <td align="right">Username:</td>
                    <td><html:text name="LoginForm" property="uname"/></td>
                </tr>
                <tr />
                <tr>
                    <td align="right">Password:</td>
                    <td><html:password name="LoginForm" property="pwd"/></td>
                </tr>
                <tr>
                    <td><html:submit value="Login"/></td>
                    <td><html:reset value="Reset"/></td>
                </tr>
            </table>
        </html:form>
        </center>
    </body>
</html> 
4

0 回答 0