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

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.

Related

Grails- Add link href to button

I'm building a simple Grails app for a web development class. For the most part, the app is finished, but I'm having one sticking issue.
On the index page, I have a series of buttons that correspond to the List, Create, and other templates that are built in Grails via scaffolding. How can I dynamically pass on the correct path to the controller action?
In order to do this, I need to get the current page URL and add the proper location. Is that possible to do in Grails or should I stick with jquery or some other ajax solution?
What are you trying to achieve here,
If you want to generate link to controller actions that you can use for button href, you can do it like this,
<button href="${createLink(controller:'foo', action:'bar')}"/>
See the createLink tag
If you want to know controller and action name, ${controllerName} and ${actionName} can be used.
Can you use ${controllerName} to get the name of the current controller?
An alternative might be ${params.controller}, but again, not 100% sure it works in gsps

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.

JSF 2.0 javax.faces.webapp.FacesServlet mappig

I started to use JSF 2.0 recently and I don't understand completely how I need to configure the javax.faces.webapp.FacesServlet to correctly handle resources.
For example, If I decided to create a web application with .xhtml files and .jsp files and I want both them to use jsf technology how am I supposed to configure the jsf servlet to handle both?
Sometimes I found example where the servlet url pattern is just /faces/*
thanks!
I suggest to use a suffix pattern as URL pattern like *.jsf. If a Facelets file (.xhtml) is present on the given view ID, then it will be served. Otherwise if a JSP file (.jsp) is present on the given view ID, then it will be served. This also gives you the room to gradually upgrade from JSP to Facelets without the need to change URLs, so that you can ultimately get rid of those legacy JSPs in an easy way.

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.

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