I have two buttons, "Search" and "Matrix Search". when I FIRST click on the "Search" button, it does what it's supposed to do. But if I click on "Search"
button after I click on "Matrix Search" button, it invokes the matrixSearch() function. Why does this happen? Does it have anything to do with scope?
Following is the sequence of my actions:
Click Button A---> Works fine(invokes function A)
Click Button B---> Works fine(invokes function B)
Click Button A---> Calls Button B onClick function (invokes function B) Why??
jsp file:
<input type="submit" title="Search" value="Search"
name="Search" id="Search"
onClick="clickSearchButton();" />
<input type="button" class="buttonIndent" value="Matrix Search"
onclick="matrixSearch()" />
function matrixSearch(){
//some Code omitted for simiplicity
form.action = '<%= request.getContextPath() %>/matrixSearch.do';
}
function clickSearchButton(){
form.action = "<%= request.getContextPath()%>/search.do";
form.setAttribute("target", "_blank");
document.form.submit();
}
Struts configuratin file:
<action path="/search" type="com.action.MyAction"
name="form" scope="session" validate="false"
parameter="search">
<forward name="success" path="tile.view"/>
</action>
<action path="/matrixSearch"
type="com.action.MyAction"
name="form" parameter="searchMatrix"
scope="request" validate="false">
<forward name="success" path="/matrix_search.jsp"/>
<forward name="failure" path="tile.view"/>
</action>
I finally found the answer. There were two problems. The first problem was the with the following statement:
form.setAttribute("target", "_blank");
For some reason, this attribute was being persisted even during my next button click.
So I added the following in my other function
form.setAttribute("target", "_self");
The second problem was there was a problem in my clickSearchFunction(). This function is long, so I didn't write the whole code here in my question. One of the statement in the function was accessing a field that I had deleted because I didn't need it anymore.
I just deleted that line and it worked like a charm.
Thanks to every body who took a shot at this question.
Related
In my Struts app, I have an action, called Foo.
<action name="Foo" class="some.path.here.foo">
<result name="SUCCESS" type="tiles">/foo.tiles</result>
</action>
Normally it calls execute(), but I want to call another method called change(). How can I do so?
My Idea was this:
<form name="Foo" action="Foo" >
<s:textfield name="Mail" placeholder="Mail" />
<select name="someselect">
<s:iterator value="someblabla">
<option value="<s:property value="somevalue"/>" label="<s:property value="Description"/>"><s:property value="Name"/></option>
</s:iterator>
</select>
<s:submit method="change" value="Go!"></s:submit>
</form>
But when I want to do this, I get
HTTP Status 404 - No result defined for action some.path.is.here.Foo and result input
Can you help me out here?
execute is the default method in an action. If you want to change it i think you can modify the description of your action with adding the "method" attribute. Like that :
<action name="Foo" class="some.path.here.foo" method="change">
<result name="SUCCESS" type="tiles">/foo.tiles</result>
</action>
Hope this help
You are getting error because, struts expects 'execute' method unless you specify explicitly. If your action method name is different, you have to specify it explicity using the parameter 'method'.
So your code should be
<action name="Foo" class="some.path.here.foo" method="change">
<result name="SUCCESS" type="tiles">/foo.tiles</result>
</action>
Struts 2 also supports wildcard methods and dynamic method invocation. DMI is less secure, and not preferred. See the docs here
I have a form in my application .That form asks for data which is meant for two different tables of my Database.
Example
i have two tables Job and Customer,I ask for customers information like name ,email,phone number and i also ask for an starting time.This starting time field is meant for the Job table.
I was thinking to have two forms with two different actions and both actions being mapped to different action classes.But then i thought this would not be a good idea as i will have two different buttons to submit .
How can i map one action to two different action classes .
Here is what i was thinking
<s:form action="action1">
<s:textfield name="field1" label="name" />
<s:textfield name="field2" label="email" />
<s:submit />
</s:form>
<s:form action="action2">
<s:textfield name="field1" label="startingtime" />
<s:submit />
</s:form>
and action1 and action2 will be mapped to two different action classes in the controller(xml file).
what i want is
<s:form action="anaction">
<s:textfield name="field1" label="name" />
<s:textfield name="field2" label="email" />
<s:textfield name="field1" label="startingtime" />
<s:submit />
</s:form>
and this action be mapped to two different classes in the xml file
EDIT:I know the following is wrong,how do i achive something like this in the correct way
<action name="anaction" class="com.codinghazard.actions.Actionclass" class="com.codinghazard.actions.anotherActionclass">
<result name="success">pages/success.jsp</result>
<result name="error">pages/failure.jsp</result>
</action>
i want a single form to post data to two different tables .How do i achieve that?
I have googled and i was not able to find an answer due the fact that i was not able to search correctly (what to search for).
I am using struts2 framework.Please dont downvote.I am a begginer and this is my first framework.
You may achieve this using Chain Result type. In this after execution of one action, another action will be executed sequentially.
<action name="anaction" class="com.codinghazard.actions.Actionclass">
<result name="success" type="chain">anotherAction</result>
<result name="error">pages/failure.jsp</result>
</action>
<action name="anotherAction" class="com.codinghazard.actions.anotherActionclass">
<result name="success">pages/success.jsp</result>
<result name="error">pages/failure.jsp</result>
</action>
You should refer this link
[a link]http://viralpatel.net/blogs/struts-2-action-chaining-example/
I am trying to build a struts application with basic functionalists. My java servlet class myAction just returns success. I have compiled this file successfully and placed the .class file in the classes folder of my project.
My Struts Config XML is as follows:
<struts-config>
<action-mappings>
<action path="/view" type="myAction" validate="false">
<forward name="success" path="/first.jsp" />
</action>
<action path="/view" forward="/view.jsp"/>
</action-mappings>
</struts-config>
I have a form on view.jsp which has an action path to first.jsp
<form action="first">
Enter name :
<input type="text" name="name"/>
<input type="submit" value="Enter"/>
</form>
But when I run this code in Tomcat Server and navigate to view.do its working fine. And when I press the submit button of the form, the page is not getting redirected to first.jsp. Instead of first.do,the browser url is navigating to: **http://localhost:8080/MyProj/first?name=asdf**.
I am trying to debug this for past two days, but no improvement. Any help will be appreciated.
404 is the common error occurred When client was able to communicate with the server, but the server could not find what was requested.
Here the url, you trying to invoke seems to be invalid. Action path is the url path to access the page. And when you click on submit on the "First" jsp page, it points to the /first action path in the struts-config.xml
please check whether you have properly deployed and make the necessary configuration changes.
It works, if your rest of the configuration are correct.
http://localhost:8080/MyProj/view.do
Since you have form action of 'first' and there is no mapping found in struts-config.xml. I assume that on click of submit button, you wanted to go to myAction and returns to 'first.jsp'. Update '/view' to '/first' as follows and check.
<action path="/first" type="myAction" validate="false">
<forward name="success" path="/first.jsp" />
</action>
Also, change action to 'first.do':
<form action="first.do">
Enter name :
<input type="text" name="name"/>
<input type="submit" value="Enter"/>
</form>
I've written a submit button like this:
<s:submit type="button" value="Delete" action="%{notesDeleteUrl}" theme="simple"/>
And I've defined the url like this.
<s:url value="notesDeleteAction.action" id="notesDeleteUrl" >
<s:param name="noteId"><s:propertyvalue="iNote" /> </s:param>
</s:url>
So basically, I have no < s:form > tag on my JSP but I need to call an action with the submit button while passing a value to it. And I get this error.
There is no Action mapped for namespace [/] and action name [notesDeleteAction?noteId=48] associated with context path [/abc].
So I understand that it's unable to resolve the action because of the added parameter, but how else can I send this value to the action?
Your error is nothing to do with the parameter. Struts doesn't know what to do with the URL /notesDeleteAction
You'll need to include the action in your struts.xml file:
<package name="yourpackage" namespace="/" extends="struts-default">
<action name="notesDeleteAction" class="foo.YourClass">
<result>somepage.jsp</result>
</action>
</package>
There are 2 ways to get the parameter in your class, foo.YourClass
One way is:
Map parameters = ActionContext.getContext().getParameters();
The other way is for you class to implement org.apache.struts2.interceptor.ParameterAware
I have a homepage with a modal struts 2 jquery dialog which calls a struts2 action containing a struts form.
Homepage dialog code
<s:url id="newItemURL" var="newItemURL" action="addNewItem" />
<sj:dialog id="newItem" href="%{newItemURL}" title="Create New Item" width="700" position="top" autoOpen="false"
loadingText="Loading..." />
<sj:a id="addNewItem" openDialog="newItem" button="true" buttonIcon="ui-icon-refresh">New Item</sj:a>
addNewItem Action Result JSP
This form submits and produces a text result if successful.
<div id="newItemForm">
<s:form action="addNewItem" id="addNewItem">
<fieldset>
<legend>Create a new item</legend>
<label for="description">Description: </label>
<s:textarea id="description" name="item.description" label="Description:" cols="20" rows="5"/><br />
<sj:submit id="submitNewItem" targets="resultNewItem" value="Submit" indicator="indicator" button="true" replaceTarget="true" />
</fieldset>
</s:form>
</div>
<div id="resultNewItem"></div>
The addNewItem form works fine standalone with <sj:head />, with AJAX result appearing in the resultNewItem div. <sj:head /> has to be removed in the addNewItem JSP to avoid conflict with the homepage.
The issue is when addNewItem Action is included as part of the dialog upon submitting the form the homepage action is called and as a result I end up with another homepage inside the dialog.
How can I solve this?
Edit:
Struts config
<action name="homepage"
class="com.actions.Homepage">
<result name="success" type="tiles">Homepage</result>
</action>
<action name="AddNewItem" class='com.actions.AddNewItem'>
<result name="success">/WEB-INF/jsp/addNewRisk/addNewRisk.jsp</result>
</action>
<action name="smoAddNewRiskINSERT" class="com.actions.AddNewItem" method="addNewRisk">
<result name="success">/WEB-INF/jsp/addNewRisk/success.jsp</result>
<result name="error">/WEB-INF/jsp/addNewRisk/error.jsp</result>
</action>
What I want to achieve
User clicks button to load dialog, addNewItem form is created (AddNewItem action), when user submits form it is sent via AJAX submit button and the result is displayed within the dialog.
First of all, give your elements unique id-s. As pointed out in Quincy comment. Second, in your struts.xml file change action name to addNewItem instead of AddNewItem.