Action in Struts2 Application - struts2

I had a sample HelloWorld Example of Struts 2 from web.
Its struts.xml contains:
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld">
<result>/success.jsp</result>
</action>
</package>
</struts>
On running application, I get a warning stating that:
No configuration found for the specified action: 'HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
So, on clicking of submit button in index.jsp page, I get Http 404 error(requested resource is not avaiable) error.

You are coding with the help of vaannilla example that is found on web with Struts2 keyword on Google search.
Are you using any development tool for coding? At least in Eclipse you can create an empty dynamic web project and then at least you have correct file structure, correctly formed xml templates and so on.
You have to follow the tutoarial strictly, not leaving anything out and having all files in correct locations. Also xml files on that example are missing some "title" type of rows that can be copied from the template of Eclipse's hello world example or similar.
But if you don't want to stick inside the vaannilla solution, the struts2 official Hello World tutorial has more comprehensive approach on their one. At least there are entire xml files present.

I got this problem quite often. so because my application does not refresh correctly.
So i clean my project and the problem is solved.
Go to 'Project' and select 'Clean...'.

Related

Struts 2: Navigation of pages through Link

I am using struts2 . I want that after clicking on link on 1st page it should go to 2nd jsp page.How to do that.
Thanks in Advance
First remember one thing since you are working with MCV2 based platform so its never advised to go from a jsp page to another jsp page directly which is being treated as bad practice.(you are not going through proper request cycle)
Request should go through Actions.Struts2 provide a convenient way to help you for such use-cases.
If in your struts config file you do not provide any Action class name framework will create an action class for you on the fly with return type as SUCCESS. This is all you need to do:
<action name=gotoJSP2>
<result>/page2.jsp</result>
</action>
This is all you need and you are good to go.
Though you can go from one JSP to Another but make sure you have no Struts2 tags and any such framework dependence since going form one JSP to another means not calling Struts2 Dispatcher filter and not letting framework to do required work to serve you.

How to check which ftl is used to render a page in a webapp built with Struts

I was wondering if there is an easy way to find the ftl template currently in use on a web page. How can I trace which ftl file is being used to generate that web page.
My web app is built using Struts
Thanks.
First, look at the URL of the page your viewing. It should look something like "..localhost:8080/your-app/someAction.action". The .action suffix is not required, but it is common. It also doesn't have to be .action, it could be anything (i.e. '.do').
Now find your struts.xml. That will be located on the classpath of your project. Your struts.xml will contain Action tags, like this.
<action name="someAction,action" class="com.your.action.ActionClass">
<result name="success"type="freemarker">/templates/some.ftl</result>
</action>
The end of the url of the page, is mapped to an action in here. Look at the result returned from the action, and you find the FTL that is used to generate the response.
This is the best I can do, with the information you have provided. Keep in mind this is a basic example, and most large apps have customized approaches, like spreading out the struts.xml to several different files, or using namespaces to divide actions into packages.
If you have any common code that's included on all your pages (say a header or footer), you could temporarily add the following to it:
Template: ${(stack.findValue("com.opensymphony.xwork2.ActionContext.actionInvocation").result.location)!}
Then view the page in a web browser.

How can I ensure all requests go through an interceptor stack?

When working in Struts2, it is just too easy to create a template and refer a URL to it without creating an associated Action. Struts2 merrily renders the template -- which is fine in most cases, but not in our case: in order to ensure proper selection of a locale, we need all our requests go through a minimal interceptor stack.
We've been researching these two ways, both unsuccessfully:
Defining a "default action" which
would be executed for any template
which doesn't have an associated
action.
Disabling the ability to
render templates without an action
-- this would force programmers to define actions for any template,
which is a good solution too.
Thanks.
Maybe look into the wildcard mappings.
<action name="*" class="struts2you.examplelogin.BaseActionSupport">
<result name="success">{1}.jsp</result>
</action>
If you place something like this as the first action I think all your unmapped jsp will be run through the default interceptor stack which you can define in struts.xml
Then also place your jsp files under the WEB-INF directory to prevent direct access
When working in Struts2, it is just too easy to create a template and refer a URL to it without creating an associated Action.
Since Struts2 is an MVC framework, every request to it should invoke an action class and therefore go through an interceptor stack. I assume that you mean that you have JSPs that are not under WEB-INF and so they can be invoked directly via URL. That's generally a poor practice in the MVC world, as your JSPs should only represent the view layer. Place the JSPs somewhere under WEB-INF and all requests will be forced to go through an action, which will resolve your problem.
As for the choices, I would advocate creating explicit mappings for each of your templates.

Visual Studio: How to override the default "Build Action" for certain extension types per project or solution?

I'm serving my asp.net mvc views from many assemblies and copying views to the main application on post-build event.
This works, however, I realized, that when I change something in view and just hit F5, changes are not included. What I have to do to see changes is to: save, build<- explicitly clicking, and then hit F5. However, it's pretty annoying solution.
I discovered that setting Build action to "Embedded Resource" on view solves the problem as well, however other devs may not remember that they have to do this after adding every view to the solution.
Is there a way to override the default build action for certain file extensions, such as: *.aspx, *.ascx in project or (better) in solution ?
What I've found is an ability to add this setting globally, per machine, but I do not want to do that (link: http://blog.andreloker.de/post/2010/07/02/Visual-Studio-default-build-action-for-non-default-file-types.aspx)
Any ideas ?
Consider the following project file outline:
<Project ToolsVersion="3.5" DefaultTargets="EmbedViews;Build" ...>
...
<Target Name="EmbedViews">
<ItemGroup>
<EmbeddedResource Include="Views\*\*.aspx" />
<EmbeddedResource Include="Views\*\*.ascx" />
</ItemGroup>
</Target>
</Project>
This will add all aspx and ascx files in Views\<subfolder> to your library as Embedded Resource. Notice that EmbedViews is added to DefaultTargets before Build - order is important here, I found out making that mistake myself :-)
Since editing all your project files to get this snippet in is cumbersome, you could make your own project template with this included.
Please let me know if this helped.
In case anyone wonders here - there is still no way to do that if you want it to work on current and future items.
In VS 2017 when adding new file when the catch-all rule is present (e.g. Content Include = "**.*ts") if you add a new file, it will add it's own line to
<ItemGroup> with it's own BuildAction, ignoring your predefined catch-all.

Map a struts2 action result to a controller instead of a jsp

I'm working on on a site built using struts2. The vast majority of our targets generate xml, so mapping the result to a jsp page makes sense. A couple of our targets actually generated binaries. I'm wondering if there is a convenient way to say that the result should come from a servlet/controller instead of a jsp.
Obviously this could be done by modifying the web.xml so the struts filter doesn't apply to those targets and mapping those targets as servlets, but this is non-ideal. Currently, the struts filter applies to the entire site, and we would like to keep it that way.
Ideas?
Thanks!
I am not sure I understand you scenario, but it seems to me you want the Stream Result
Here you can find an excellent tutorial with code examples about Struts2 file upload and file download that covers the Stream Result:
http://www.jeetrainers.com/struts2-course/chapter12-13-1-1#slide

Resources