Validating ActionErrors in JavaScript - java

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);">

Related

hide passed parameter from jsp to struts2 action class

<s:url action="someAction" var="act">
<s:param name="param1">value1</s:param>
</s:url>
<s:a href="%{act}">Go Action</s:a>
By clicking Go Action link, the address will be www.example.com/someAction?param1=value1
I want to hide passed parameters (param1=value1) like method="POST" in submitting of form. Is there anyway I can do it? Thanks.
No a URL is a GET request. If you want to POST you can do a post request with a form. If you want a button or link to do a post request, best to use JS/jQuery to hide the form and tie the buttons/links click event to submitting the form.
Struts2 has no control over the client side, there it's just HTML, CSS and JS.
As mentioned by JB Nizet, if it is about securing your information see the discussion here: How to send password securely over HTTP?
There is little issue sending most data in a get request (other than a password!) because if it is a form someone can readily see the content of the form, where seeing the parameters in the url is certainly harder.
From a server security stand point you should always assume the client can and will be able to send the worst possible data. That is why the validation framework in struts2 is so easy to use, and unlike primitive web programming systems, type conversion is a huge help. If you have a property that is an int and use that in a query you know it will be an int and not a String. If you need a String better use an Enum to ensure the value is allowed.
To hide passed parameter actually you need to submit the form. You should prevent the default behavior of the click event and replace it with the form event. Do it like in this example
<s:form id="f1" action="someAction">
<s:hidden name="param1" value="value1"/>
<s:url action="someAction" var="act"/>
<s:a id="a1" href="%{act}">Go Action</s:a>
<script type="text/javascript">
$(document).ready(function() {
$("#a1").click(function(event) {
event.preventDefault();
$("#f1").submit();
});
});
</script>
</s:form>

Form submit using JavaScript in Struts 1.2

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

Generating multiple events in JSP page having more than one button

I am a beginner in the JSP World as per I have noticed that, there is one form tag which has action and method attribute. In action tag we must write the URL of the servlet which gets activated after clicking the submit button.
But my problem start here. I am trying to develop a register page.
I have two servlets:
one checks for the availability of existing user,
second register the user.
Does anyone have any idea how to achieve it without a href link or image button?
Move the code that checks for availability of the existing user and register the user to its own service class say UserService. Make the form submit to 2nd servlet which uses UserService to perform both the operations.
You can have two separate forms but not nested forms in HTML. If you want a single form to change its actions (its target URL) depending on which submit is used, the only way you can achieve that is through javascript.
You can do this with your existing approach by registering a Javascript function
<body>
<script type="text/javascript">
function submitType(submitType)
{
if(submitType==0)
document.userForm.action="CheckUserAvailability.do";
else
document.userForm.action="RegisterUser.do";
}
</script>
<form name="userForm" method="post">
--Other elements here
<input type="submit" value="Check User Availabiliy" onClick="submitType(0)"/>
<input type="submit" value="Check User Availabiliy" onClick="submitType(1)"/>
</form>
</body>
Yes You can.. In the action you give the Servlet name. and inside that servlet you can call java methods which are written in other classes.
Which means you can check
1.one checks for the availability of existing user
2.second register the user
Using two java classes(may be according to your choice)
Just call those methods with in the same servlet..Guess you got an idea.

Having JSF spit out an HTML Search field. Doable?

I'm not a Java developer, but work with a team that is using JSF 1.2
We'd like to start using HTML 5 tags and attributes. It does not appear that JSF 1.2 supports those by default.
Is there anyway to have a JSF text tag:
<x:inputText>
spit out an html 5 search tag:
<input type="search" placeholder="blahblah" />
Right now, I'm having to let it output a regular text field and then I place inline JS after it to trigger a function that converts it client side:
<input type="text">
<script> funciton here that changes type to 'search' and adds placeholder attribute</script>
It works, but is a bit hacky. Is there a legitimate way to get server-side JSF to output proper HTML 5 tags?
Create a custom component. This allows you fine grained control over rendered HTML.
Or upgrade to JSF 2.0, then you can create a composite component which is a lot easier.

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