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.
Related
In Thymeleaf I would like to generate following HTML
<span data-my-attr>
</span>
I would like to display data-my-attr conditionally, but there seems to be no way how to display or NOT to display an empty attribute conditionally.
In case of required attribute there is th:required but for custom attributes there is nothing.
I tried to use th:attr="'data-my-attr'=${value}" and value is true or false, but it does not work.
Let's assume condition is true when your attribute should be shown and false when it shouldn't. You can have following:
<span data-my-attr th:attr="${condition} ? 'meaningless' : 'data-my-attr'=''"></span>
Explanation:
According to this thread when you specify the empty value for an attribute within th:attr then Thymeleaf will delete this attribute. So in above snippet:
data-my-attr is added by default.
The empty value is assigned to an attribute using th:attr.
The name of the attribute overriden with empty value is selected accordingly to ${condition}.
When ${condition} is true then data-my-attr should stay so any meaningless name (not present within the tag) should be picked.
Otherwise data-my-attr should be deleted so this name is picked.
Feels hacky but such a way of attribute removal seems working since 2012. Therefore I'd consider it stable.
HiddenFor description is:
Returns an HTML hidden input element for each property in the object that is represented by the specified expression.
I read that it is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.
HiddenInput description is:
Represents an attribute that is used to indicate whether a property or field value should be rendered as a hidden input element.
Can you please tell me when is HiddenInput useful? and when to use it instead of Html.HiddenFor?
Thanks!
HiddenFor is determined by the view - the view creates a Hidden input value from any element of the viewmodel or data.
HiddenInput is an attribute on a field in the (view)model - which means that it can be used across multiple views and indicates that this field should be hidden wherever it is rendered by an EditorFor helper
My bean is viewscoped. I have a simple string property with a getter and setter. The getter works fine(checked by initialising the property), but not the setter. In the setter method I am building a Stringbuffer using each of the incoming parameter.
Code:
public String getParamval() {
return paramval;
}
public void setParamval(String paramval) {
logger.info("Incoming value:" + paramval);
pvals.append(paramval);
this.paramval = "VAL";
}
Is that wrong? I have tested within the setter to see if the input string is being passed but apparently the method is not being called/invoked at all. In the view am using a #{} notation.
View:
<c:forEach items="${gdsiGeodataBean.requiredfields}" var="reqs">
<h:outputLabel value="#{reqs}:* " />
<pou:inputText value="#{gdsiGeodataBean.paramval}" required="true" requiredMessage="Input is required."/>
</c:forEach>
And why would I wanna build a stringbuffer in a setter method? because, the inputtext are created dynamically based on a dynamic list. I only have one bean property to bind to.
I know I could use a map, but for the same reason as above, i seem not able to updated a map values in the setter method. This is connected to the question I asked here Updating a map value in a managed bean
Even though the approach is entirely wrong (the getter won't return the right value after you submit the form!) and I have already answered your previous question as to using the map, the setter should really be called in this particular case.
That the setter is not called can have several causes. The most famous is that the form isn't been placed inside a <h:form> or that you're incorrectly nesting multiple <h:form>s inside each other. Another cause is that the input component or one of its parents have a rendered attribute which happen to evaluate false during the submit request.
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.
Struts 2 will resolve all property names during view rendering against the top object in the ValueStack first. But how can one access a property with the same name on the object lower on the stack?
Example:
Let's say I have an Action class called MyAction and it has a a logDate property. In the view rendered after this action is invoked, there's this:
<s:iterator value="users" status="itStatus">
<s:property value="logDate"/>
</s:iterator>
Now imagine a User object also has a logDate property. During the iteration, Struts 2 will always push the current user object to the top of the ValueStack effectively resolving all property names against it. But, what if I wanted to access logDate from the action? What's the correct way to do this?
You can use an index to look at a substack. For example:
[0].logDate would start the search at the stop of the stack and find the logDate on the user object. [1].logDate would start the search one level deeper and find the logDate on the Action class. This OGNL page (see the section right above the Accessing static properties header) has some more detail and here is another explanation.