How do I troubleshoot struts2 flow? [duplicate] - struts2

Good afternoon,
I realise this question has been asked multiple times but these questions were mostly caused by the person placing the struts.xml file in the incorrect place or mis-spelling a path name or something.
I've followed the tutorial from the Struts 2 website here as best I can but keep getting the following error:
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [hello] associated with context path [/basic_struts].
I'm using Maven as my build tool of choice
My struts.xml file is located in the root of the WebContent folder and is not in the WEB-INF\classes folder.
Below is the structure of my struts.xml file:
<?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">
<!-- This minimal Struts 2 configuration file tells the framework that
if the URL ends in index.action to redirect the browser to index.jsp. -->
<action name="index">
<result>/index.jsp</result>
</action>
<action name="hello"
class="com.me.actions.HelloWorldAction" method="execute">
<result name="success">jsp/HelloWorld.jsp</result>
</action>
</package>
I have also placed the HelloWorld.jsp file in a folder called jsp and I've pointed this out in the struts file. The index file code which is suppose to call the above action looks like this:
<p>
Hello World
</p>
Questions
1) Is my placing of the struts.xml file correct? (note - my web.xml file is inside the WEB-INF folder) and if not where should it be placed?
(note - i read it should be placed in the src/main/resources folder but where is that? I would have thought that was part of the java resources but why place it there? )
2) Do you need to include any jars in the lib folder or build path for this to work? From the website all you have to do is include a dependency in the Maven pom file - in mine this looks like this:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
</dependency>
Thanks
Update
Thanks to Roman C for his help. My struts file was in the wrong place. If anyone else has this problem I would recommend checking out this question that has a nice screenshot of where to position the struts file - please remember to call it struts.xml (lowercase) instead of uppercase.

Looks like a brief explanation of error: the struts.xml file should be on web application classpath. src/main/resources correspond to the Maven project structure and should be added as a source folder to your web project. When built it's merged with other source folders that are compiled and placed to the folder of the compiler output along with compiled classes. When deployed the compiler output folder copied to WEB-INF/classes. See the Maven site for getting started with web projects.

Related

Hide directory from URL

I am new in Struts. When I create my first Struts project and run it then URL of the project is the following:
10.1.21.85:8080/shravan/aboutus/about.jsp
But here in URL all paths of JSP pages display i.e my about.jsp page is inside shravan/aboutus folder so how I hide this directory structure in the URL?
First you should keep your JSPs private by putting them inside WEB-INF directory.
Then you have to map an action in struts.xml to show the desired jsp.
<struts>
<package name="default-package" extends="struts-default">
<action name="about-us">
<result>/WEB-INF/jsp/aboutus/about.jsp</result>
</action>
</package>
</struts>
And now your url should look something like this:
http://10.1.21.85:8080/shravan/about-us.action
You have to check in web.xml how is your struts configured to map the urls.
It could be:
/*.action, /*.do or simply /*

Underscores and wildcard action mapping in Struts 2

I'm working with a Struts 2 webapp with the following action mapping:
<action name="something_*" class="foo.whatever.MyAction" method="{1}">
<result>blah/myJsp.jsp</result>
...
</action>
So if I load the URL /something_load.action, it calls to MyAction.load(), and so on. Piece of cake. What puzzles me is that loading /something.action does work too (I guess it's invoking the execute() method). How is it possible? My action mapping is supposed to match "something_", but there is no underescore in my URL. It should give me an error! Shouldn't it?
I've double checked that there isn't another mapping for "something.action" in the struts config files. I also checked the web.xml file, just in case...
The only explanation that I can imagine is that the underscore is ignored in Struts if I use wildcard mappings. But then it would make no difference to load /something_load.action, /some_thing_lo_ad.action... and that's not true.
I'm aware that this must be a very noobish question, but I've been unable to solve the mistery, neither looking through Stackoverflow questions nor the Struts documentation.
This is the main struts.xml file:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.freemarker.templatesCache" value="true" />
<package name="default" extends="struts-default">
<!-- interceptors ... -->
<!-- global results for error pages -->
</package>
<!-- lots of includes -->
</struts>
It appears that wildcards are matched loosely in order to support some legacy syntax. So the issue isn't with underscore but with loose matching pattern.
From the javadocs:
Patterns can optionally be matched "loosely". When the end of the pattern matches \*[^*]\*$ (wildcard, no wildcard, wildcard), if the pattern fails, it is also matched as if the last two characters didn't exist. The goal is to support the legacy "*!*" syntax, where the "!*" is optional.

no effect of "resultpath" constant in struts.xml

I have all the jsp pages (ex: index.jsp) in the WebContent folder and my application is working smoothly.
Now, I created a folder "pages" inside WebContent, where I transferred all my jsp pages.
And I added the following line inside the <struts> tags:
<constant name="struts.convention.result.path" value="/pages" />
But still, its not searching for jsp inside the "pages" folder.
I am getting:
HTTP Status 404 - /MyApplicationName/index.jsp
Where am I doing a mistake?
EDIT: The current structure of my application is as follows:
The welcome page is an action instead of a jsp page. The result page of this action is index.jsp. This action populates the dropdown menu and some other fields in index.jsp. Now, I want to keep index.jsp along with other jsp pages in the folder "WebContent/pages". I have over 30 jsp pages in my application, and I don't want to mention the full path of every jsp in the result-tag. Thats why I want to make use of the struts2 constant "struts.convention.result.path"
My question is, why the code I have mentioned above is not working?
Accessing index.jsp directly bypasses all S2 request handling.
There's no S2 or S2 Convention plugin involved, you're just accessing a JSP page.
If index.jsp is first page of your application then you need to change its path to /pages/index.jsp in <welcome-file> tag of web.xml. And for rest I agree with #Dave Newton :)
Probably your mistake was in struts.xml.
When you create a new-Folder under web-content make with the same name (folder-name) of
xml file under src and include it in struts.xml file it will work fine.
For eg: suppose you created example folder under web-content, now create example.xml file
under src folder and in struts.xml file include example.xml
example.xml:
<struts>
<package name="example" namespace="/example" extends="default">
<action name="your action name" class="your class" method="your method">
</action>
</package>
</struts>
struts.xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<action name="" class="" method="">
</action>
</package>
<include file="example.xml"/>

Geting 404 error when request is made from JSP

I am trying to learn Struts2, but I am unable to create a working hello world application. It's giving a 404 (page not found) error when request is made from JSP. I kept all the lib files in the lib folder, placed struts.xml inside classes folder, but I am still getting this error.
Here is the view:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<form action="HelloWorld">
<input type="submit">
</form>
</body>
</html>
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">
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is my struts.xml file
<!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="HelloWorld" class="vaannila.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
This is the error:
HTTP Status 404 - /Testi/HelloWorld
type Status report
message /Testi/HelloWorld
description The requested resource (/Testi/HelloWorld) is not available.
Apache Tomcat/6.0.33
This is the same question that you posted in Struts2 : Using form action just a bit more verbose.
You're problem is in the form tag you are using. It's not a struts form tag and the action attribute in a struts form tag is handled differently in a action attribute in an HTML tag.
I think my answer in Struts2 : Using form action should work for you.
PS: if you haven't changed your action extension, you may be able to make this work by simply adding a .action onto the end of your standard HTML tag action:
<form action="HelloWorld.action">
...
</form>
I would suggest to create a new folder under tomcat.webapps folder and place all your files according to the standards and give a try if you are using any IDE.
Try debugging from scratch,
first see if you are able to hit localhost:8080 sucessfully,
then try hitting a servlet(change the web.xml accordingly) to see if
you are able to go through.
Finally try hitting struts2 action. Make sure url-mapping you give is
correct.
I think this should be a mapping issue.
Can you post the error log content? and about the structure of the project.404indicatng the your server to able to locate the requested resources this can be a issue of your mapping or may be t unable to find the mapping file in class path.
you need to place struts.xml file in the class path so if you are using Eclipse just drop your struts.xml file inside src folder rest eclipse know how to handle this
Also the best way to learn struts2 is to download the sample applications from there official site
Example Applications:
this will help you to understand and learn struts2 better because
Sample application will help you to understand what structure one need to have
What all configuration files need to be placed and at what location
What are all required dependencies for a simple hello world application
Add imported lib to Action class
ex:
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
//....
//....
}

Struts 2.2.1 gives There is no Action mapped for namespace / and action name

I've got a simple Struts 2 application that I build and deploy to glassfish with Netbeans. When I point the web browser to http://localhost:8080/Struts2Hello/login.action/ it gives this error:
There is no Action mapped for namespace / and action name .
My action is named "login". Why does it think it is named "."?
(In order to get this far I've also had to add the javassist-3.7.ga to my lib/ directory. I'm not sure why, but that's what others have done to make Struts 2.2 work.)
Here's my web.xml and struts.xml files (yes, the struts.xml gets deployed to WEB-INF/classes):
<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>Login.jsp</welcome-file>
</welcome-file-list>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="action.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
</package>
Thanks.
Dean
My problem was that I had the .jsp files in the WEB-INF/ dir instead of one level higher. Strange error message for that problem, though.
In struts.xml(or any other struts config files included), content of mapping is back to the original Servlet/Jsp, i.e. you CAN NOT put .jsp files under WEB-INF. e.g. if the result in struts config file is: Welcome.jsp then you should put Welcome.jsp under the package root, like
Web-pages
...WEB-INF
...Welcome.jsp
...menu
......menu.xhtml
More often that not when struts gets the action name wrong even when there's nothing wrong with struts.xml, the problem is that the tag lib definition is missing from the jsp file.
crazy to answer this one after a year, but I had the same problem and 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....!

Resources