I am working on Struts2 Interceptors .
I have read that Struts2 Interceptors are just like Filters , which execute before the Action class is executed and one more time after processing the result ( Please correct me if i am wrong ) , that is two times
But when i ran the below code , the interceptors are executed only once .
Please correct me if i made any mistake .
Please see my code below :
This is My Struts.xml file
<struts>
<constant name="struts.devMode" value="true" />
<package name="test" extends="struts-default">
<interceptors>
<interceptor name="loginkiran" class="vaannila.MyLoginInterCeptor" />
</interceptors>
<action name="HelloWorld" class="vaannila.HelloWorld" method="kiran">
<interceptor-ref name="loginkiran" />
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
This is my Action class
public class HelloWorld
{
public HelloWorld() {
}
public String kiran() {
System.out.println("iNSIDE THE aCTION CLASS");
return "SUCCESS";
}
}
This is my Interceptor class
public class MyLoginInterCeptor implements Interceptor {
#Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("Destroying Interceptor");
}
#Override
public void init() {
}
#Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
System.out.println("iNSIDE THE iNTERCEPTOR");
return invocation.invoke();
}
}
This is my JSP File :
<html>
<body>
<%
System.out.println("iNSIde THE jsp");
%>
</body>
</html>
The Output for the above code is this :
iNSIDE THE iNTERCEPTOR
iNSIDE THE aCTION CLASS
iNSIde THE jsp
Interceptors are not executed twice (nor are filters): interceptors (and filters) wrap the action (or servlet/etc.)
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Before action invocation...");
return invocation.invoke();
System.out.println("After action invocation...");
}
Related
I would need to trigger some action on schedule.
Let's consider this struts.xml :
<action name="myAction" class="app.MyAction">
<result name="success">
myJsp.jsp
</result>
</action>
this java class :
public class MyAction extends ActionSupport{
private String name;
public getName(){ return name; }
public setName(String name){ this.name = name }
#Override
public String execute() throws Exception {
Return SUCCESS;
}
}
And myJsp :
<s:form action="myAction" method="get">
<s:hidden name="name" value="blabla" />
<s:submit key="submit"/>
</s:form>
Now it would be interesting for me to trigger "myAction" every 30 seconds.
Once the jsp loaded, I'd wait 30 seconds and then the page would refresh.
PS: I don't need some ajax technologies
And the winner is... Javascript:
setTimeout(function() {
location.reload();
}, 30000);
In my application using struts 2 framework and tiles.
Now, I want to use displaytag to display the data in a database table. I did the following:
in my struts.xml:
<action name="users_admin" class="com.controller.admin.UserAction">
<interceptor-ref name="checkSession" />
<result name="login">/login.jsp</result>
<result type="tiles" name="none">/users_admin.tiles</result>
</action>
In file UserAction:
public class UserAction extends ActionSupport{
private List<Users> listUsers;
private String myActionName;
public String getMyActionName() {
return myActionName;
}
public void setMyActionName(String myActionName) {
this.myActionName = myActionName;
}
public List<Users> getListUsers() {
return listUsers;
}
public void setListUsers(List<Users> listUsers) {
this.listUsers = listUsers;
}
#Override
public String execute() throws Exception {
listUsers = UserServices.selectAll();
return NONE;
}
}
And In file user_admin.jsp(file use in tiles):
<s:actionerror cssStyle="color:red"/>
<div class="well">
<display:table name="listUsers" id="listUsers" requestURI="/UserAction" cellpadding="5px;"
cellspacing="5px;" style="margin-left:50px;margin-top:20px;">
<display:column property="EMAIL" title="Email"/>
<display:column property="NAME" title="Name"/>
</display:table>
</div>
File user model:
package com.model;
public class Users {
private String EMAIL;
private String NAME;
public String getEMAIL() {
return EMAIL;
}
public void setEMAIL(String EMAIL) {
this.EMAIL = EMAIL == null ? null : EMAIL.trim();
}
public String getNAME() {
return NAME;
}
public void setNAME(String NAME) {
this.NAME = NAME == null ? null : NAME.trim();
}
}
When I run the application error occurs in apache tomcat log
org.apache.catalina.core.ApplicationDispatcher.invoke Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: org.apache.commons.collections.IteratorUtils
Who can help me point out why the error, in the solution to overcome it?
Add commons-collections-3.1.jar to your libraries.
Note that you are using only one Interceptor, unless checkSession is the name of your stack.
EDIT
Interceptor checkSession to check my log on the website. How should I use.
You can define a custom interceptor stack, and then reference it, or include your custom interceptor AND another stack (for example, the default one) for a single action, like this:
<action name="users_admin" class="com.controller.admin.UserAction">
<interceptor-ref name="checkSession" />
<interceptor-ref name="defaultStack" />
<result name="login">/login.jsp</result>
<result type="tiles" name="none">/users_admin.tiles</result>
</action>
I am developing a simple HelloWorld struts application. I am doing the configuration using struts.xml file. the welcome page is displayed but when I click on the form submit button, it gives 404 error.
Kindly provide some pointers.
action class:
package client;
public class Hello {
public String name;
public String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
message = "Hello " + name;
return "success";
}
}
namecollect.jsp
<s:form action="hello" method="post">
<s:textfield name="name">Enter name</s:textfield>
<s:submit></s:submit>
</s:form>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="client.Hello">
<result name="success">hello.jsp</result>
</action>
</package>
This is the package structure:
Your config file is in the wrong place.
By default struts.xml is expected to be at the root of your classpath.
I have written an interceptor to prevent caching but the pages still cache.
Interceptor:
public class ClearCacheInterceptor implements Interceptor {
public String intercept(ActionInvocation invocation)throws Exception{
String result = invocation.invoke();
ActionContext context = (ActionContext) invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
HttpServletResponse response=(HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE);
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
return result;
}
public void destroy() {}
public void init() {}
}
Struts.xml
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="caching" class="com.struts.device.interceptor.ClearCacheInterceptor"/>
<interceptor-stack name="cachingStack">
<interceptor-ref name="caching" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="Login" class="struts.device.example.LogIn">
<interceptor-ref name="cachingStack"/>
<result>example/Add.jsp</result>
<result name="error">example/Login.jsp</result>
</action>
</package>
</struts>
Application works fine; it executes interceptor but it doesn't prevent caching.
I have solved my problem. Thanks to developer tools for helping me to trace out.
A slight sequence change in my code helped me out: as per the Struts 2 interceptor docs the result is rendered before invocation.invoke() returns. Setting the headers before the result is rendered back to the client sets the headers in the returned result.
i.e.,
public String intercept(ActionInvocation invocation)throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
return invocation.invoke();
}
I'm a beginner in Struts2. I am used in PHP, while logging to save authentification in a session variable, which I can destroy after logging out. I wonder how I can do the same process in Struts2 : to set a session variable while logging in and to destroy it while logging out. Thank you a lot.
Update ( An additional solution )
In addition to the useful answers and comments, we can use :
session.remove("session_var_name"); // instead of session.clear();
to remove one exact session variable instead of removing all the session variables. Thank you all.
You can do one of the following
public class MyAction extends ActionSupport implements ServletRequestAware
{
private HttpServletRequest httpServletRequest;
public void setServletRequest(HttpServletRequest request)
{
this.httpServletRequest = request;
}
public String login()
{
httpServletRequest.getSession(false).setAttribute("key", your_session_object);
return SUCCESS;
}
public String logout()
{
httpServletRequest.getSession(false).removeAttribute("key");
return SUCCESS;
}
}
public class MyAction extends ActionSupport implements SessionAware
{
private Map sessionMap;
public void setSession(Map map)
{
this.sessionMap = map;
}
public String login()
{
sessionMap.put(key, your_session_object);
return SUCCESS;
}
public String logout()
{
sessionMap.remove(key);
return SUCCESS;
}
}
The second alternative i.e. implementing SessionAware is preferred since it shields you from Servlet APIs.
You can use Scope Interceptor when you call logout, and with "end" type in your struts xml configuration, Interceptor set null to your session object:
<action name="scopea" class="com.mevipro.test.action.ScopeActionA">
<result name="success" type="dispatcher">/jsp/test.jsp</result>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="scope">
<param name="key">funky</param>
<param name="session">person</param>
<param name="type">start</param>
</interceptor-ref>
</action>
<action name="scopeb" class="com.mevipro.test.action.ScopeActionB">
<result name="success" type="dispatcher">/jsp/test.jsp</result>
<interceptor-ref name="scope">
<param name="key">funky</param>
<param name="session">person</param>
<param name="type">end</param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
</action>
you must define "start" and "end" the start is when you initialise your object in session and the "end" for destroy your object
For more detail : https://struts.apache.org/docs/scope-interceptor.html