How to invoke Action class creation in Struts2 - java

I have login and calculation JSP pages. Calculation page contains list, which needs initialized action object. Here is login.xml
<struts>
<package name="default" extends="struts-default">
<action name="*Action" class="com.task.action.{1}Action">
<result name="success">/calculation.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
calculation.jsp:
<s:form action="CalculationAction">
<s:select label="Types"
list="calculationTypes"
name="calculationType" value="getDefaultCalculationType"/>
<s:submit />
</s:form>
After executing LoginAction class, i got "success" and move to calculation.jsp. Problem is CalculationAction is not created, and list in jsp can't initialize. If i call http://localhost:8082/Task/CalculationAction.action , jsp page is creating correctly.
I also tried to make references in login page and it works too.
<s:url id="calculationUrl" action="CalculationAction"> </s:url>
<div class="tab-wrapper">
Login Page
<s:a href="%{calculationUrl}">Calculation Page</s:a>
</div>
How to make the program working correctly after executing LoginAction?

Add one more result for LogingAction to redirect to CalculationAction, and return this result instead of "success" in the LogingAction.
<action name="*Action" class="com.task.action.{1}Action">
<result name="success">/calculation.jsp</result>
<result name="input">/login.jsp</result>
<result name="successfulLogin" type="redirectAction">CalculationAction</result>
</action>
The CalculationAction on "success" should return a calculation.jsp and calculation action will be in the value stack, so you can get calculationTypes initialized when the action was executed.

Related

Struts2 - returning to a specific section and/or part of a .jsp page

I am using the Struts2 framework and trying to get it to return to a specific section id on a page. Currently, it throws an error when I try to do the following:
<struts>
<constant name="struts.devMode" value="false"/>
<package name="default" namespace="/" extends="struts-default">
<action name="default">
<!--suppress Struts2ModelInspection -->
<result type="redirectAction">index.jsp</result>
</action>
<action name="sendEmail" class="com.brickhouse.action.EmailAction">
<result name="success">index.jsp#contact</result>
</action>
<action name="sendNewsletter" class="com.brickhouse.action.NewsletterAction">
<result name="success">index.jsp</result>
</action>
</package>
Of course, if I remove the hashtag and just let the "sendMail" action return to regular index.jsp as opposed to index.jsp#contact it works fine.
The section id that I am trying to return is contact.
<section id="contact">
<div class="container">
. . . .
</div>
</section>
Any thoughts?
You should not use a # sign as the dispatcher result, because dispatcher doesn't change URL the request is forwarded to. The resource with such name couldn't be found. Instead you can use it to build the URL or retuning a redirect result type to skip to the section (the last is never used). For example
<s:url var="skipToContact" value="#contact"/>
<s:a href="%{#skipToContact}">Skip to Contact</s:a>
...
<section id="contact"></section>
will skip to the section when you click on it. Make sure the section is not at the end of the document, i.e. the height from the section to the end of document is greater than a browser window height.

How to return a HTML page from struts.xml itself without going to an action?

If a request is made to a HTML page, how to return it directly from struts.xml without going to an action?
Create an action that has a result in the struts.xml but doesn't have a class, i.e.
<package name="default" extends="struts-default">
<action name="index">
<result name="myPage">/path/to/page.html</result>
</action>
You could also create a global result, that could be found from any action, i.e
<global-results>
<result name="myPage">/path/to/page.html</result>
</global-results>
<action name="index"/>
You could create an interceptor that returns a result while intercepting and action. These are essential elements of the configuration that invoke a dispatcher to forward to requested page.

Error in Form Action attribute struts2

My login.jsp is in the web folder.
And action for this is specified as :
struts.xml
<package name="admin" extends="struts-default" namespace="/secure">
<action class="actions.LoginAction" name="authenticateUser">
<result name="success" type="redirect">index</result>
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
login.jsp
<s:form action="secure/authenticateUser" method="post">
</s:form>
========================
At first request it works.
but if validate() method of action returns errors then it creates url as:
"secure/secure/authenticateUser" for the Form action attribute.
I also tried <s:url> tag but still same problem can anybody help me.
or may provide alternate solution for this.
Your action attribute on the form tag possibly has a wrong name. Use
<s:form namespace="/secure" action="authenticateUser" method="post">

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>

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