I am a complete newbie to Struts2 and am unable to run anything involving it on tomcat in either eclipse/browser. Tomcat is working fine when I am just mapping servlets/jsp in a web.xml file, but when trying to switch to struts2 I can't get it to work. When I try to run on the server it isn't even going to my index jsp...just loading a 404 error. I followed an online tutorial when I couldn't get my app for my class running and the tutorial I can't get to run either. Here are the jar files I have added to tomcat lib/build path jars -
asm-5.0.2.jar
asm-commons-5.0.2.jar
asm-tree-5.0.2.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts-core-1.3.10.jar
struts2-dojo-plugin-2.3.20.jar
xwork-core-2.3.20.jar
filter added to 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>
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="helloworld" extends="struts-default">
<action name="hello"
class="com.struts2.files.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
Any ideas as to why I am getting a 404 when trying to run the app?
I guess, you've added the wrong struts-core-1.3.10.jar to your classpath. That probably belongs to Struts 1.x. You need to include the latest struts2-core-2.x.x.jar from the Struts 2 framework.
A typical Struts 2 application has the following jars under WEB-INF/lib directory.
commons-fileupload-1.2.1
commons-io-1.3.2
commons-logging-1.1
freemarker-2.3.13
junit-3.8.1
ognl-2.6.11
xwork-2.1.2
struts2-core-2.1.6
struts2-convention-plugin-2.1.6 (if using annotations)
Try downloading the latests jars from struts.apache.org.
The filter class org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter belongs to Struts2 but you are using struts1 jar file struts-core-1.3.10.jar
As Ravi suggested, download the Jar file from Struts-2.3.20
Related
I am new to Struts 2 and I have been following a video tutorial on Struts 2(Koushik). I have created the Struts.xml, the action class and JSPs as same as created in the tutorial. But it gives the following exception.
Exception:
Jan 13, 2014 9:30:48 PM org.apache.struts2.dispatcher.Dispatcher warn
WARNING: Could not find action or result: /Struts2Starter/getTutorial.action
There is no Action mapped for namespace [/] and action name [getTutorial] associated with context path [/Struts2Starter]. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
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.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Starter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
TutorialAction.java (Action class that I'm using)
package org.koushik.javabrains.action;
public class TutorialAction {
public String execute(){
System.out.println("Hello from execute");
return "success";
}
}
Project structure
success.jsp and error.jsp are normal jsp files with some text. I did a lot of things to resolve this issue by googling. But nothing didn't solve this. Please let me know if anyone knows what's behind this. I highly appreciate it. :)
Renaming Struts.xml naming convention to struts.xml will work.
The message from error clearly says that
no Action mapped for namespace [/] and action name [getTutorial]
associated with context path [/Struts2Starter]
This means that action configuration is not available at runtime. Check out the config-browser plugin to debug configuration issues.
To map correctly url to the action two parameters are required: the action name and namespace.
Struts loads xml configuration on startup and it should know the location of the struts.xml. By default it's looking on classpath to find a file with known name like struts.xml. Then it parses document and creates a runtime configuration. This configuration is used to find appropriate configuration element for the action url. If no such element is found during request, the 404 error code is returned with the message: There is no Action mapped for namespace and action name.
Also this message contains a namespace and action names used to find the action config. Then you can check your configuration settings in struts.xml. Sometimes the action name and namespace, stored in ActionMapping point to the wrong action. These values are determined by the ActionMapper which could have different implementation, used by plugins.
There's also another setting that might affect this mapper and mapping, but the point is the same if you get this message then URL is used didn't map any action config in runtime configuration. If you can't realize which URL you should use, you might try config-browser plugin to see the list of URLs available to use.
change your Struts.xml and put <constant name="struts.devMode" value="true" /> in struts.xml Development mode or devMode it enables extra debugging and helps indebugging
try below code
<?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" namespace="/" extends="struts-default">
<action name="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
U just need to check that action name is constant everywhere...i also had same problem i resolved by removing namespace as it is not necessary which i see u have not mentioned and also i had different action name at loginpage.jsp and struts.xml page.. so just see ur action name
Your JSP file should not be under the WEB-INF folder, but should be under the web folder. I've attached an image to show you the right hierarchy.
Folder System
When I try to run my Spring + Struts 2 project on WL Server 9.2 (I have to user this version, so please don't ask me to user the newer version of WL Server), I had this error.
The URL address in the browser is :
http://localhost:7003/SpringStrust2/
Full stacktrace:
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
at jsp_servlet.__user._jsp__tag0(__user.java:115)
at jsp_servlet.__user._jspService(User.jsp:12)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3269)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2019)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1925)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1394)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
Here is my web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>User.jsp</welcome-file>
</welcome-file-list>
struts.xml:
<struts>
<constant name="struts.enble.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="myapp" />
<package name="default" extends="struts-default">
<action name="user" class="user">
<result name="success">/success.jsp</result>
<result name="input">/User.jsp</result>
</action>
</package>
</struts>
user.jsp:
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring + Struts2</title>
</head>
<body>
<s:form action="user" method="addUser">
<s:textfield name="username" key="user.name" />
<s:submit key="submit" />
</s:form>
</body>
</html>
I'm very sure about putting files in correct folders, including jar libraries in lib folder...
Here is the list of jars I'm using:
antlr-runtime-3.0.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang3-3.1.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
junit-3.8.1.jar
ornl-2.6.11.jar
org.springframework.asm-3.9.9.M3.jar
org.springframework.beans-3.0.0.M3.jar
org.springframework.context-3.0.0.M3.jar
org.springframework.core-3.0.0.M3.jar
org.springframework.web-3.0.0.M3.jar
org.springframework.web.servlet-3.0.0.M3.jar
struts2-convention-plugin-2.1.6.jar
struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
xwork-2.1.2.jar
You're hitting the User.jsp file (with its S2 tags) without running through an action like the error says.
You can either:
Redirect to a Struts action from the welcome file (a common technique, and trivial)
configure the container to allow actions as welcome files
Remove the S2 tags from the welcome file
I had the same problem. My Struts 2 application was running on localhost like knife on butter but on VPS it failed. Finally the solution is that Web-Inf folder in project conflicts with web-inf folder of Tomcat.
So move your project/web-inf/web.xml contents to
usr/local/easy/share/easy-tomcat7/conf (for Cent OS)
and move your jars from youtproject/web-inf/lib to
usr/local/easy/share/easy-tomcat7/lib (for Cent OS)
For other OS do the same at respective path..
Hope this will be helpful..
This question already has answers here:
HTTP Status 404 - The requested resource (/ProjectName/) is not available
(3 answers)
Closed 6 years ago.
I'm using struts framework and this problem is occurred when i deploy my project....
why this error is occurred at the deploy time???
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<welcome-file-list>
<welcome-file>userLogin.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
</web-app>
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="common" template="/FirstLayout.jsp">
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="body" value="/footer.jsp"/>
</definition>
<definition name="validUser" extends="common">
<put-attribute name="title" value="Login User"/>
<put-attribute name="contents" value="/userLogin.jsp"/>
</definition>
<definition name="invalidUser" extends="common">
<put-attribute name="tilte" value="Login Error" />
<put-attribute name="contents" value="/relogin.jsp" />
</definition>
</tiles-definitions>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="p1" extends="tiles-default">
<action name="signin" class="action.LoginAction ">
<result name="success" type="tiles">validUser</result>
<result name="failure" type="tiles">invalidUser</result>
</action>
</package>
</struts>
this error is occurring while i add struts jar file in my project... earlier it was running without any error..Is there any problem with my strut file. I am using Myeclipse 8.5 and Tomcat server 6.0.
.
The thread is old, but I will post the solution anyway, as more people are learning how to write a web application using apache struts frame work, they will undoubtedly encounter the same problem.
The problem is that Eclipse does not copy the ROOT folder from Apache Tomcat to your Eclipse work place.
Just go to your Apache-Tomcat folder, then COPY the ROOT folder in the webapps folder.
Paste it into your Eclipse:
your_workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
tmp0 is your current server.
Also, just make sure you add your struts .jar files.
That's all folks
TL
404 may occur due to either not setting welcome file(but you set it) or if the server failed to deploy your project.
The second may be the reason here.
Check the console logs did the server start at all.
Secondly as you mention about the jar it may happen that you have included two jars of struts but with different versions.
Better use a build tool like Maven or Ant.
Tell if that helps.Thanks.
I am unable to execute my struts2 application. I am using eclipse indigo IDE, tomcat 7 and jdk 1.7.
The jar files I included are:
commons-logging-1.0.4.jar,
freemarker-2.3.8.jar,
ognl- 2.6.11.jar,
struts2-core-2.0.11.jar,
xwork-2.0.4.jar
I placed the struts.xml in classes folder in WEB-INF and I also tried it placing in
src folder but I could not able to make it. I am getting the below error on console
There is no Action mapped for namespace / and action name tutorial. - [unknown location]
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./tutorial.action">
Username: <input type="text" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
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="tutorial" class="com.test.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-
app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Starter</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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
TutorialAction.java
package com.test;
public class TutorialAction {
public String execute() {
System.out.println("Hello from execute");
return "success";
}
}
As others have pointed out, you do not have a getTutorial action in your mapping, only a "tutorial". However, I'm going to skip over that and suggest that you learn how to sanity check a Struts2 app. Anytime you are setting up a new technology, and even when you are working with a familiar technology, it is very useful to understand how to do a basic sanity check -- in this case, you need to verify that your struts xml has been successfully parsed and the framework knows about your actions.
I HIGHLY RECOMMEND you add the Struts 2 Config Browser plugin to your struts 2 apps. To add the plugin, you just get the jar ( all struts 2 plugins are jars ) and put in your webapp's lib directory. That's it. When you start your applicaiton, hit this URL:
http://localhost:8080/starter/config-browser/index.action
And it shows you all of the actions, as well as other configurations, that the framework knows about. Essentialy struts 2 diagnostic tool, and too easy to use to not use it.
Try to add namespace attribute into you package element of struts.xml file.
Like this:
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="tutorial" class="com.test.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
I had the same problem and I saw multiple posts on this one....here is the possible solution
I was pretty sure that I had mapped all my actions accurately, but it was showing the same error above....so I just cleaned the project and then ran it again..it worked perfectly fine...give it a try !
I encountered this so many times...so to avoid such kind of things, I just added "../eclipse.exe -clean" to the shortcut icon property....this works and u can forget about getting such kind of errors which is actually not an error....!
The error comes because the server is not able to find correct path for struts.xml .
Its better to put the struts.xml in parallel to src folder or in WEB-INF/classes.
Try this once don't append .action
<form action="tutorial">
place your struts.xml file in src outside the package or in WEB-INF classes folder.
I'm trying to configure sitemesh to only take effect for a certain subset of action mappings in my Struts 2 application.
Say for example, I have the following struts.xml snippet:
<package name="default" namespace="/" extends="struts-default">
<action name="showForm">
<result>/view/form.jsp</result>
</action>
</package>
<package name="widgets" namespace="/widgets" extends="struts-default">
<action name="showForm">
<result>/view/form.jsp</result>
</action>
</package>
I would like the output of "/showForm.action" to be decorated by SiteMesh but for "/widgets/showForm.action" to be returned empty instead. The critical part here is that I want the JSP file to be reused by both action mappings.
But try as I might, I can't seem to get SiteMesh's tag to recognize a mapping. I have to specify the file "/view/form.jsp" to be excluded instead and that means I won't be able to reuse the JSP file.
Is there any way I can get around this?
I'm using Struts 2.0.14.
Thanks,
Wong
Same answer as for Using SiteMesh with RequestDispatcher's forward(). You can change the way the sitemesh filter is mapped to incoming requests.
I think you'd want:
<filter-mapping>
<filter-name>sitemesh</filter-name>
<servlet-name>MyServlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>