问题
I am new to struts and Struts 2.x, I am getting below exception when i am trying to make request to action class from Struts 2 form.
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)
If my understanding is not wrong, When I made request via login button it will go to struts.xml
and it will look for action class.
Seems it is not able to locate my action class is what i understand and I have refer this page too, looks similar but I could not figure out what exactly the problem is in my code.
My struts.xml
,login.jsp
and Login class recourse code is as follows:
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;
}
}
回答1:
You are probably using the deprecated FilterDispatcher
in your web.xml
. Replace it with org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
.
More information here.
回答2:
The NullPointerException
in StrutsActionProxy
at line 69. The error message should be
There is no Action mapped for namespace [xxx] and action name [yyy] associated with context path [zzz]
and namespace is not empty here. The namespace is determined from the action mapping. If you have a last slash in the URL then action mapper will use your action name as a namespace, this is wrong you'd better explicitly define a namespace in the package configuration and use namespace attribute in the Struts JSP tags. Using an action extension in the URL could help you with the last slash problem.
回答3:
I figured it out now, I am having action name in the struts.xml is with "Login" and in the struts form it is having "login" is is causing problem.
This means names are case sensitive for action Name Login
and login
are different, I did not noticed earlier L vs l
in Login.jsp :
<body>
<s:form action="Login" method="post">
in Strust.xml
<action name="login" class="com.myapp.strust2x.web.action.LoginAction" method=...
Once i change to both same it worked now for me.
来源:https://stackoverflow.com/questions/35306548/why-this-npe-when-i-request-for-struts2-action