After logout back/reload issue in Struts 2 - java

I have a login page (Index.jsp) , here user put user id and password. On submit LoginAuthentification.java(action class) called and authenticate the user, but according to the result in the action class it returns the JSP.
<action name="login" class="com.struts2.LoginAuthentication"
method="execute">
<interceptor-ref name="clear-cache" />
<result name="manager">/ManagerView.jsp</result>
<result name="SSE" type="redirectAction">
<param name="actionName">viewPlan</param>
<param name="userID">${userID}</param>
</result>
<result name="input">/Index.jsp</result>
<result name="error">/error.jsp</result>
</action>
In this case, it is returning ManagerView.jsp. Now in this JSP, I added a hyperlink for logout, and it is doing below
<action name="logout" class="com.struts2.LoginAuthentication"
method="logout">
<interceptor-ref name="clear-cache" />
<result name="success">/Index.jsp</result>
<result name="error">/error.jsp</result>
</action>
Code from Action class:
public String logout() {
//if (session instanceof org.apache.struts2.dispatcher.SessionMap) {
session.clear();
//session.re
System.out.println("test");
session.remove("loggedInUser");
((org.apache.struts2.dispatcher.SessionMap) session).invalidate();
//}
return "success";
}
after logout it is redirected to Index.jsp, now I clicked on back button
It display "confirm form resubmission" message in chrome and webpage expired in IE. But when I reload the page it login the old user automatically.
I have added
<%
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Expires", "0");
response.setHeader("Vary", "*");
%>
in the JSP as well as in interceptor.
I am trying to block auto login on reload.

The problem is that after logout you are not actually redirect to a new action. The cause of such behavior when you pressed the back button you got a conformation dialog in the browser. The back button is not used to call an action, unless it's not invoked via triggering it using Ajax. You should follow post-redirect-get pattern when doing request for logout.
<action name="logout" class="com.struts2.LoginAuthentication" method="logout">
<interceptor-ref name="clear-cache" />
<result name="success" type="redirect">/</result>
<result name="error">/error.jsp</result>
</action>

I resolved the issue by redirecting it to another action. and wrote another interceptor to validate if it is having user id in the session or not.
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
<action name="login" class="com.struts2.LoginAuthentication"
method="execute">
<interceptor-ref name="defaultStack"/>
<result name="manager" type="redirectAction">
<param name="actionName">home</param>
</result>
</action>
<action name="home" class="com.struts2.LoginAuthentication"
method="home">
<result name="success">/ManagerView.jsp</result>
<result name="error">/error.jsp</result>
</action>

Related

Restricting the file type in struts2 using "allowedTypes" parameter

I have a form where the user is only allowed to upload pdf files. I use the following struts action to limit the size and file type, but only the size limit works. Any ideas on what I am missing? Thanks!
<action name="commentAction" class = "gov.mo.dnr.rat.controller.comment.CommentAction">
<interceptor-ref name="validUserStack">
<param name="fileUpload.maximumSize">5242880</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
<result name="success" type="tiles">comment</result>
<result name="input" type="tiles">comment</result>
</action>
I found the problem. The allowedTypes should be fileUpload.allowedTypes, in this case.

How to validate specific action using XML configuration in Struts 2

I want to validate specific method only in action class.
Action method is this.
public String add() throws Exception {
// aflag = true;
org.setAoName(aoName);
orgBo.addOrg(org);
orglist = orgBo.searchOrg(organisationSearch);
setAoName("");
setAflag("viewaddorgs");
return "add";
}
Problem is currently I have many action methods in action class. When I call those methods it validate this method. Please help me to validate only this method by XML.
This is my OraganisationAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="aoName">
<field-validator type="requiredstring">
<message>
The name is required!
</message>
</field-validator>
<field-validator type="requiredfield">
<message>
The name is required!
</message>
</field-validator>
</field>
</validators>
This is my JSP :
<s:form id="post" action="addorgs" validate="true" >
<s:textfield name="aoName" label="Add Organisations" />
</s:form>
struts xml :
<package name="default" extends="struts-default">
<result-types>
<result-type name="tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="*security" class="com.ast.action.admin.SecurityAction"
method="{1}">
<result name="second" type="tiles">secondTemplate</result>
<result name="pass" type="tiles">orgTemplate</result>
</action>
<action name="*orgs" class="com.ast.action.admin.OraganisationAction"
method="{1}">
<result name="viewadd" type="tiles">orgAddTemplate</result>
<result name="input" type="tiles">orgAddTemplate</result>
<result name="add" type="tiles">orgAddTemplate</result>
<result name="addtbl" type="tiles">orgAddTemplate</result>
<result name="search" type="tiles">orgTemplate</result>
<result name="delete" type="tiles">orgTemplate</result>
<result name="viewedit" type="tiles">orgEditTemplate</result>
<result name="edit" type="tiles">orgEditTemplate</result>
<result name="edittbl" type="tiles">orgEditTemplate</result>
<result name="orgmenu" type="tiles">orgTemplate</result>
<result name="view" type="tiles">orgViewTemplate</result>
</action>
it works with annotations but with XML no success yet.
To exclude some methods from validation you can override parameters of the validation interceptor. For example in interceptor stack you can write
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse,yourmethod</param>
</interceptor-ref>
in the action configuration you can use above code and
<interceptor-ref name="defaultStack">
<param name="validation.excludeMethods">input,back,cancel,browse,yourmethod</param>
</interceptor-ref>
Note yourmethod is a method name.
You can specify a validation.xml for only one action by a postfix of action name in this file. For example AcrionClass-add-validation.xml will work only for the action name add in the action config. Note here add is an action name, not a method name.
Often, the name attribute matches the method name, but they may also differ.
I had similar problems but then these are couple of things that i rectified :
1) DOCTYPE in the yourAction-yourAlias-validation.xml should be
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
2)Created a custom interceptor to exclude the methods that need not be validated.Below is a sample for my candidate registration where i had to skip validation for district method which fetched districts for a state.
<interceptor-stack name="clientValidation">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,district</param>
</interceptor-ref>
<interceptor-ref name="workflow"/>
</interceptor-stack>
<package name="registration" namespace="/candidate" extends="struts-default">
<action name="candidateRegistration" class="com.app.action.CandidateAction">
<interceptor-ref name="clientValidation"/>
<result name="input">/jsps/student/studentregistration.jsp</result>
<result name="failure1">/jsps/student/registrationfailure.jsp</result>
<result name="cancelRegistration">/jsps/login.jsp</result>
<result name="district">/jsps/includes/dropdown_districts_regoffice.jsp</result>
</action>
</package>
Use an action alias: map different methods of an Action to different action aliases in struts.xml, and instead of the single yourAction-validation.xml file, add a yourAction-yourAlias-validation.xml for the validators you want to perform for that method only.
Read more here.
EDIT
No its not validation anything now.
<validators>
<field name="aoName">
<field-validator type="required">
<message>You cannot leave the aoName address field empty.</message>
</field-validator>
</field>
</validators>
name is OraganisationAction-add-validation.xml and put it with action class package. Is there anything to enable validation in struts.xml ?
If you have a correctly configured (for example the default) Interceptor Stack, validation is already enabled. If you are using a file named OraganisationAction-add-validation.xml, that means that:
your action CLASS must be named OraganisationAction.java;
your action mapping in struts.xml must be the following:
<action name="add" class="your.package.OraganisationAction" method="add">
<result>yourResult.jsp</result>
</action>
Look for typos, check your Interceptor Stack, and it will work automatically.

Struts2 localize interceptor

I'm trying to create an Interceptor able to change the language but I can not change it.
Relevant Interceptor code:
public String intercept(ActionInvocation invocation) throws Exception {
ActionMapping mapping = (ActionMapping) invocation
.getInvocationContext()
.get(ServletActionContext.ACTION_MAPPING);
Map<String, Object> params = mapping.getParams();
if (params != null) {
Locale locale = (Locale) params.remove(LOCALE_PARAMETER);
if (locale != null) {
ActionContext.getContext().setLocale(locale);
}
}
return invocation.invoke();
}
struts.xml:
<package name="default" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<interceptors>
<interceptor name="navigator"
class="it.apps.mca.web.interceptors.NavigatorInterceptor">
</interceptor>
<interceptor name="locale"
class="it.apps.mca.web.interceptors.internationalizations.LocaleInterceptor">
</interceptor>
<interceptor-stack name="customStack">
<interceptor-ref name="navigator" />
<interceptor-ref name="locale" />
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="locale"
class="it.apps.mca.web.actions.internationalizations.LocaleAction">
<interceptor-ref name="customStack"></interceptor-ref>
<result name="success" type="redirect">
<param name="location">${target}</param>
<param name="parse">true</param>
</result>
</action>
<action name="login"
class="it.apps.mca.web.actions.authentication.LoginAction">
<result name="success" type="tiles">/welcome.tiles</result>
<result name="input">index.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
I know i18n interceptor already exists.
I added an interceptor that performs redirections. After adding this interceptor, I lose the location of the page.
The same thing happens if I perform a redirect through the action. You know tell me the reason?
That Interceptor already exists: i18n Interceptor
It is part of the defaultStack, so you don't have to manually include it.
Example of usage
Other info in this answer.

struts2 redirecting to default action when working with tiles

i have index.jsp like this
<% response.sendRedirect("home.action"); %>
i want to invoke that action like http://localhost:8080/Myapp
but i want to remove index.jsp and i tried with
<default-action-ref name="home"/>
<action name ="home"----
but i got 404 error specifying that the action doesnot exist
What to do in this case ?
when i am putting a breakpoint in the action (class) control is not coming to action so i think that <default-action-ref name="home"/> is not working
Please provide some helpful inputs?
I don't thinks it's a good idea but it will fulfill your requirement.
<action name="">
<result type="redirectAction">home</result>
</action>
There is a Simple Logic Which I've Used
<action name="regi">
<result name="success" type="tiles">regi.tiles</result>
</action>
<action name="registration" class="com.regform">
<result name="success" type="redirect">regi.action</result>
</action>

Form with session scope

I have an actionform associated with a action mapping in session scope. After submitting the form I want to make some changes to the form when it reaches the action.
Also, the above action forwards the request to some other action based on a request parameter. But when the form reaches the second action, the changes I made on it in first action are gone.
I am using Struts 1.x
1st Action
MultiChangeForm changeForm = (MultiChangeForm)form;
myCustomChanges(changeForm);
request.getSession().setAttribute("form", changeForm);
2nd Action
MultiChangeForm changeForm = (MultiChangeForm) request.getSession().getAttribute("form");
struts-config
<action path="/multiController"
type="com.multi.action.MultiControllerAction"
name="MultiChangeForm"
scope="session"
input="form.jsp"
validate="false">
<forward name="generate" path="/Generate.do" />
</action>
<action path="/Generate" type="com.multi.action.MultiGenerateAction"
name="MultiChangeForm"
scope="session"
input="form.jsp"
validate="false">
<forward name="success" path="ResultForm.jsp" />
<forward name="failure" path="form.jsp" />
<forward name="error" path="error.jsp"/>
</action>

Categories