Form with session scope - java

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>

Related

After logout back/reload issue in Struts 2

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>

How to redirect to another action in Struts in given time?

For example, once an action has been called I want it to redirect to another action in 5 seconds. Is it possible?
<action name="proceedupdateaccount" class="accountAction" method="updateAccount">
<result name="success" type="redirectAction">update-message</result>
<result name="input">/accountupdateform.jsp</result>
</action>
<action name="update-message">
<result>/message.jsp</result>
<!-- in 5 seconds this action should redirect to logout action -->
</action>
<action name="logout">
<result type="redirect">/j_spring_security_logout</result>
</action>
You can simply redirect them from the first action to another action using the <meta http-equiv="refresh" > code on the first action's result, like this:
In the message.jsp:
<meta http-equiv="refresh" content="5;url=logout">

how to add random query parameter to success response of action

I have declared an action into struts action mapping area. we want to add random query parameter onto success.jsp page.is it possible like /home/success.jsp?id=10000
<action path="/Signup" name="signupForm" type="signup.SignupAction"
validate="true" input="/home/signup.jsp" scope="request">
<forward name="login" path="/home/success.jsp" />
<forward name="error" path="/home/signup.jsp" />
</action>
First thing I would try is:
<forward name="login" path="/home/success.jsp?id=<%=id%>" />
or
<forward name="login" path="/home/success.jsp?id=${id}" />

Providing action parameter value in struts-config in Struts1

Just as in Struts2 we can provide a parameter value in struts.xml as below
<action name="myS2Action" class="demo.myS2Action">
<result>/myS2Page.jsp</result>
<param name="myS2Param">value</param>
</action>
In Struts1, Is there any way to set some parameter value in struts-config.xml?
I have following entries in struts-config.xml
<form-bean name="myS1Form" type="demo.MyS1Form"></form-bean>
<action path="/myS1Action" type="demo.myS1Action" name="myS1Form" scope="request" >
<forward name="success" path="/myS1Page.jsp"></forward>
</action>
In Strut1 configuration, we can use set-property tag. Following question is related to usage of set-property tag.
In Struts1, how to use set-property tag inside action tag?

Struts2 submit button method call doesn't fire

I am having problems with the below code, can anyone explain why the method may not be being fired on the jobListAction? 'Setup' is being called twice upon submission of the form. In short, I can't seem to get the struts button to call multiple methods. Any pointers / things to check?
public class JobListAction {
public String execute() {
System.out.println("setup");
}
public String deactivate() {
System.out.println("called");
}
public String callonme()
{
}
}
JSP:
<s:form id="recordsListForm" method="post" action="jobList">
<s:submit type="button" action="deactivate" value="Deactivate Selected Jobs" method="deactivate" />
<s:submit type="button" action="callonme" value="CallonMe" method="callonme" />
</s:form>
Struts.xml
<!-- Job List -->
<action name="jobList" class="JobListAction">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
<!-- Job List - Deactivate Job -->
<action name="deactivate" class="JobListAction" method="deactivate">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
<action name="callonme" class="JobListAction" method="callonme">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
I guess in struts 2 u need to tell the method name in Struts.xml file, try that out, I hope it works...
<action name="jobList" class="JobListAction" method = "deactivate">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
If you want to have a single action declaration that can call multiple methods in the same action class, look into using wilcard mappings:
View
<s:form id="recordsListForm" method="post" action="jobList">
<s:submit type="button" action="jobList_deactivate" value="Deactivate Jobs" />
<s:submit type="button" action="jobList_callonme" value="CallonMe" />
</s:form>
struts.xml
<!-- Job List -->
<action name="jobList_*" method="{1}" class="JobListAction">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
The above mapping will match any action that starts with jobList_ and then use the rest of the match as the method to call in the JobListAction class.
Works fine for me; what version? Is dynamic method invocation enabled (it is by default)?
What do you mean by "call multiple methods?" You can only call a single method per-request.
My stdout:
setup // On initial form display
called // Clicking submit
Cut-and-pasted your code (more or less) verbatim.

Categories