Struts 2 action class not working - java

I am using struts2 for developing a web application.
I have include the required jars for struts2 but when it is going to call the struts action class it is throwing 404 error.
There is no error on console and browser does not showing .action extension whitch it shows when struts.xml call an action class.
I am using jdk 1.6 and struts 2.0.
Am I missing any jar who is responsible for all this.
In jsp I am simply calling the function from
<s:form action = "Mergexmlaction" method = "post"/>
Here is my struts.xml and web.xml
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.multipart.maxSize" value="6000000000" />
<package name="default" namespace="/jsp" extends="struts-default">
<action name="Mergexmlaction" class="com.hm.merge.mergeaction.Mergexmlaction">
<result name="success" >/jsp/Result.jsp</result>
<result name="error" >/jsp/Browse_multiplexmlfiles.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">600000000</param>
</interceptor-ref>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="xml_file_merging" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>xml_file_merging</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>jsp/Browse_multiplexmlfiles.jsp</welcome-file>
</welcome-file-list>
</web-app>

There are a few issues.
<package name="default" namespace="/jsp" extends="struts-default">
1) I'd recommend against having a namespace of "jsp", it doesn't make any sense. Namespaces should be something meaningful to the application and/or user.
<action name="Mergexmlaction" class="com.hm.merge.mergeaction.Mergexmlaction">
2) Don't name an action with "action", there will either be a .action extension, or no extension at all. Either way, there's no reason to duplicate "action" in the first case, and no reason to use "action" if there's no extension. Just "mergexml", "mergeXml", etc.
<result name="success" >/jsp/Result.jsp</result>
3) I recommend putting your JSP pages under WEB-INF to avoid direct client access.
<interceptor-ref name="fileUpload">
4) Once you declare any interceptors, you must declare all interceptors. This action has only a single interceptor running. It's possible this is okay, but it's almost never the right thing to do.
<welcome-file>jsp/Browse_multiplexmlfiles.jsp</welcome-file>
5) And this is the ultimate issue, depending on how you're accessing the application. You show the welcome file as being a JSP page, which is presumably using S2 tags. This won't work: the tags depend on their being a complete S2 request, a value stack, etc.
All access to an S2 app should take place through an S2 action, not a JSP. If you look at the rendered HTML for the directly-accessed JSP you'll see neither namespace nor action extension rendered.

an error 404 is a bad link. means you just are calling a link that doesnt exist. Check your mapping in your struts config file and make sure you use the correct url.

The namespace attribute of package element is optional and if it is not present, the default value "/" is assumed. If the namespace attribute has a non-default value, the namespace must be added to the URI that invokes the actions in the package.
For example, the URI for invoking an action in a package with a default namespace is this:
/context/actionName
To invoke an action in a package with a non-default namespace, you need this URI:
/context/namespace/actionName

Related

Action Mapping in Struts2 [duplicate]

This question already has answers here:
There is no Action mapped for namespace [/] and action name [login] associated with context path [/Struts2Test]
(4 answers)
Closed 2 years ago.
Currently migrating an app from Struts1 to Struts2.
When I click my "submit" button, it gives me an error saying "There is no Action mapped for action name requestInput". What is wrong with my code that is the cause of this error?
web.xml
<web-app>
<display-name>My Project</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<struts>
<include file="struts-default.xml" />
<constant name="struts.custom.i18n.resources" value="global" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor-stack name="uploadFile">
<interceptor-ref name="fileUpload" />
<interceptor-ref name="uploadForm" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="basicStack" />
</interceptor-stack>
</interceptors>
<action name="requestInput" class="com.class.action.FileAddAction" method="execute">
<result>/result.jsp</result>
</action>
</package>
</struts>
FileAdd.jsp
<s:form method="POST" action="requestInput" enctype="multipart/form-data">
...
<s:submit property="submit" style="background:#dccaa0" value="Submit" theme="simple"/>
...
</s:form>
FileAddAction.jsp
public final class FileAddAction extends ActionSupport implements SessionAware, ServletRequestAware {
...
public String execute() throws Exception{
...
}
}
And, my struts.xml is in the Java src folder and in the WEB-INF folder.
What else can I do to fix this? Thanks..
Two issues, otherwise looks good.
First issue:
action name="requestInput" class="com.class.action.FileAddAction"
method="execute"
You can't name your package with the identifier class, it is not a valid Java identifier
Second issue:
interceptor-ref name="uploadForm"
What is "uploadForm"? It's definitely not provided by Struts 2, see the list.
By naming the package differently, and removing interceptor-ref uploadForm, i am able to click the FileAdd.jsp Submit button without any issue and it is redirecting me successfully to result.jsp. I used v2.3.37.
Note:
struts.xml should be in the src/main/resources folder (struts.xml must be on the web application’s root class path). Check this out.
web.xml is to be under src/main/webapp/WEB-INF folder. Check this out.

Struts 2 application throws 404 exception

In my struts 2.3.16.1 application, after filter mapping in the web.xml "resource not found exception" occurs .Even the jsp pages in web directory also shows the same exception.
Thanks for your help in advance.
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
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>
<package name="default" extends="struts-default">
<action name="getTutorial" class="org.kishore.struts2.TutorialAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
If your error is
type Status report
message /YourProjectName/success.jsp
description The requested resource is not available.
Then it means your success.jsp page is missing
And if message is blank and in console error is
Unable to load configuration. - action - file:/C:/Users/Parth/workspace1/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/serializ/WEB-INF/classes/struts.xml:7:75
then your class org.kishore.struts2.TutorialAction.java is missing
If you are getting following error
SEVERE: Dispatcher initialization failed Unable to load configuration. - bean - jar:file:/F:/apache-tomcat-6.0.39/wtpwebapps/struts2starter/WEB-INF/lib/struts2-‌​portlet-plugin-2.3.16.1.jar!/struts-plugin.xml:31:133 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(Configurati‌​onManager.java:70)
Caused by: java.lang.ClassNotFoundException: javax.portlet.PortletMode
It means download Portlet Api 2.0.jar because without it you will not be able to use struts2-‌​portlet-plugin-2.3.16.1.jar. If you do not add Portlet Api 2.0.jar you will continue getting error java.lang.ClassNotFoundException: javax.portlet.PortletMode
Note: Also make sure you are using compatible version of jars which have same version as struts-core-2.x.x.jar.
For example if you are using struts2-core-2.2.3.jar then you must use struts2-‌​portlet-plugin-2.2,3.jar.

The requested resource is not available in struts2 when action class is configured in struts.xml

I've tried to create a new action class and add it to struts.xml, the action class successfully get compiled but when I run tomcat it shows The requested resource is not available 404 error on index page itself.
CreateUser.jsp
<s:form action = "CreateUserAction" >
<s:textfield name = "name" label = "Name" />
<s:textfield name = "userName" label = "User Name" />
<s:submit value = "Register" />
</s:form>
CreateUserAction.java
public String execute() {
setMessage("Hello " + getUserName());
return "SUCCESS";
}
Struts.xml
<package name="default" extends="struts-default">
<action name="CreateUserAction" class="com.ecommerce.action.CreateUserAction">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
Actions in Struts2 are mapped to the URLs that are built from the configuration. When the url of the request match the action name and namespace then it is executed, otherwise error code 404 is returned by the dispatcher. Struts2 is implemented as a filter and it's looking for the requests that are mapped in it's configuration. For example if I want it to filter all URLs I will write in web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
If you say the error 404 on index page itself, then you should ask yourself which URL corresponds to it and what action is mapped via configuration to that URL. If you need more detailed picture of what configuration is and what URLs you could enter to execute an action install the config-browser plugin.
for the above issue:"The requested resource is not available in struts2 when action class is configured in struts.xml"
I have a suggestion.
Copy and paste all the external jars in in the folder lib(WebContent-->WEB-INF-->lib)
remove all external jars from Build path (Project -->buildpath-->Configure Build path)
I found this solution is working fine for eclipse Luna with Tomacat 7

what is the differance path type in java struts.xml?

I am new java struts framework. But I want to ask a question.
In struts.xml path is .do like "/AddReq.do" OR path is only name like "AddReq"
What is difference between "/AddReq.do" and "AddReq" ?
For example
<action path="/AddReqPage"
type="...actions.AddReqPageAction">
<forward name="success" path="AddReq" />
<forward name="failure" path="/bos.jsp" />
</action>
<action path="/AddReq"
type="...actions.AddReqAction"
name="AddReqForm" validate="true"
scope="request">
<forward name="success" path="/AddReqDetail.do" />
<forward name="hata" path="AddReq" />
<forward name="failure" path="/bos.jsp" />
</action>
Not much difference. Both should work - provided you map to struts ActionServlet correctly in your web.xml.
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Conventionally, struts uses *.do pattern to distingush its servlet from other servlets and JSPs
".do" is a action extension. You configure it in servlet mapping. When struts parses the url it's looking for such extension to distinguish static calls from the struts action. Then finds the mapping that correspond that URL but without ".do ". However, you have still specify ".do" in the forwards if your application configured to use that extension. Nowadays, this extension has less meaning as before. The URL rewrite technique brings to completely ignore that extension. With
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/c/*</url-pattern>
</servlet-mapping>
and reference above you could completely dismiss it.

Why welcome-file-list is not working if I remove struts2 .action extension?

I have a problem if I remove the .action extension inside my Struts2 application. I put this in my struts.xml:
<constant
name="struts.action.extension"
value="" />
The application works correctly except in the index page. I have in my web.xml this:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
When I access to http://localhost/myApp/, I get the following error:
There is no Action mapped for namespace [/] and
action name [index.jsp] associated with context path [/myApp].
- [unknown location]
However if I access to http://localhost/myApp/fooAction, I'm not getting any errors and works perfectly.
If I change the extension for a non empty extension (like "html"), I see the index page perfectly if I access to http://localhost/myApp/.
So then, is there something wrong in what I'm doing? Why am I getting this error when I remove extension? Is there any posible way of not getting it?
Edit: If I put an action in the <welcome-page> the error is the following:
There is no Action mapped for namespace [/] and action name []
associated with context path [/myApp].
I was having same issue in one of the application where i need to call an Action on page load in place of index.jsp or welcom.jsp in <welcome-page>.I did the following steps
Placed the following entry in my web.xml.
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
I created an empty file with name index in my web-app folder and finally placed the following entry in my struts.xml file
<action name="index" class="welcomeAction">
<result>/ab.jsp</result>
</action>
So in this case when i was hitting this URL www.myapp.com/myApp,its calling index action of Struts2 and i was able to do all init work for my welcome page.
I had same issue, but solved!!!!
If u use
<constant name="struts.action.extension" value=""/>
in struts.xml
then put welcome file as
<welcome-file>index.jsp</welcome-file>
in web.xml
and give the action in struts.xml as follows
<package name="default" extends="struts-default">
<action name="index.jsp">
<result>WEB-INF/login.jsp</result>
</action>
</package>

Categories

Resources