I want to call a Servlet which exists in a different war from my war. When user clicks a button we need to call the post method of the servlet. To implement this I did see an existing example which is slightly different but works in that case.
I am using jsf, so in the jsp there is a h:form with another html form inside of it. Below is the code:
<h:form>
<div id="gform" class="column span-20 append-1">
<h:outputText value="Text." /><br/><br/>
<h:commandLink id="addPaymentButton" styleClass="button" onclick='autorenew();return false;'> <span><h:outputText value="Payment Option"/></span> </h:commandLink>
<a id="noThanksButton" href="#"><span><h:outputText value="No Thanks"/></span></a><br/><br/><br/>
<h:outputText style="color:grey" value="Some text" />
<div> </div>
</div>
<form id="hiddenSubmit" method="post" action="https://localhost.myapp.com/myapp/LoginRouter" >
<input type="hidden" name="redirectUrl" value="/myapp/customers/addNewSavedCCInfo.faces"/>
<input type="hidden" name="jump_message" value="IAmJumpingToCC"/>
<input type="hidden" name="jump_url" value="/premiumServices/myPage.htm"/>
<input id="hiddenSubmitButton" type="submit" name="submit" style="display: none" value='' />
</form>
</h:form>
<script language="javascript">
function autorenew(){
window.alert('In js fnt');
document.hiddenSubmit.getElementById('hiddenSubmitButton').click();
window.alert('In js fnt COMPLETE');
return false;
}
So when the button is clicked, javascript is executed which submits the form to the servlet. However I can see in firebug that the second form which I need to submit does not appear. I am not sure how I can call the post method of a servlet class in a different war. Any ideas welcome, I am really stuck!
Thanks.
As per the HTML specification it's forbidden to nest <form> elements. The (mis)behaviour is browser dependent. Some browsers will send all parameters, some browsers will send only the data of the parent form, other browsers will send nothing.
You want to have a single form here. You can perfectly replace the <h:form> by a plain vanilla HTML <form> with the desired action pointing to the servlet in question.
Related
I am trying to open a popup using the below code in jsf.
Forgot password
<div class="modal fade" jsf:id="forgotPassword">
<div class="modal-dialog">
<form jsf:id="reset-password-form">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Request for new password</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="inputEmail">EmailId</label>
<div class="col-sm-9">
<input type="text" class="form-control" jsf:id="inputEmail"
jsf:value="#{myBean.passwordResetEmail}"
name="inputEmail" />
<h:message for="inputEmail" />
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary"
jsf:action="#{myBean.resetPassword}">sendPassword</button>
</div>
</div>
</form>
</div>
</div>
I am facing some issues here.
When the user enters wrong email Id, the user shall be on same page and errors shall be shown. But I see, the popup closes automatically when the invalid emailId is entered.
I also want to display success messages as well.
Can any one help me how to do that.
When the user enters wrong email Id, the user shall be on same page and errors shall be shown. But I see, the popup closes automatically when the invalid emailId is entered.
You're indeed synchronously submitting the form and performing a full page reload. It's exactly that full page reload which causes an apparent "automatically close" of the dialog (it's actually not closed, it's just exactly like you're pressing F5 in browser directly after opening the dialog). You need to submit the form asynchronously and only perform a partial reload.
You can do that by simply enclosing <f:ajax> in the UICommand component, exactly like shown for <h:commandButton> in the average JSF tutorial (which your <button jsf:action> passthrough element ultimately get converted to).
<button ... jsf:action="#{myBean.resetPassword}">
<f:ajax execute="#form" render="#form" />
</button>
Both execute and render attributes indicate in this particular example that the entire form must be processed, and that only the form itself must be partially updated. As long as the form is inside the modal dialog, and not outside, then this will keep the modal dialog open (at least, in the HTML DOM tree).
I also want to display success messages as well.
Just add a (global) faces message in action method.
public void resetPassword() {
// ...
FacesMessage message = new FacesMessage("Some success message");
FacesContext.getCurrentInstance().addMessage(null, message);
}
<h:messages globalOnly="true" />
A null client ID indicates a global faces message and those will only be shown when globalOnly="true" is set.
I have created a project that contains two login mechanisms:
The first one is the first authentication mechanism that uses a simple login page (get a user if exists from the database using a function findUser)
And the second authentication mechanism is the login mechanism offered by JAAS (j_security_check)
Basically I am trying to achieve this goal: when the user will
authenticate successfully (in the first login page) he should be
able to skip the second page login (offered by JAAS) without having
to reenter his username and password in login form (j_security_chek
) i prefer that the login page will not be shown at all and jump to the resources pages directly.
I need some suggestion, and if the question is not clear enough please do not hesitate to notify that, thank you in advance for your reply.
I founded a temporary solution, it works very well. So it's simple I just filled the j_username & j_password with values comming from the first login mechanism (the first one) and what i do is that i had created a function with javascript ( accessResource() ) That submiting the login form (the form with action = j_security_check) on load of the current page
jaasLogin.xhtml :
<h:head>
<title>Facelet Title</title>
<script>
function accessResource(){
document.forms["loginForm"].submit();
}
</script>
</h:head>
<h:body class="metro" onload="accessResource()">
.......
.......
<form id="loginForm" name="loginForm" method="post" action="j_security_check" class="form bg-login">
<div class="container span5" name="container">
<h:outputLabel>Utilisateur</h:outputLabel>
<div class="input-control text" data-role="input-control">
<input type="text" name="j_username" value="#{moduleBean.userBean.username}"/>
<h:commandButton type="button" class="btn-clear" tabindex="-1"></h:commandButton>
</div>
<h:outputLabel>Mot de passe</h:outputLabel>
<div class="input-control password" data-role="input-control">
<input type="password" name="j_password" value="#{moduleBean.userBean.password}" />
<h:commandButton type="button" class="btn-reveal" tabindex="-1"></h:commandButton>
</div>
<div class="buttons">
<input name="login" type="submit" value="LOGIN" />
</div>
</div>
</form>
wish that help you
I need a link which redirect me to a different site and send POST parameters. Something like:
<h:form>
<h:commandButton value="submit" action="http://example.com">
<f:param name="user" value="robson">
</h:commandButton>
</h:form>
The code above doesn't work of course.
I'd like to acheive this in HTML:
<form action="http://example.com" method="POST">
<input type="hidden" name="user" value="robson">
<input type="submit" value="submit">
</form>
Is that possible?
Use the vanilla HTML <form> tag, not the JSF tag if you're going to send form data to a non-JSF target.
The JSF form tag is designed to facilitate JSF postback operations, which is why it has no "action" attribute.
What is the proper way to call a servlet from a facelets file using a form with submit button? Is there a particular form required?
Just use a plain HTML <form> instead of a JSF <h:form>. The JSF <h:form> sends by default a POST request to the URL of the current view ID and invokes by default the FacesServlet. It does not allow you to change the form action URL or method. A plain HTML <form> allows you to specify a different URL and, if necessary, also the method.
The following kickoff example sends a search request to Google:
<form action="http://google.com/search">
<input type="text" name="q" />
<input type="submit" />
</form>
Note that you do not need to use JSF components for the inputs/buttons as well. It is possible to use <h:inputText> and so on, but the values won't be set in the associated backing bean. The JSF component overhead is then unnecessary.
When you want, for example, to send a POST request to a servlet which is mapped to a URL pattern of /foo/* and you need to send a request parameter with the name bar, then you need to create the form as follows:
<form action="#{request.contextPath}/foo" method="post">
<input type="text" name="bar" />
<input type="submit" />
</form>
This way the servlet's doPost() method will be invoked:
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bar = request.getParameter("bar");
// ...
}
You can call in below way from jsf:
<h:outputText value="Download" />
<h:outputLink value="#{request.contextPath}/files" id="btnDownload1" styleClass="redButton">
<h:outputText value="FILESDOWNLOAD" />
</h:outputLink>
</h:panelGrid>
Then in web.xml:
<servlet>
<servlet-name>files</servlet-name>
<servlet-class>com.Download</servlet-class>
It's our first JSF app, and I'm in the middle of integrating our graphic designer's CSS into our facelets files. He tells me that he needs the name and id attributes of the input tags to be the same as the for attribute of the label tag.
His request:
<label for="username">User Name:</label>
<input id="username" type="text" name="username" />
However, when JSF code renders the HTML, I get extra identifiers in these attributes.
My facelet code:
<label for="username">User Name:</label>
<h:inputText value="#{login.username}" id="username" name="username" />
Final XHTML that's sent to the browser:
<label for="username">User Name:</label>
<input id="j_id2:username" type="text" name="j_id2:username" />
It makes sense to me from a JSF standpoint, but is there a way for me to meet our graphic designer's request and make everyone happy? Or is this a bad JSF oversight?
Thanks!
You can use the JSF outputLabel tag, which should handle the ids automatically:
<h:inputText id="username">
<h:outputLabel for="username" value="User Name: "/>
</h:inputText>
Edit: To avoid confusion: You can also put the outputLabel outside the inputText Element. I just use it mostly like this.