This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Dynamically Mapping Form Action with javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Dynamically Mapping Form Action with javascript" Watch "Dynamically Mapping Form Action with javascript" New topic
Author

Dynamically Mapping Form Action with javascript

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi everyone,

Can I dynamically assign the action property of an html:form tag in struts using javascript?
If I omit the action property, I am getting the error "According to the TLD or the tag file, attribute action is mandatory for tag form".
If I let the action="", my error would be "Cannot retrieve mapping for action"

I need to validate what the user have click in my jsp page then render the desired action using javascript. My javascript looks like this.



But I know that if I change the declaration from <html:form> to <form > tag, I can dynamically assign the action of the form using javascript. But I am using struts html tags so I need to implement <html:form>. Can I do it this way? Thanks.


Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, you can dynamically assign an action using JavaScript. Remember that the Stuts <html:form> tag gets rendered as a <form> tag when the response is sent to the browser. Also remember that JavaScript acts upon the plain HTML tags that are rendered to the browser, not the Struts tags you used to tell the JSP engine how to compose the page.

However, to keep with the rules of the Struts html tag library, you must provide an action attribute, even if you never intend to use it. Just specify action="/" to get past this requirement. Then when the page is submitted, your javaScript will provide the actual action.

One word of caution, though: The population of properties in an ActionForm that Struts normally does will only take place if your JavaScript specifies an action path for which an ActionForm has been defined.


Merrill
Consultant, Sima Solutions
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Merill,

Thanks for clearing about javascript non-interaction with struts tags.

I did try what you have said and put action="/" to get pass the requirements of the struts tag. But I got the following error javax.servlet.jsp.JspException: Cannot retrieve mapping for action /.

I am not sure about this, but is this because the declaration in my web.xml for goes like this
<url-pattern>*.do</url-pattern>
so that's why it can't find any mapping? Thanks

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Ok, I can see why this doesn't work. My bad. Struts has to know which action you're using not only when the page is submitted, but also when the page is first displayed, so it can populate any html:xxx fields with data. So, you can't just put "/" as your action. You must use a valid action that exists in your struts-config.xml file.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Merill,

Thanks. I did try mapping a valid action in my form tag and during the button click I render the javascript to run and assign a new path in my form action tag. Thanks for the help as always..
Radha Kethana
Greenhorn

Joined: Jun 11, 2003
Posts: 13
HI Mark & Merill,

I need to change the action of html:form dynamically depending on input URL. I did exactly what you guys were saying but could not make it work. Can you please help me on this one. I am getting an error saying that "The requested resource (/updateInsertIC) is not available." If I don't change the action dynamically then this action is working. That confirms that all configurations for this action are correct.

Here is the javascript code:
----------------------------

function OnSubmitForm()
{
if(queryString('operation') == "view")
{
document.myForm.action ="/viewinterfacecredentials";
document.myForm.target="lowerFrame";
document.myForm.submit();
}
else
{
document.myForm.action ="/updateInsertIC";
document.myForm.target="lowerFrame";
document.myForm.submit();
}
}
function queryString(parameter) {
var loc = location.search.substring(1, location.search.length);
var param_value = false;
var params = loc.split("&");
for (i=0; i& lt;params.length;i++) {
param_name = params[i].substring(0,params[i].indexOf('='));
if (param_name == parameter) {
param_value = params[i].substring(params[i].indexOf('=')+1)
}
}
alert("operation in viewTop.jsp:"+ param_value);
if (param_value) {
return param_value;
}
else {
return false; //Here determine return if no parameter is found
}
}


Here is the form tag:
---------------------
& lt;html:form action="/updateInsertIC" styleId="myForm" method="post">

Here the button which will call the script code:
------------------------------------------------
& lt;INPUT type="button" name="Go" value="GO" onklick="OnSubmitForm()">

Regards
Radha
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You've overlooked the fact that you need to add the ".do" suffix to your actions when you're modifying actions with JavaScript. Struts tags do this automatically, but you need to do it yourself in JavaScript.
Radha Kethana
Greenhorn

Joined: Jun 11, 2003
Posts: 13
Hi Merrill

Thank you so much for your help. It is working now. I could not have figured that out by myself in a short period. Appreciate your help.

Regards
Radha
Radha Kethana
Greenhorn

Joined: Jun 11, 2003
Posts: 13
Hi Merrill

I have been trying to resolve one issue but could not. I thought You might have an answer.Can you please help me on this.

Here is the issue.


In my struts application, I have a log in page that leads to homepage where I have all the opations like inser/delete... to manupulate database data. Home page is divided into 4 frames. Topframe for title, leftframe for list of operations and on right frame again divided into top and bottom frames. SO, when user clicks on delete he has to select some values that are displayed on right topframe and then click submit button. So in the corresponding action class first I will check whether the user is valid to perform this action if not it has to go back to login page. If the user is a valid user then correctsponding page will display on bottom right frame other wise whole page has to redirect to login page. That is where I am facing the issue. "In either case the resultent page is getting displayed in the bottom right frame itself". I specified redirect="true" for "login" forward which is a global-forward like below..

Struts_config
-----------------

<global-forwards>
<forward name="login" path="/logon.jsp" redirect="true"/>
</global-forwards>

<action path="/insertData" name="insertDataForm" scope="request" type="com.boeing.aiCredentialValidator.actions.InsertAction" input="/insert.jsp">
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/insertSuccess.jsp"/>
<forward name="failure" path="/blank.htm"/>


In my top right frame jsp page I have below code:
-----------------------------------------------------

function OnSubmitForm()
{
if(queryString('operation') == "view")
{
document.myForm.action ="viewinterfacecredentials.do";
document.myForm.target="lowerFrame";
document.myForm.submit();
}
else if(queryString('operation') == "delete")
{
document.myForm.action ="deleteConfig.do";
document.myForm.target="lowerFrame";
document.myForm.submit();
}
else
{
document.myForm.action ="updateInsertIC.do";
document.myForm.target="lowerFrame";
document.myForm.submit();
}
}

In my action class I have below code:
-------------------------------------

public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
StringBuffer operationType=new StringBuffer();


HttpSession session = request.getSession();
String target = new String("success");
EmployeesActionMapping employeesMapping = (EmployeesActionMapping)mapping;
if(employeesMapping.isLoginRequired())
{
if(session.getAttribute("USER") == null)
{
target = new String("login");
ActionErrors errors = new ActionErrors();
errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("errors.login.required"));
if(!errors.isEmpty())
{
saveErrors(request, errors);
}
return mapping.findForward(target);
}
}
.........
...............

}

Regards
Radha
[ July 07, 2008: Message edited by: radha kethana ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest putting the following JavaScript in your Login page. This will make sure that it is always displayed in the full browser and won't be contained in any frameset.


The above was obtained from this URL:

http://www.codelifter.com/main/javascript/framebuster1.html

You should probably credit the author if you use it.
[ July 07, 2008: Message edited by: Merrill Higginson ]
Radha Kethana
Greenhorn

Joined: Jun 11, 2003
Posts: 13
HI Merrill

You are amazing. I have been trying different options for the last one week. Thank you very much. I will definitely credit the author.

Regards
Radha
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Radha,

You're very welcome. One suggestion, though: Since this question has nothing to do with the original thread, it should have been posted as a new thread with a descriptive subject heading. Just keep this in mind for future questions.

Since these forums are searchable, it makes it easier for someone else facing a similar problem to find the answer from a search if there is only one topic per thread and subject headings accurately describe the problem being discussed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically Mapping Form Action with javascript
 
Similar Threads
Cannot Retrieve Definition for Form Bean Null
STRUTS: client-side validation in jsp using DynaValidatorForm
Struts Validator With Modules
Clientside validation using javascript in conjunction with STRUTS
how to populate dependent <html:select >