Can I not submit a form to a servlet from JSF? - forms

* Unable to find matching navigation case with from-view-id '/home.xhtml' for action 'MemoServlet' with outcome 'MemoServlet'
I try to accomplish it through:
<h:commandButton type="submit" value="add" action="MemoServlet"/>
but all the tutorials in the world only do it with a bean, which i don't want. I've come across any navigation rule that accomplishes my request.

Why a servlet? What exactly is the functional requirement? Doesn't the servlet contain "too much" code which you could just refactor into a separate class and import/call that in both the original servlet and the JSF bean action method?
Anyway, to fix the particular problem, you need either a plain vanilla HTML <form> element whose action points to the servlet URL or to call ExternalContext#dispatch() on the servlet URL inside the bean's action method.

Related

Dynamically loading content through Groovy server page upon form submit

I have a Groovy project (vanilla; no Grails) with an index.gsp that takes form input from the user and sends it in a POST request to a Groovy script. The form is set up like this:
<form action="somewhere" method="POST" enctype="multipart/form-data">
// some other inputs
<input type="submit"/>
</form>
Is there any way (ideally not using Javascript) to dynamically load content on the same page after the user submits? Redirecting to another GSP might also work. Just something simple, like a string containing whatever the user typed. It seems like Grails has plenty of options, but unfortunately I can't use it.
As you mentioned, Grails is capable of doing what you need without any complex code. Since you can't use it, you will have to use JQuery(Javascript) to make an AJAX call. AJAX is the he only way that I know to achive that.
Just make an AJAX call to your groovy script. JQuery.ajax has a success function to be called if the request succeeds. You can use it to update a hidden dive after the form. This success function has the data returned from the server as an argument, that data could be the string containing whatever the user typed. In that case just add the data to the hidden div and then make that div visible.
function onSucceed(data) {
$('#hiddenDivToUpdate').text(data);
$('#hiddenDivToUpdate').show();
}
You can learn about JQuery.ajax() in this link AJAX

Struts 2 missing reset method?

I am migrating a project from struts 1.3 to struts 2.0. Now I have read through various resources available on web but could not find a struts 1.3 reset() form method alternative in struts 2.0.
My action class extends ActionSupport and implements Modeldriven. I have implemented the validate method in action class and that works perfect. However, when the form has no errors and the submission is done properly, the page reloads with initial values.
I expected that I will receive a blank form. I looked for a reset() form method but could find none. Currently, I am explicitly setting all form values to blank. I don't see this as a good programming practice. Please suggest how to implement form reset() in struts 2.0.
EDIT: ok, you are working with Tiles (that I've never used), then I'll try another solution:
if you have an empty JSP tile, and after the user compiled its fields and submitted the form, you want to ALWAYS clear those fields... what about declaring only SETTERS in the Action, and not the GETTERS ?
You could only set from JSP to Action, but not read them from the JSP... then your fields will be always empty in the page, and always rewritten by setters in the Action.
If i got it, you are on JSP, you call the method saveStuff() of the Action (or of another action), then you come back to the page...
in this case, you can use a RedirectAction return type, that will strip all params from the request, and redirecting your request to the execute() method of your Action.
So, instead of
<action name="myAction" class="com.blabla.myAction">
<result name="success">/myPage.jsp</result>
</action>
just do
<action name="myAction" class="com.blabla.myAction">
<result name="success">/myPage.jsp</result>
<result name="stuffSaved" type="redirectAction">
<param name="actionName">myAction</param>
</result>
</action>
This way you re-enter in the initial state (assuming you are not putting anything in session that is read by the JSP).
Because your Action and Form are in the same class in Struts2, you have to implement your own reset() method to clear any data that you would like.

Dont fill request scoped bean

I have a request scoped bean which is filled with various properties from a form. These properties are then used to update another view scoped bean. Now I want to give the user the possibility to the reset the form in such a way that all form fields are holding the values they had when the page was loaded the first time. These values are defined through the bean itself:
#ManagedBean
#RequestScoped
public class ItemSearchBean {
private Rarity minRarity = Rarity.None;
private Rarity maxRarity = Rarity.None;
...
}
Notice though that the form submiting button actually invokes a ajax request, therefor no full page reload goes on.
The submitting button:
<p:commandButton value="Search"
actionListener="#{itemSearchBean.refreshTable}"
update="itemTable,notify"/>
I already tried to use a simple reset button, but it only reseted the form to the last submitted values:
<p:commandButton type="reset" value="Reset"/>
One has to somehow ask the server for a fresh new bean (or prevent it to fill the bean), but I have no clue how to do this.
You should be able to do that with a plain HTML link to the same view:
Reset
or let JSF create the link for you:
<h:link value="Reset" />
This way you'll have a new GET request that will create a new UIViewRoot, just like if you were accessing the same view in a new browser tab.
If you want a button instead, you can use an h:button
<h:button value="Reset" />
This button will rely on a Javascript that will reload the page on click.
you could also do a two phase setup. a little more work, but allows all sorts of Undo workflows if you take it further.
basically, have two beans that encapsulate your form with the usual accessor/mutator pairs. then in your controller, expose only one (either x, or y) and that is your "form bean". in y, you store the pristine copy that is retrieved from the repository, that way you can do field level or bean level reversion without having to reload the page or do a full data remarshall from the repo.
so, simply:
2 beans to encapsulate the form (you might even be able to re-use your entity if it maps onto the form).
fill both from the repo during data marshall
expose one to the user in the facelets
keep the second as a backup for resetting/reverting either fields or the entire form
there are other ways to skin the "undo" cat, and this is only another.

Avoid Scriptlets in Jsp for displaying data on page load

I realize that when you submit the form in a jsp, in the mapped servlet you can get the desired data, set it in the proper scope(say request) and forward it to jsp like this:
request.setAttribute("myList", myList); // Store list in request scope.
request.getRequestDispatcher("/index.jsp").forward(request, response);
However am wondering for pages which doesn't have a form or in other words we want to display data as soon as page loads, how can we efficiently load the data without using scriptlets like
<%= myBean.populateData("String Argument_1")%>
Would highly appreciate if anyone can provide any recommendations around the same.
The fact that the request comes from a form or not doesn't change anything. The servlet receives a request, and then can do some processing and forward to a JSP:
servlet gets request parameters
servlets uses those parameters to get requested data from a database, and populate some beans with said data. It may also build some beans from scratch, to display a form with default values
servlet puts those beans in request attributes
servlet forwards to a JSP
JSP avoids using scriptlets and rather uses JSP EL, the JSTL and custom tags to display the information stored in the beans in request scope
I think using EL in combination with JSTL can help you in the most common situations. If this is not enough you can write EL functions or your own custom tags.

How to submit a form in RichFaces?

I know it maybe sounds a basic question but I'm having a hard time figuring this out.
First of all I have this form:
<h:form>
<h:inputText value="#{movies.name}"/>
<a4j:commandButton id="mybutton" value="Modify" immediate="true" action="#{movies.testModify}"/>
</h:form>
I want to catch the value from the input text from within my testModify() method from movies bean.
My problem is that testModify doesn't get called at all. The odd behaviour that I noticed is that when I remove h:form tag the method does get called but I still don't know how to get the value from my input text.
From what I've read, a4j:commandButton needs h:form for it to work properly.
Any help will be highly appreciated!
Use an h:commandButton instead of an a4j:commandButton. The first is the standard JSF button that will submit your form, the latter performs an ajax request.
First of all, as stated by Markos Fragkakis, use a basic <h:commandButton> instead of an ajaxified <a4j:commandButton>, as in your case, you have no interest of using an Ajax action here.
Second, remove the immediate="true" attribute on this button. Using this attribute means that the default ActionListener should be executed immediately (i.e. during Apply Request Values phase of the request processing lifecycle), rather than waiting until the Invoke Application phase.
If your action on Java bean is still not called, maybe something wrong happend before the Invoke Application JSF lifecycle phase. Adding a <h:messages> will display the possible issues:
<h:form>
<h:messages/>
<h:inputText value="#{movies.name}"/>
<h:commandButton id="mybutton" value="Modify" action="#{movies.testModify}"/>
</h:form>