How to get the request header in Struts2 actions - struts2

How can I get the request header in Struts2 actions I can not use request.header("") method because in struts2 request is just a Map<String,Object> thus this method is not available.

After checking in the internet, I found that I need my action to implement ServletRequestAware, which lets an interceptor to push the HttpServletRequest in your action. later I can use the same. Thanx all for replying .. :)
public class CategoryAction implements ServletRequestAware{
// Your code goes here...
}

Related

Grails - Redirecting a Request

Can't I just call then function "second" with no redirect action if I in the same class to get the same result i would get with redirect action in the following code?
class SampleController {
def first() {
// redirect to the "second" action...
redirect action: "second"
}
def second() {
// ...
} }
Can't I just call then function "second" with no redirect action if I
in the same class to get the same result i would get with redirect
action in the following code?
Of course you can call a method from within a controller action but whether or not you get the same result depends on a number of factors. The best way to approach this is to understand the differences between a redirect, a forward and invoking a method. Understanding those will help you understand when each of them makes sense.
When you initiate a redirect the response is sent back to the client with information which causes the client to send a separate request back to your application. A full HTTP round trip is made. A forward is similar to a redirect except it all happens within the first request. Control is sent to the forwarded action without needing to go back to the client and having it send a second request. Invoking a method is different than all of that. Invoking a method is just invoking a method and there are a number of things that won't happen when you do that. For example, filters (Grails filters or regular servlet filters) will not be involved in that method call, even if the method you are calling is a controller action and there are filters configured that would normally apply to calls to that action. You really should never invoke a controller action method directly. If the method includes helper code that you might want to call, put that code in a regular non-action method (make it non-public) and then invoke it from wherever that makes sense. Usually a better idea is to move that method out of the controller altogether and put it in a helper class where it will have a number of a benefits including that it can be re-used wherever appropriate, tested separate from a controller, etc. This helper class might or might not be a Grails service, depending on how it is used and what it is doing.
I hope that helps.
You can use
forward action: "second"
Forwards a request without issuing an HTTP redirect.

Getting exact URL in Struts2

I want to get the URL accessed by the Struts2 web application. For instance, if user accesses http://www.webpage.com or https://www.webpage.com, I need the protocol; http or https. I tried implementing the action with ServletRequestAware and using request.isSecure() or request.getScheme(). However, I am getting false and 'http' even if I access 'https://..' link.
Is there any way to get the protocol ?
You can extract it from the header:
System.out.println(request.getHeader("referer"));
OUTPUT:
https://www.webpage.com
I think you forgot to try one more thing.
Implement Action with ServletRequestAware and use
request.getProtocol();
Else you can use the following code in your action class:
HttpServletRequest request=ServletActionContext.getRequest();
String protocol=request.getProtocol();
System.out.println("Protocol Used::"+protocol);

Struts2 :Can Interceptors can handle Unauthorized Access?

I am trying to understand Struts2 Interceptors. So please excuse if my questions are dumb.
I guess that interceptors are specific to an action class (that means before calling a specific action class, they get invoked).
For example:
<action name="test" class="com.jranch.Test">
<interceptor-ref name="GuiStackā€/>
<result name="success">/secure.jsp</result>
</action>
My question is: Assume a scenario where pictures in a website must be protected from unauthorized access (Means if the user directly enters an URL in the browser, they should not be allowed to see the pictures until they are logged in).
My view is that if its related to Servlet Filters, I can write a simple filter by putting url-pattern tag to /* to check all requests. Can Struts2 interceptors handle this as I guess they are specific to action class?
Please correct me if i am wrong.
Well As Steven told Interceptors are not specific to any Action, they in fact are the core of Struts2 framework
Interceptors are a set of reusable components.In all cases they are Applied to a request processing cycle which includes from Exception Handling to Role handling.
Its very trivial use case when one will write a Interceptor for a particular Action.
Use case you are talking about can be handled by Interceptor where for each request for a particular resources can be first intercepted by the Interceptor and based on out custom criteria whom to allow access we either forward the request down the calling stack or can reject the request.
public String intercept (ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext ();
Map<String, Object> session = ActionContext.getContext().getSession();
Object user = session.getAttribute (USER_HANDLE);
if (user == null) {
//Any processing
return "login"; //User is not logged in so ask him/her to login
} else {
return invocation.invoke (); //user is trusted one let her go ahead
}
}
Interceptors aren't necessarily specific to an action -- in fact, in most cases, they're applied to many actions or globally to all actions (very similar a servlet filter).
This answer talks about how to use an interceptor for authentication in a Struts2 application.

JSF 2.0: POST/Redirect/GET pattern when action method return null (stay on the same page)

I need to avoid double POST on refresh. So I'm using POST/Redirect/GET pattern (faces-redirect=true) and navigation handler (by #BalusC) like in this post. How to use that pattern in JSF 2.0 when action method return null (stay on the same page) ? Is it even possible or i need to use something other that POST/Redirect/GET ? I need to keep alive a view-scoped bean, so returning in action method the same view (PRG works) is not a solution (causes lost view scope).
view test.xhtml:
<h:commandButton action="#{bean.send}"/>
bean:
#ManagedBean
#ViewScoped
class Bean {
String send() {
// do something...
return null;
}
String send2() {
// do something...
return "test"; // view scope lost
}
}
Marioosh.
Post/Redirect/Get causes two lifecycle scenarios:
The redirect causes a Non-Faces Response (Section 2.1 of the JSF-2 spec).
Get causes a Non-faces request with a Faces Response (Section 2.1.1). A new view is created in this scenario.
some frameworks do implement this strategy by default: for example Spring WebFlow. Each Submit (POST) is followed by a redirect GET with a parameter "execution" enabling Spring to find the SAME view.
for example :
http://myhost:8080/booking-spring-faces/spring/mypage?execution=e1s2
I would love to find this feature in JSF toolkits like Omnifaces from BalusC !

How to build an action url in a Struts 2 action?

I have an Action that returns URLs which need to call another Struts2 action. In a JSP I would do <s:url>. Is there something equivalent to this that I can call inside of the action?
Since struts is creating your action class, simply use the Inject annotation and have struts tell you!
#Inject
public void setActionMapper(ActionMapper mapper) {
this.actionMapper = mapper;
}
You will probably need to construct the URL yourself inside of your action. One thing you may want to look at is org.apache.struts2.components.URL. This is the class that is used by the s:url tag to create the URL, although it may just be easier to create the URL yourself.
You can use Action Chaining... All you will do is you will call another action.
Use Struts forwards:
http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/com.ibm.etools.struts.doc/topics/cstrdoc006.html
http://www.mkyong.com/struts/struts-forwardaction-example/

Resources