【问题标题】:Why this NPE when I request for Struts2 action?当我请求 Struts2 操作时为什么会出现此 NPE?
【发布时间】:2016-02-10 03:58:33
【问题描述】:

我正在使用 Struts 和 Struts 2.x,当我尝试从 Struts 2 表单向操作类发出请求时遇到异常。

File:   org/apache/struts2/impl/StrutsActionProxy.java
Line number:    69
Stacktraces
java.lang.NullPointerException   org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:554)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)

如果我的理解没有错的话。当我通过登录按钮发出请求时,它会转到struts.xml,它会寻找动作类。

似乎它无法找到我所理解的动作类,我也参考了这个page,看起来很相似,但我无法弄清楚我的代码中到底有什么问题。 我的struts.xml,login.jsp和Login类资源代码如下:

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />

    <package name="default" extends="struts-default">
        <action name="login" class="com.myapp.strust2x.web.action.LoginAction" method="execute">
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
</struts>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
     <s:form action="Login" method="post">
        <s:textfield key="userName" label="User Name"/><br />
        <s:password key="passowrd" label="Password"/><br />
        <s:submit value="Login" label="Login" />
    </s:form>
</body>
</html>

登录操作

package com.myapp.strust2x.web.action;

    import com.myapp.Constant;
    
    public class LoginAction {
    
        private String userName;
        private String passowrd;
    
        public LoginAction() {
            System.out.println("now Object Created LoginAction");
        }
        
        public String execute() throws Exception {
            System.out.println("HI i am from hello");
            return Constant.SUCCESS;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassowrd() {
            return passowrd;
        }
    
        public void setPassowrd(String passowrd) {
            this.passowrd = passowrd;
        }
    
    }

【问题讨论】:

    标签: java jsp struts2 nullpointerexception action-mapping


    【解决方案1】:

    您可能在您的web.xml 中使用了已弃用的FilterDispatcher。将其替换为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    更多信息here

    【讨论】:

    • 如果您将文档链接到正确的包,您怎么可能报告错误的包?请修复它
    • 我使用相同的过滤器:org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter :(
    • @KaroLa 如果您使用的是该软件包,那是一个错误。查看更正后的包
    【解决方案2】:

    第 69 行 StrutsActionProxy 中的 NullPointerException。错误消息应该是

    There is no Action mapped for namespace [xxx] and action name [yyy] associated with context path [zzz]
    

    这里的命名空间不为空。命名空间由动作映射确定。如果 URL 中有最后一个斜杠,那么动作映射器将使用您的动作名称作为命名空间,这是错误的,您最好在包配置中明确定义命名空间并在 Struts JSP 标记中使用命名空间属性。在 URL 中使用操作扩展可以帮助您解决最后一个斜杠问题。

    【讨论】:

      【解决方案3】:

      我现在想通了,我在 struts.xml 中的动作名称带有“Login”,而在 struts 表单中它的“login”是导致问题。 这意味着名称对操作区分大小写名称Loginlogin 不同,我之前没有注意到L vs l

      在 Login.jsp 中:

      <body>
           <s:form action="Login" method="post">
      

      在 Strust.xml 中

      <action name="login" class="com.myapp.strust2x.web.action.LoginAction" method=...
      

      一旦我更改为相同的,它现在对我有用。

      【讨论】:

        猜你喜欢
        • 2012-10-08
        • 2019-11-11
        • 1970-01-01
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 2017-03-19
        相关资源
        最近更新 更多