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>
Related
I am trying to develop an application in struts2 where I have multiple forms and may or may not have same action. To avoid CSRF I also added token with these forms, but only one token at a time working. If I clicked on another submit button then I am redirecting to error page which is configured for CSRF. I am stuck here why this is happening, am not able to use multiple token on same page or is there any solution for this.
Please find struts2.xml code which I have configured.
<action name="expUsers" class="com.org.action.ExpUser">
<interceptor-ref name="CSRFStack"/>
<result name="invalid.token" type="tiles">csrfError</result>
<result name="success" type="stream">
<param name="contentDisposition">contentDisposition</param>
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
</result>
<result name="failed" type="chain">csrfError</result>
</action>
I have the following dummy form example below which will let you understand what i am doing.
<form id="form-1">
<s:token/>
....
</form>
<form id="form-2">
<s:token/>
....
</form>
<form id="form-3">
<s:token/>
....
</form>
... so on multiple forms with token.
Thanks to All for giving me your suggestion. I have found a working solution so i am giving answer to my own question.
When we generate <s:token/> then the hidden field will be generate like this:-
<input type="hidden" name="struts.token.name" value="token">
<input type="hidden" name="token" value="1IPDKJ3QWM8X4JXAV0RKC0A9XVQ4I83E">
which is default behavior. Now if i want to generate different token for each struts2 token tag then i have to specify the unique name to token like this :-
<s:token name="unique-1"/>
Then it will generate hidden field like this:-
<input type="hidden" name="struts.token.name" value="unique-1">
<input type="hidden" name="unique-1" value="1IPDKJ3QWM8X4JXAV0RKC0A9XVQ4I83E">
Now i have two different token for two different forms in same page and this tricks also working perfectly without any issue at the time of writing this answer.
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.
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.
I am trying to handle the form-data and put it in the data base derbi that comes with netbeans.The server i am using is Glassfish. After filling the form when i click the submit data button, according to the action handler the request should follow to a servlet named FormHandler.do but when i try to reach out to the page FormHandler.do this is the error message displayed by the browser :
This webpage is not found
No webpage was found for the web address:
file:///W:/UnderTest/WebApplication_GLASSFISH/src/java/FormHandler/FormHandler.do
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.
But the file is there in the same directory as the html file (that has form). This is the type of url i give in the form tag:
<form method="post" action="FormHandler.do">
<b>Name:</b><input type="text" name="Name" /> <br /> <br />
<b>Email:</b><input type="email" name="Email" /> <br /> <br />
<b>Password:</b><input type="password" name="Password" /> <br /> <br />
<input type="submit" value="Register" />
What could be the reason for this problem ?
(After the request goes to the servlet FormHandler.do it checks (as per logic) if the text fields are empty. If they are empty it forwards the request to a servlet that shows the errors and if correct it inserts that data into the database and shows the success servlet.)
No webpage was found for the web address:
file:///W:/UnderTest/WebApplication_GLASSFISH/src/java/FormHandler/FormHandler.do
You need to access web resources by a HTTP path, not by a local disk file system path. Something like as:
http://localhost:8080/WebApplication_GLASSFISH/FormHandler.do
The /WebApplication_GLASSFISH part is the context path. It's unclear which one you're using, but this information is printed in the server's startup log. Something like as this:
INFO: WebApplication_GLASSFISH was successfully deployed in 3,770 milliseconds.
You need to make sure that the URL in your browser address bar matches this.
Also, HTML files should go in "Web Pages", not in "Source Packages". After you move the Registration.html into "Web Pages", you can open it by
http://localhost:8080/WebApplication_GLASSFISH/Registration.html
This way the form will submit to the proper servlet URL assuming that you've a
<form action="FormHandler.do">
This all is rather trivial. I'd suggest to go through a bit decent Servlet book/tutorial once again. Put your mouse above the servlets tag which you put below the question and click at the info link to get a good starting point.