Map being read but not set by HtmlInputText - jsf-2

I have a dynamically created HtmlInputText, which is set to pull its value from a map in a session scoped bean. Like so.
HtmlInputText input = new HtmlInputText();
String expression = "${catalogue.itemValues.A" + item.getId() + "}";
ValueExpression valExpression = expressionFactory.createValueExpression(facesInstance.getELContext(), expression, String.class);
input.setValueExpression("value",valExpression);
where itemValues is a map with a getter of getItemValues() and the key would be A1, A2, etc.
I have programatically added a value to the Map with the key A1 and value 1234. Whenever the JSF page appears, the value is rendered. However, when I change the value and submit the form, the value is not changed.
I have tested with a h:inputText element and linked it to the same key and it is able to update the value and the new value is reflected in the generated HtmlInputText component.
How is this caused and how can I solve it?

You should be using #{} syntax to bind input values, not the ${} syntax. The #{} can do a get and set, while the ${} can only do a get.
See also:
Difference between JSP EL, JSF EL and Unified EL

Related

Struts OGNL expression [duplicate]

Fetching some variables in hidden field using struts2
<s:hidden name="ABCFormBean.dependentLists[0].lastNameEng" id="h_dependantLastNameEng_id"/>
I need to display the same in the same page
<s:property value="ABCFormBean.dependentLists[0].lastNameEng"/>
doesnt work
how to display the content?
Nothing gets displayed because the property value doesn't map the value in the value stack. Bu default the action is on top of the value stack. If you map properties they should should be initialized and have getters and setters. To display
<s:property value="ABCFormBean.depLists[0].firstNameEng"/>
the following method calls are expected
getABCFormBean().getDepLists().get(0).getFirstNameEng()
If you can get this value in the action method, then you would have the values been displayed by the property tag.

How to retrieve a property value from modelstate

How can I retrieve a property value from modelstate in httppost action.
Below is the code, I used to retrive the hidden id field from modelstate. But is it not possible to have strongly typed version to get value. Like, if property name is modified, prompting a compile time error.
Could anyone please explain difference between "AttemptedValue" and "RawValue".
ModelState state;
if (ModelState.TryGetValue("id", out state))
{
string value = state.Value.AttemptedValue.ToString();
}
Attempted value is used by the framework and it contains concatenated list of values. In my case, since it is id field, I am going ahead with attempted value. Below link has more information on this.
http://forums.asp.net/t/1571473.aspx/1?MVC+2+Custom+ModelBinder+and+storing+the+attempted+value+for+the+view
you can iterate the ModelStateDictionary object and through the keys(property name) on the dictionary get the value of the desired property or you can do something like ModelState["PropertyName"].Value

how to set a value to <s:hidden > from the action class without using list and iterator in struts 2?

In action class i am reading value from the database.
Let's say "abc".Now the "abc" value should be populated to jsp page.i.e "abc" value should set to s:hidden field in the jsp page.
Since it is single value , i don't want to use List in the action class.
Is there any other way to do that ?
Why would you want to use a list? Just provide the appropriate getter like:
public Object getAbc(){
return abc;
}
and in your page access it with simple OGNL expression like:
<s:hidden name="filedYouWantToSetThisValueTo" value="%{abc}"/>
Hope I got it right.

String[] vs Integer[] in Struts2 action attribute

I am new t struts2. i built one page, that shows list of employees. I can seach the employes based on his name by applying filter criteria and click on find button. At the same time, i provided check box in the left side for each employee name for delete operation. for all checkboxes, i gave Integer[] attribute name which is declared in Custom Actionclass.deleteaction is working fine. But when i click Find button the action is not getting submitted. Then i changed Integer[] to String[] both functions are working fine. What will be the problem? is it something like, attributes should only be String type.
The cause of your problem is that the Struts2 checkbox sets a boolean property on the action class:
Struts 2 Form Tags: Checkbox
When you defined the checkboxes as an Integers, the framework couldn't covert the boolean to an Integer. However it was able to convert the boolean to Strings. If you check the results in your action class, you should see the String[] populated with "true" and "false".
In general Struts2 is pretty good at converting submitted form data to whatever object type you want. Check out the docs on type conversion for more info.

Add empty value to a DropDownList in ASP.net MVC

I'm building a data entry interface and have successfully bound the columns that have reference tables for their data using DropDownList so the user selects from the pre-configured values.
My problem now is that I don't want the first value to be selected by default, I need to force the user to select a value from the list to avoid errors where they didn't pick that field and by default a value was assigned.
Is there a more elegant way of doing this than to add code to include an empty value at the top of the list after I get it from the database and before i pass it to the SelectList constructor in my controller class?
The Html helper function takes a 'first empty value' parameter as the third argument.
<%=Html.DropDownList("name",dataSource,"-please select item-")%>
You can also use this way:
dropdownlist.DataTextField = ds.Tables[0].Columns[0].Caption;
dropdownlist.DataValueField = ds.Tables[0].Columns[1].Caption;
dropdownlist.DataSource = ds;
dropdownlist.DataBind();
dropdownlist.Items.Insert(0, new ListItem("Select ...", string.Empty));

Resources