Printing an error message on screen using Wicket - java

So, I got a wicket page with some components I can update, and some of them updates automatically.
this.getFieldOneInput().add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
Panel.this.populateFieldTwo(target);
Panel.this.populateFieldThree(target);
}
});
There is an auto complete feature so I can fill my field on with available objects from DB. And every time i change my field one value, I will also populate fields two and three.
But I need to do something else here. I need to check if the chose object for field on got some null valued attribute. If any of its attribute is null, I want to show an error message on screen: "FieldOne attribute X is null". I want it to be shown on screen like those red wicket messages when some field is required.
How can I do that?

By using the Wicket FeedbackPanel
HTML:
<div wicket:id="feedback"></div>
Page:
Add a new FeedbackPanel, and just do error(message)
Reference: http://www.wicket-library.com/wicket-examples/compref/wicket/bookmarkable/org.apache.wicket.examples.compref.FormPage?0

Related

DropDownChoice and press back Button

Hi I created webapp and I have DropDownChoice (default value "A") but when I selected some item ("B") from DropDownChoice and then I do some Action (for example sumbmit) and after I press back button at browser so my DropDownChoice is selected to "B" but I want "A" because it is default value
HOW can I handle back press and set DropDownChoice on default value ???
Wicket keep page state even after you leave the page. If you visit the same page more than once, the state of your page is stored by Apache Wicket, and will be displayed to the user exactly before they leave the page.
You need to clear your model.
public class AnyPage extends WebPage{
public AnyPage(PageParameters pageParameters){
super(pageParameters);
}
#Override
public void onConfigure(){
// Clear the List of choices here
this.listDropdown.clear();
// set dropdown model to null
this.setDropdown(null);
}
}
#Override
public void onConfigure();
is executed everytime your page is loaded.
I guess that you are passing to the DropDownChoice a model that depends on the value that was submitted

Changed select on jsp get null value in servlet

I have a jsp page with a number of selects that are populated using jQuery i.e. they have the <option> tags, they just get it via a function. Each select has some 30 options each.
<form id="target" name="target" action="/project/myservlet" method="get">
<select class="myClass" id="sel1" name="sel1" ></select>
<select class="myClass" id="sel2" name="sel2"></select>
...
</form>
I received these values in my servlet using request.getParameter("sel1") but I'm getting null for the selects that are changed. As in, say I select values from the 2nd, 4th and 5th selects, then these selects get null values in the servlet. Others get the 0th value (default and unchanged) - which is okay.
This can help explain my question. I'm getting null in the next page when I modify the select.
According to this, if I use onload in select, it helps take the updated values to the next page for ONE select. But the problem is that I don't just have one select on the page. I want the updated values to go the next page without a page refresh/going to another page, unless submit for them is clicked. I think the request is not getting updated when the change takes place? Even the url in "get" doesn't get the changed selects.
There is no error as such, just that I am getting the values if the selects are unmodified (defaults). It sends the default values to the next page. When I select another option from the drop down, I get null on the servlet unless I use onchangeto submit the form. But that doesn't work for me since I have many selects. I can't keep submitting the form and going to the next page on every select change.
EDIT:
If it helps, here is my jQuery code:
$(document).ready(function() {
$('select').change(function(){
var v = $(this).val();
$('select option[value="'+$(this).data('old-val')+'"]').prop('disabled', false);
$(this).data('old-val',v);
if(v != "0"){
$('select option[value="'+v+'"]').not(this).prop('disabled',true);
}
});
$('select').each(function(idx,select){
var stateArray = ["Preference No. "+(idx+1),"Bill", "Sally", "Alice"];
$.each(stateArray, function(iIdx, iItem){
$(select).append('<option value="'+iIdx+'">'+iItem+'</option>');
});
});
$( "#target" ).submit(function( event ) {
alert( "Handler for .submit() called." );
});
});
EDIT 2: Servlet:
public class Preference extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
try {
PrintWriter out = response.getWriter();
String sel = request.getParameter("sel1");
}catch(Exception e){ }
}
}
You need to add <option> tags into your <select> tags.
Each <option> must have value attribute like this:
<option value="3">Apple</option>
Value from the value attribute of the selected option will be value of your <select>. You will get it on server-side by name as usual.
The selected options were disabled from all the select elements in my jQuery. I had to modify the jQuery a little. Disabled elements are not carried forward in a form. On modifying the jQuery to not disable the options, the problem was resolved. Had to take a bit of a help to recognize and correct that.

Richtext field (CKEditor) doesnt keep value after 2nd submit for new(unsaved) document

I have a serious problems with richtext field in my app. (Domino R901). I have extracted the scenario into simple Xpage with just one field and submit button only.
I have simple XPage now, with one field and submit button. RT field looks like this:
<xp:inputRichText id="inputRichText1" value="#{docui.body}"></xp:inputRichText>
Plus there is a standard button which runs JAVA action below with full submit:
This action is called on button click:
public void r(DominoDocument docui) throws NotesException {
Document cdoc = docui.getDocument(true);
if (cdoc.hasItem("body")){
print("Body exist");
} else {
print("Body doesnt exist");
}
docui.setDocument(cdoc);
}
Now when I enter some text into rich text field (CKEditor) and click on submit, it reloads the page (full submit) and I can see the text in CKEditor properly. Now when I do it 2nd time, text disapear and even in my JAVA code it says, that 'Body doesnt exist'. The problem seems to lie in fact, that CKEditor doesnt send content to server, when there is no change to it ... see here richtext control doesn't store the input content . I understand this behaviour for saved existing document, but when you have a new document, it simply doesnt submit value during second submit because it doesnt changed between requests. But because its a new unsaved document the value is simply discarded / lost . Is there any workaround how to deal with this?

GWT History - Why does it change the focus of my screen?

I have a simple application structure that will contain three composites at any one time - my header, footer and content composites. The composites are laid out in the follow manner.
<body>
<div id="header">
<div id="content">
<div id="footer">
</body>
The composites are assigned to the three <div>'s in my EntryPoint. The EntryPoint also contains a History Handler as follows.
History.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
if(historyToken.isEmpty()) {
parseTokens();
ContentContainer.INSTANCE.setContent(new IndexScreen());
}
else if(historyToken.equalsIgnoreCase("registration")) {
ContentContainer.INSTANCE.setContent(new RegisterScreen());
}
else if(historyToken.equalsIgnoreCase("login")) {
ContentContainer.INSTANCE.setContent(new LoginScreen());
}
}
});
History.fireCurrentHistoryState();
In my Composites, I have several ClickEvent handlers that look similar to the following.
#UiHandler("registerLink")
public void handleClick(ClickEvent event) {
ContentContainer.INSTANCE.setContent(new RegisterScreen());
History.newItem("registration", true);
}
This registers the history very nicely. It also brings me to the appropriate pages when I click the back button. However, it has the very odd effect of focusing the browser on the content composite, with the effect of the browser being scrolled to the bottom. It's not a deal breaker but it kind of breaks the user experience a little. (Do let me know if the explanation isn't clear, I'll add images)
What exactly is causing this and how can I fix it?
GWT history tokens work with the hash # token. In html this was originally intended to use the browser to focus on a specific part of the page, if an element has a name or id with the string after the # token. Could it be you have an id matching your history token?
Also in you handleClick you call setContent. But if I'm correct the call after it triggers a change event and will end up in the onValueChange, which also calls the setContent. So it looks like you are calling setContent twice here.

dynamically update textbox value from database when combobox value is changed

Here is the situation. I have a drop down menu. The option sin this drop down menu are being populated by fetching some values from the database. To do this following is what i have done.. :-
<select name="product_list" onchange="selectProduct(this.value)">
<option value="none">Select one</option>
<%
List<String> options = new ArrayList<String>();
DynamicCombo comboBox = new DynamicCombo();
options = comboBox.generateComboBox();
Collections.sort(options);
int tempVar = 0;
while (tempVar < options.size()) {
out.print("<option value=\"");
out.print(options.get(tempVar));
out.print("\">");
out.print(options.get(tempVar));
out.print("</option>");
tempVar++;
}
%>
</select>
DynamicCombo is a class that has a method called 'generateComboBox()'. This method simply returns an array list containing all the values that are fetched from the database, which is what i need to show in my drop down box in the front end (jsp page). On my jsp page i simply iterate through this list and print it as options appropriately.
This works absolutely fine.
Now i have another text box on my form, say 'textbox1'. Now the requirement is that this text box value should be updated depending on what the user has selected from the above drop down box.
So for example if the user selects 'prod1'(which is a primary key in the backend database table) option from the drop down box, then the corresponding value ( the product name) should be fetched from the database table and should be updated in the textbox named 'textbox1'.
The other thing is this entire thing is contained in a form which is supposed to be finally submitted to the servlet for further processing.
So how can i achieve this.
i figured out the solution to my own problem. It might not be the most elegant way of doing it, but it does the job pretty well.
So as per my requirement, what i exactly wanted to do was.... insert a value (that will be fetched from the database) into a text box on my form depending on what the user chooses from the drop down box that is already present on my form.
To achieve this, i went about and thought if some how i could nest a form withing my main form, it'd solve my issue. But i discovered that nesting of forms is not allowed. So the next option i thought of was to some how submit the same form without the user clicking on the submit button and also handle it appropriately as an 'incomplete' submit (in the sense that the form is still to be submitted manually by the user by clicking on the submit button) on the server.
So i simply made use of the 'onChange' event of a drop down box. I created an additional hidden field on my form.I wrote a simple javascript function that would simply set the value of the hidden field to the string-"partial Submit" and would submit my main form (say named 'form1') as :-
document.getElementById("hidden_id").setAttribute("value","partial submit");
form1.submit;
The function that does the above will be called whenever (and everytime) the onchange event of the drop down box gets fired.
When the user finally clicks on the submit button on the form to submit the finally completed form, then another javascript function is called that simply sets the value of the hidden field on the form to the string, "final submit" and would submit the form as :-
document.getElementById("hidden_id").setAttribute("value","final submit");
form1.submit;
Now on my server, i checked for the value of this hidden field as :-
if(request.getParameter("hidden_id").equals("partial Submit"))
{
// make a database connection, pass the value user selected from the drop down box
// to a prepared statement that does the query for getting the 'productName' from
// the database, collect the returned string in a variable and set a
// request attribute with this returned value. This value can simply be used in the
// jsp to fill in the value part of the textbox1.
}
else
{
if(request.getParameter("hidden_id").equals("final Submit"))
{
// do the rest of the final processing that needs to be done when user finally
// submits the completed form.
}
else
{
// throw an exception to take care of the possibility that the user might send
// in a 3rd value as a value for the hidden field.
}
}
Since you havent provided the code for selectProduct(this.value) , i presume that it submits the jsp page as when you change the value in the drop down.
If that the case in the servelt, set the value that you want to show in jsp in request object
request.setAttribute("valuetodisplay" ,valuetodisplay);
and now in jsp
<input type="text" value ='<%= request.getAttribute("valuetodisplay")%>' />

Categories