0

用这个把我的头发扯掉。尝试在Struts中做一个小型登录应用程序 1. 没有数据库连接,只要用户名和密码相同,就可以登录。

我使用了几个不同的教程。当我运行应用程序时,我的第一页login.jsp会加载。如果无论是否在用户名和密码框中输入内容都单击提交,我会收到 HTTP 500 错误。有人可以帮助我吗?

错误:

0000001a webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[action]: java.lang.NullPointerException
    at struts.action.LoginAction.execute(LoginAction.java:17)
    at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:53)
    at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:64)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:48)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:280)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:459)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3935)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)

登录操作.java

package struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import struts.form.LoginForm;

public class LoginAction extends org.apache.struts.action.Action {
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginForm loginForm = (LoginForm) form;
        if (loginForm.getUsername().equals(loginForm.getPassword())) {
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(FAILURE);
        }
    }
}

登录表单.java

package struts.form;

import org.apache.struts.action.*;

public class LoginForm extends ActionForm{

    private String username;
    private String password;


    public LoginForm() {
        super();
    }
    private static final long serialVersionUID = 104092268304152302L;


    public String getUsername() {

    return username;
    }

    public void setMessage(String username) {

    this.username = username;   
}

    public String getPassword() {

        return password;
        }

        public void setPassword(String password) {

        this.password = password;   
    }
}

struts-config.xml

    <?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <form-beans>
        <form-bean name="LoginForm" type="struts.form.LoginForm"/>
    </form-beans>
    <global-forwards />

    <action-mappings>
        <action name="LoginForm" path="/Login" scope="session" type="struts.action.LoginAction" >
            <forward name="success" path="/success.jsp" />
            <forward name="failure" path="/failure.jsp" />
        </action>
    </action-mappings>

</struts-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
    <display-name>StrutsExample</display-name>
<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>
<init-param>
  <param-name>debug</param-name> 
  <param-value>2</param-value> 
  </init-param>
  <init-param>
  <param-name>detail</param-name> 
  <param-value>2</param-value> 
  </init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
  <session-timeout>30</session-timeout> 
  </session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

登录.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
    <html:form action="/Login" >
        User Name :<html:text name="LoginForm" property="username" />
        Password  :<html:password name="LoginForm" property="password" />
        <html:submit value="Login" />
    </html:form>
</body>
</html>
4

1 回答 1

0

您的表单 bean 中没有任何setUsername()方法。所以Struts在提交表单时无法在表单bean中设置用户名,所以表单中的用户名为null,从而loginForm.getUsername().equals(...)抛出NullPointerException。

setMessage()将方法重命名为setUsername().

另请注意,您不需要在表单标签中设置 name 属性。只需设置属性属性就足够了。

于 2013-04-16T20:35:12.193 回答