[英]Why this NPE when I request for Struts2 action?
我是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
並查找操作類。
似乎無法找到我理解的動作類,我也參考了此頁面 ,看起來很相似,但是我無法弄清楚代碼中到底是什么問題。 我的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>
LoginAction
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;
}
}
您可能在web.xml
使用了不贊成使用的FilterDispatcher
。 將其替換為org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
。
更多信息在這里 。
第69行的StrutsActionProxy
中的NullPointerException
。錯誤消息應該是
There is no Action mapped for namespace [xxx] and action name [yyy] associated with context path [zzz]
並且此處的命名空間不為空。 名稱空間由操作映射確定。 如果URL中的最后一個斜杠,則動作映射器將使用您的動作名稱作為名稱空間,這是錯誤的,您最好在程序包配置中顯式定義名稱空間,並在Struts JSP標記中使用名稱空間屬性。 在URL中使用操作擴展名可以幫助您解決最后的斜杠問題。
我現在想通了,我在struts.xml中使用“ Login ”輸入動作名稱,而在struts表單中使用“ login ”則引起問題。 這意味着名稱對操作區分大小寫名稱Login
和login
是不同的,我之前沒有注意到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=...
一旦我將兩者都更改為相同,它現在對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.