NullPointer异常 - Struts 1

时间:2018-03-03 14:54:52

标签: jsp struts struts-1

My Struts Config XML

<struts-config> 
    <form-beans> 
        <form-bean type="org.apache.struts.action.ActionForm.LoginForm" name="fo mLogin"></form-bean> 
    </form-beans> 

    <global-exceptions> 

    </global-exceptions> 

    <global-forwards>
        <forward name="welcome" path="/Welcome.do"/>
    </global-forwards> 

    <action-mappings>
        <action path="/validateLogin" type="com.myapp.struts.action.LoginAction" input="/" name="formLogin">
            <forward name="success" path="/welcomeStruts.jsp"/>
            <forward name="error" path="/mylogin.jsp"/> 
        </action> 
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings> 

    <controller processorClass="org apache . struts . tiles .TilesReguestProcessor"/> 

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>

Login Form Class

public class LoginForm extends ActionForm {

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

LoginAction class

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    LoginForm login = (LoginForm) form;
    System.out.println("UserName:" + login.getUsername());
    System.out.println("Password :" + login.getPassword());
    IfaceLogin iface = new ImplLogin();
    login = iface.validLogin(login);
    if (login != null) {
        System.out.printlnr("Hari");
        return mapping.findForward(SUCCESS);

    } else {
        return mapping.findForward("error");
    }
}

MyLogin JSP

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> 
<%@page contentType="text/html" pageEncoding="LITF-8 "%> 
<!DOCTYPE html> 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html;"/>
        <title>,7M Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <html:form action="/validateLogin.do">
            <html:text property="username" />
            <html:password property="password" />
            <input type="submit" value="Login"/>
        </html:form>
    </body>
</html>

Error

Info: Loading application [StrutsiTest] at [/Struts1Test]
Info: StrutslTest was successfully deployed in 1,827 milliseconds.
Warning: Resource org/apache/struts/action/ActionResources_en_US.properties Not Found.
Warning: Resource org/apache/struts/action/ActionResources_en.properties Not Found.
Severe: Error creating form bean of class org.apache.struts.actionl.ActionForm.LoginForm java.lang.NullPointerException
        at org.apache.struts.config.FormBeanConfig.createActionForm(FormBeanConfig.java:289)
        at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:259)
        at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:213) at org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:526)
        at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:503)
        at org.apache.jsp.mylogin_jsp._jspx_meth_html_form 0(mylogin_jsp.java:100)
        at org.apache.jsp.mylogin_jsp._jspService(mylogin_jsp.java:73)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:-1)

类加载器无法使用名称加载类。确保您可以使用正确的FQCN类。

如果您将null替换为表单类的FQCN,则此方法不应返回getType()

protected Class formBeanClass() {
    ClassLoader classLoader =
        Thread.currentThread().getContextClassLoader();

    if (classLoader == null) {
        classLoader = this.getClass().getClassLoader();
    }

    try {
        return (classLoader.loadClass(getType()));
    } catch (Exception e) {
        return (null);
    }
}