Form submit using JavaScript in Struts 1.2 - java

I am using Struts in my application.
I am trying to submit a form using javascript. Hence I did this in html file:
<html:form name="returnForm" action="/returnMedia">
and this in <script> tag:
document.forms["returnForm"].submit();
This is normally what we do while submitting a form using javascript.
But in struts 1.2, I am getting an exception, asking me to set the type for the form also.
Checking the docs, it seems this is not the correct way to submit the form in struts 1.2.
Please let me know how to proceed with this.
Thanks,

how about this
{
document.forms[0].action = "UserAction.do?method=add"
document.forms[0].submit();
}
look at this link
http://www.dzone.com/tutorials/java/struts/struts-example/struts-dispatch-action-example-1.html

Related

Sitemesh custom javascript per page

I am using sitemesh for a spring based site. The problem is that I have some javascript that I want it to run on the onload event of only one specific page using jquery $(function() { ... }) and not on every page.
I have included the jquery.js in the bottom of my decorator, after the body. So, if I try to include a <script> tag in the body of my decorated page the script won't be executed because jquery will be loaded after that! I know that I could include the jquery.js in the header of my decorator so it will be before the custom script in the decorated page however I don't really like that solution since it will contain javascritp in the head of my page.
So I would like to have something like a placeholder in my sitemesh decorator in where the custom from my decorated page will be placed (as you can understand I come from the django world :p). Is this possible ? Do you propose anything else or should I just put my jquery.js in the header and be done with it ?
To answer my question, after some search I found the following solution:
I Added the following to the end of my decorator page (after including jquery.js)
<decorator:getProperty property="page.local_script"></decorator:getProperty>
I also added the following
<content tag="local_script">
<script>
$(function() {
alert("Starting");
});
</script>
</content>
The decorated result page contained the contents of the local_script tag exactly where I wanted them and everything worked fine :)
I don't know why this feature of sitemesh is not properly documented - using this you can have a great templating behaviour (like django).

Validating ActionErrors in JavaScript

Is is possible to validate or view the ActionErrors/ActionMessages in JavaScript? so that proper javascript alert can be displayed on the screen.
Thanks.
Javascript used for client-side validations, where as ActionErrors/ActionMessages are for serverside.
Precisely, Struts validation is an alternative to the Javascript validations. So Why would you want to combine both of them? I feel it is meaningless.
yes you can.
you must add these 2 lines to your jsp file.
<script src="scripts/libs/staticStrutsJvs.js"></script>
<html:javascript formName="XXXForm" dynamicJavascript="true" staticJavascript="false" bundle="someBundleName" />
File staticStrutsJvs is a part of struts framework.
These lines automatic generate javascript validation code with method names
validateXXXForm()
In the end you have to call this method before submit form.
<html:form action="/XXXAction" method="post" onsubmit="return validateXXXForm(this);">

How do I inject another JSP page into a <div> when clicking a link?

I have two different divisions in a JSP page. One contains a menu of links, when clicked the div2 (id-content) loads different pages accordingly. I am doing something like -
<div id="menu">
<ul class="navbar">
<li><a name="login" href="Login.jsp" onclick="changeContent()">Login</a>
</li></div>
and in the script I have something as -
<script language="JavaScript">
function changeContent() {
document.getElementById('content').load('Login.jsp');
}
</script>
I also tried -
document.getElementById('content').innerHTML=
"<jsp:include page="Login.jsp">";
None of the ways worked. Please suggest how should I
Try jquery..
function changeContent() {
$('#content').load('Login.jsp');
}
The solution is to use Ajax, which will asynchronously retrieve your page content that can be pasted in with the innerHTML method. See my answer to a similar question of how an Ajax call works and some introductory links.
As to why your examples in your answer don't work, in the first case there is no load() method on an Element object (unless you've defined one yourself and not shown it). In the second example, as one of the question comments says, there is probably something causing a syntax error in the javascript.
As an FYI, when there is a syntax error in some javascript in a web page, the current expression being parsed and the rest of the <script></script> block will be ignored. Since this is inside a function declaration, that function will never get defined.
For instance, an embedded quote in the included page will end the string for the innerHTML assignment. Then the javascript parser will try to parse the remainder of the HTML causing a syntax error as the HTML will not be valid javascript.
We use jquery. Add a click event handler to the anchor elements. In the click handler call $('#content').load(your_url);. You might want to use the load(url, function() { ...}) version. More info here http://api.jquery.com/load/
Your initial page comes down from the server. It's displayed by the browser. When you click on a link (or a button) in the browser, you want to fill the second div with new HTML. This is is a perfect job for an AJAX request. What the AJAX object in the browser does, is to send a POST (or whatever) string to the server. And then the Ajax object receives the HTML response back from the server. And then you can display that response data which the AJAX object contains, anywhere you want.

Accepting http session request parameter in jsp page using jquery

I am working in a web application using struts. I am performing some operations in struts action class and using session.setAttribute i am setting result value. My struts action class sends result to jsp page where i am displaying result. I have to display message after loading jsp page, hence i am trying to use jquery. But i am not getting how to handle http sesion request in jquery.
Earlier i was taking one property in sturts-config.xml and setting the value instead of sending http session request parameter. But it doesn't work for me, because every time when i refresh my page it takes initial value from struts-config.xml. i am taking struts property as hidden field in jsp page by writing:
<html:hidden property="propname" styleId="id_propname" name="formname" />
for setting property value null I am writing following code in jquery.
<script type="text/javascript">
$(document).ready(function()
{
alert("some text");
$("#id_propname").val("");
});
</script>
Is there any way where i can directly get the http session request in jquery ? please reply me if you can give some suggestions I thanks to all valuable suggestions in advance.
IMHO there is no simple way to pass your session to the jquery . What you can do is to set the value of some hidden field and declare a global variable on the script and access the value and check for the valid condition if any .
Although its ugly and highly discouraged to use scriptlet inside jsp , this might work :-
<% String sessionVal = session.getAttribute("yourSessionValue").toString() %>
<html type="hidden" id="id_propname" name="propname" value=<%=sessionVal> >
In your jquery :
<script type="text/javascript">
var sessionVal;
$(document).ready(function()
{
sessionVal = $("#id_propname").val();
alert("this is the session value at Script " + sessionVal );
});
</script>
Null check for session is not done.
Or
If you are using tag libraries you can set the session value on form bean and use something like this in jsp :-
<logic:equal name="yourFormBean" property="yourProperty" value="someDefinedValue">
Do your action here
</logic:equal> <br />
Well http Sessions and Requests, in the form of java Objects and related to servlets, live in the server side. JQuery on the other hand will "normally" live in the client side... therefore your problem description is a bit "off".
The parameters you "send" in the httpsession or/and in the httprequest are for the jsp generation where jQuery does not play any part. What then gets sent to the browser is the HTML generated by the jsp engine. This HTML when in the browser can then be used by jQuery.
By the way the framework name is Struts and not Sturts.
Taking in account this information I don't really understand what in fact you want to do.

Strut2 validation, Overriding css_xhtml theme

HI Folks,
I'm using the Struts 2 validation. And for displaying the custom error messages I'm overriding the css_xhtml.. and the validation.js for client side validation. Every thing is going well but the validation is on form submit.. can i do it for all the form fields Onblur event.. Any suggestion highly appreciated
Thanks in Adavance
Cheers,
Vinayak V B
You could just call the forms onsubmit function through an onblur handler. Something like:
<script language=javascript">
function callOnsubmit(form) {
var code = form.getAttribute("onsubmit");
eval(code);
</script>
<s:textfield name="foo" onblur="callOnsubmit(this.form);"/>
you can use the jquery plugin to have onblur (client side) validation on your forms.
Furthermore, you may use DWR validation for struts2. You will have to go through some setup procedures that involve adding the DWR.xml in your configuration file but once all that is done, you will be able to have AJAX validation on your forms. Example

Categories

Resources