Make global results package independent struts 2 [duplicate] - struts2

This question already has an answer here:
global results across different packages defined in struts configuration file
(1 answer)
Closed 6 years ago.
When global results are configured outside a package in struts.xml dtd says I can't do that. How to have one error page as a result for unforeseen error result across packages.

What you want to do is leverage package inheritance:
<package name="myapplication-default" extends="struts-default">
<!- place your global result definitions here in this package -->
</package>
<package name="someotherpackage-default" extends="myapplication-default">
<!-- reuses configuration from myapplication-default -->
</package>

Related

How do I troubleshoot struts2 flow? [duplicate]

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.

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.

ERROR: application/pdf is not a supported mime type [duplicate]

This question already has answers here:
File is not downloading from server
(3 answers)
Closed 9 years ago.
I am using Struts2 and Liferay for developing an application.
My struts.xml is as follows:
<action name="download" class="com.stp.portal.view.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="abc.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
I basically want to download a pdf file from the server. But I get the following error:
10:05:55,782 ERROR [Jsr168Dispatcher:38] Could not execute action
java.lang.IllegalArgumentException: application/pdf is not a supported mime type
at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:159)...
Don't know how to resolve this. Would really appreciate someone's help. Really need this to be done.
Looking at the source code seems like only application/vnd.wap.xhtml+xml and text/html are allowed.
Source: http://grepcode.com/file/repo1.maven.org/maven2/com.liferay.portal/portal-impl/6.0.2/com/liferay/portlet/MimeResponseImpl.java
Put portletUrlType="resource" to the <s:url> tag which you use to create download link.
<s:url var="downloadUrl" action="download" portletUrlType="resource" />

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"/>

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