I'm new to Struts 1 so may be its already a resolved question.
The situation is: I have a list of <html:multibox> tag, which are rendered into html-checkbox element when the page loads. I want the checkboxes to be checked by default (without using javascript/jquery).
You would set the fields in your Form if you want them selected. For multiple checkboxes with all the same name but different values, your Form should have a String[] property that holds all the selected values. Just populate that with the values you want selected by default. This could be something as simple as:
public void reset(ActionMapping mapping, HttpServletRequest request) {
if(multiboxField == null) {
multiboxField = new String[2];
multiboxField[0] = "optionOne";
multiboxField[1] = "optionTwo";
}
}
The best way to do this is with a *formname*SetupAction.java class.
Set your struts-config.xml to redirect people who click on your page to this SetupAction. Import your form class, populate your String[] with whatever values you want default-checked, and action-forward them back to your page. This also allows you to dynamically populate them, based on DB data or session variables or whatever you want.
Related
I'm loading some data into a hidden Webix combo box via URL with two parameters. After loading, I want to store all values from the combo box into an array. But this doesn't work. If it is possible to load data from URL to an array without loading it first into a combo, how does this work? I use Spring ResourceMapping annotation in the controller.
You can use a webix DataCollection which work as any webix data component but without any graphical representation :
var store = new webix.DataCollection({ url:"data.php" });
Another solution is to manually do ajax request :
webix.ajax().get('data.php', function(txt, data) { console.log(data.json()); });
Trying to add a filter form to a standard crud 'list' page.
// set filter defaults
ImageFilter defaultFilter = new ImageFilter();
defaultFilter.setYear(currentYear);
defaultFilter.setColor(user.getFavouriteColor());
// get filter for view
Form<ImageFilter> form = Form.form(ImageFilter.class).fill(defaultFilter).bindFromRequest();
ImageFilter filter = form.get()
bindFromRequest() alone gets a form with the filters a user specified, but on first load the fields have no selected value
fill(defaultFilter) alone does provide a form with the default filters selected
chained in this way the behaviour is just identical to the behaviour of the one called last.
What is the appropriate way to set the defaults?
Ideally, on the index page:
the default filter selection when not given any parameters
if requested with ?year=2010&color=blue those filters show on the form
if requested with ?year=2010 the form shows 2010 and the user's favourite color
Check if default values are present then go with the flow.
If not present, add set it in filter or render on UI using querystring params.
You can achieve what you want by splitting your code into two actions.
In the first one you fill a form with the default filter and display it to a user.
public static Result show() {
ImageFilter defaultFilter = new ImageFilter();
defaultFilter.setYear(currentYear);
defaultFilter.setColor(user.getFavouriteColor());
Form<ImageFilter> form = Form.form(ImageFilter.class).fill(defaultFilter);
return ok(index.render(form));
}
In the second action you simply bind form values from the request. If user changes some values form will be filled with them along with unchanged ones set as the defaults.
public static Result handle() {
Form<ImageFilter> form = Form.form(ImageFilter.class).bindFromRequest();
ImageFilter filter = form.get();
return ok("TODO handle form");
}
Edit
If you want to fill missing fields with the defaults after user input without showing them in the view before submit I'm afraid you'll have to do it manually after binding.
I have an ajax enabled list of records that I'm going through and each one has a dropdown box that I'm trying to make a required field for the form to submit. To complicate matters the 'Close Record' button is not the submit button so I can't just use required attribute on the select(dropdown box) that I'm using. The value for the selected dropdown box is saved in an Enterprise Java Bean so I thought I could just write a JavaScript function to check the value:
function CheckForm() {
var clearObj = document.getElementById("mySelect");
if(clearObj.value != "") {
return true;
} else {
clearObj.style.backgroundColor ='yellow';
}
return false;
}
This doesn't work because once I close one and go to the next it's maintaining the value of the previous record on the page. Basically I have an update-content event that I need to know how to handle. Any ideas as to how to manipulate the DOM or JSON object to make this select a required field? Thanks.
With the little information given, I would assume that when you close the existing record and then loading the next record, you are doing it through an ajax request. If thats the case, then you can add a call back for the ajax request, which would reset the drop down.
This should be a comment, but as you see, I dont have 50 points :-)
I am trying to make a filter for a searchfield where a number of checkboxes can be checked to choose what people want to search. I am currently trying to do this with the CheckGroup component but as I do not have a submit button I do not know how I can retrieve the latest checked objects. One thought of doing it was using Javascript, to call a function in Javascript and retrieve all the checkboxes like that. I currently have the following code in Wicket. So my question would be how to do this and if it is possible to not do this with Javascript. I have tried using AjaxFormChoiceComponentBehaviour and that works but since it does a post whenever a checkbox is checked, I think JS would be a better option.
public Filter(String id) {
super(id);
form = new Form("filterform");
types = resultItemHandlerPool.getTypes();
checkGroup = new CheckGroup<Class<?>>("checkGroup", new PropertyModel<Collection<Class<?>>>(this,"types"));
ListView typesListview = new ListView<Class<?>>("typesList", new PropertyModel<List<? extends Class<?>>>(this,"types")) {
#Override
protected void populateItem(final ListItem<Class<?>> item) {
item.add(new Check<Class<?>>("check", item.getModel()));
item.add(new Label("className", item.getModelObject().getSimpleName()));
}
};
typesListview.setReuseItems(true);
checkGroup.add(typesListview);
form.add(checkGroup);
add(form);
}
public List<Class<?>> getSearchableTypes() {
return types;
}
Thanks and kind regards,
Merlijn
You say you want to do the search server side. So, the server needs to know which items are checked in order to do the search.
Just use a plain old form for the searchfield (including checkboxes) and make it so that after entering the search-value the user posts the form. That way, the serverside code will receive the search value and the list of checked checkboxes and will know exactly what to search for.
AjaxFormChoiceComponentBehaviour does indeed update the server side Checkgroup after every click with an ajax post. If you only need to know the value of the Checkgroup after posting the search value, just don't use the AjaxFormChoiceComponentBehaviour and submit the form. Both a normal form submit and an ajax submit will work here.
I am developing an Wicket application. But my question is not really Wicket related. In that app I have a horizontal menu. This menu is created by few links. On clicking the link you will be navigated to some page. Now based on the page you are currently viewing the css class attribute of the link of the menu will be changed to "selected". This is the description of the problem.
Now I am solving this problem by using a integer value. The value is saved in the session and it is updated when any one link has been clicked. Based on that saved value, which link will be "selected", will be determined at runtime.
I am implementing this in following way:
//On link click I set a number in session
public void onClick() {
session.setValue(1);// or 2 or 3
}
When the menu is created I switch between the value and modify the css class, as follows:
switch(session.getValue){
case 1: add css to home;
case 2: add css to profile;
// and so on.
}
I was wondering that is this the only right way to do it? Or there some other better techniques or design patterns exist which can help me to achieve this in better way?
Store the menu items in an array (or an ArrayList):
items[0] = home
items[1] = profile
And use the index of the array as menu identifier. When you receive the selected menu itentifier, retrieve the corresponding item with
items[selectedItem]
You could also use a Map if the identifiers are not numbers, or don't go from 0 to N.
For a start, use an enum or static constants instead of magic numbers (1, 2, 3).
The Visitor Pattern is commonly used to avoid this sort of switching. You might not want to implement the full pattern in your case, but it's worth knowning. JB Nizet's answer may be more practical in your situation.
See https://en.wikipedia.org/wiki/Visitor_pattern
These SO questions might give you some ideas, too
Java visitor pattern instead of instanceof switch
Java Enums - Switch statements vs Visitor Pattern on Enums - Performance benefits?
I have implemented it using EnumMap and an Enum type as its key. I have defined an Enum:
public enum NavigationStatus {
HOME,
PROFILE;
}
In session I set the value of the current navigation as:
private NavigationStatus activeUserNavigationStatus;
public NavigationStatus getActiveUserNavigationStatus() {
return activeUserNavigationStatus;
}
public void setActiveUserNavigationStatus(NavigationStatus activeUserNavigationStatus) {
this.activeUserNavigationStatus = activeUserNavigationStatus;
}
Primarily I set it to: setActiveUserNavigationStatus(NavigationStatus.HOME);
Now where the menu is building I created an EnumMap:
EnumMap<NavigationStatus, Component[]> menuMap = new EnumMap<NavigationStatus, Component[]>(NavigationStatus.class);
And added elements to it, as:
menuMap.put(NavigationStatus.HOME, new Component[] { homeContainer, home });
And also on click methods of the links I set the status value:
public void onClick() {
session.setActiveUserNavigationStatus(NavigationStatus.PROFILE);
}
Last of all I checked the current value from the session and set the css class accordingly:
Component[] menuComponents = menuMap.get(getSession().getActiveUserNavigationStatus());
menuComponents[0].add(new AttributeAppender("class", new Model<Serializable>(" active")));
menuComponents[1].add(new AttributeAppender("class", new Model<Serializable>(" active")));
This is without switch statement and combines the idea of JB Nizet's ArrayList index and Oli Charlesworth's Enum.
Thank you.