Cannot display a JSTL Value into input field - Spring MVC - java

I have this value ${agact.ppr} that i want to show in my page jsp. It can be shown like this easly:
<c:out value="${agact.ppr}" />
but I want to use it into input form like this:
<form:input type="text" value="${agact.ppr}" path="ppr" />
but it's not working I don't know why. I displayed others attributes in input fields without problem and this one no.
My class is like this :
Class Agent{
int ppr;
/** setters and getters**/
}

Try removing the 'value' attribute, 'path' should be all you need there. Also make sure the getter and setter are valid.

Related

Unable to get data input in the jsp, from jsp to a form, using logic:iterate

The main problem is that I use Struts 1.0.2. But I can't change it.
I have an issue using an attribute of an arraylist in my jsp. The display works fine but I don't get the data instanciated in the arraylist in my form. In debug mode I see that it never comes to the getter of my property. I've found 2 solutions in struts 1.0 but I can't make it works.
The arraylist in my ActionForm :
private ArrayList<AccountScreenBean> accountsScreen = new ArrayList<AccountScreenBean>();
The class declaration of the Account object in the Arraylist :
public class Account implements Serializable, Cloneable {
private String bic;
public String getBic() {
return bic;
}
public void setBic(final String newBic) {
bic = newBic;
}
}
The call in my jsp :
<logic:iterate name="BankAccountsActionForm" property="accountsScreen" id="accScreen" indexId="index">
<html:text name="accScreen" property="<%="accountsScreen["+index+"].bic"%>" size="18" maxlength="11" onkeyup="suivant(this,'partPays',11,'accScreen')"
styleClass="inscCB_SaisieTexte" />
</logic:iterate>
The console error :
weblogic.servlet.jsp.CompilationException: Failed to compile JSP
/jsp/Contents/bankAccountsContent.jsp bankAccountsContent.jsp:171:81:
This attribute is not recognized.
" size="18"
maxlength="11".
with accountsScreen underlined
I've also tried another way in my jsp :
<html:text name="accScreen" property="accountsScreen[${index}].bic" size="18" maxlength="11"
onkeyup="suivant(this,'partPays',11,'accScreen')"
styleClass="inscCB_SaisieTexte" />
This time the console error was :
java.lang.IllegalArgumentException: Invalid indexed property
'accountsScreen[${index}]'
Do you have a solution to one of these problems or another way to do this?
Aleksandr M help me to find the lead to the final solution. Indeed I needed to use simple quotes instead of double ones. Then there was an access problem to the accountsScreen property because it wasn't and indexed one. Finally I needed 2 different names for getter, even if the arguments wasn't the same. So I add the indexed prefix to the indexed getter.
In the jsp :
<html:text name="BankAccountsActionForm" property='<%="indexedAccountsScreen["+index+ "].partAccountNumber2"%>'
in the java form, I needed an indexed property to access to an element of accountsScreen ArrayList. So I had the getter to access to an element by Index :
public void setAccountsScreen(int index, AccountScreenBean accScreen) {
this.accountsScreen.add(index, accScreen);
}
public AccountScreenBean getIndexedAccountsScreen(int index) {
return accountsScreen.get(index);
}
Thanks every one.
I think the problem with your first "workaround" is the <% %> magic, because of the quotes. And it also seems unnecessary, as you should already have the indexed element in accScreen.
I would try
<logic:iterate name="BankAccountsActionForm" property="accountsScreen" id="accScreen" indexId="index">
<html:text name="accScreen" property="bic" etc="..." />
</logic:iterate>
instead.

Struts 1: form can read bean value but on submit does not write in it

Hello dear folks of stack overflow.
I encountered a problem recently in my Struts application.
I have a jsp which displays some bean value correctly (i paste only relevant part of the code, i simplified to the extreme):
<table>
<logic:iterate name="bean" property="list1" id="listItem">
<tr>
<td>
<html:checkbox name="listItem" property="selected">
</html:checkbox>
</td>
</logic:iterate>
</table>
My bean has a list1 property with its getter and setter
private List<RandomObject> list1;
public List getList1() {
return list1;
}
public void setList1(List list1) {
this.list1=list1;
}
and my sub-bean has a selected property:
private boolean selected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
Now, when enter this jsp, the values i get are correct, ie: if my mapped object in DB was at selected=true, the checkbox is checked. What i dont get is how do i save the changes i make in this form. When i submit, all the changes are lost. This is pure struts related, because in debug when i enter the StrutsAction linked to submit, the ActionForm i get has already lost all interesting values. Also i feel like it has to do with the fact that the value i want to retain isn't directly stored on the main bean, but rather is a property of a sub-bean, because on the same page there are a lot of other properties directly on the main bean that i have no trouble saving.
What did i miss ?
It's probably declaring incorrect name attributes in the rendered HTML. If you look at the generated source code for your page it probably looks like this:
<input type="checkbox" name="selected" ...>
which would look for a selected field in your form bean class.
If you're using a collection that's a field in your form bean you want to be using the <nested:form>, <nested:iterate> and <nested:checkbox Struts tags, not the <html:form>,and` ones. So it would then look like this:
<nested:form action="foo">
...
<nested:iterate property="list1" id="listItem">
<tr>
<td>
<nested:checkbox property="selected">
</nested:checkbox>
</td>
</tr>
</nested:iterate>
...
</nested:form>
Note that I've removed the name attributes from the Struts tags, because they're not needed (in my experience they actually cause all kinds of problems - if you use <nested:*> tags don't use a name attribute). In the case of the <nested:iterate> the level of nesting is the form bean itself, so it knows that it needs to look for the list1 property in the form bean.
That tag creates its own nesting level, so the <nested:checkbox knows that it needs to look for the selected property of the current element in the iteration.
The rendered HTML will look something like this (for the first element):
<input type="checkbox" name="list1[0].selected" ...>
which means the selected field of the first element (index 0) in the collection referenced by the list1 field of your form bean.
And, of course, you'll need to make sure that you're using a session-scoped form bean, rather than a request-scoped one.
in my case i had to put the name attribute into the iterate otherwise it gives me error(cannot find property into any bean...)
I resolved deleting the attribute name into the checkbox: it seems it create a new object at page scope that is not connected with the form.
This is my code:
<nested:iterate id="apertura" type="it.puglia.innova.view.actionform.AperturaForm" indexId="index" name="strutturaRuraleForm" property="listAperturaForm">
<nested:checkbox styleId="checkbox_${index}" property="flagContinuato" onchange="changeOrarioContinuato(${index})"/>
that's it :-) didn't need to change html:form to nested too.
Bye

Spring - Displaying a Label that reflects and updates a #ModelAttribute field

I have a #ModelAttribute account which has a field named title. I need to display this field in my JSP, and also bind it in the next call cycle. If I do this;
Title: ${editAccountForm.account.title} <br/>
It only displays the value. When someone submits the Form in the JSP, account is empty again. How do I get the label to reflect the value, just like a form:input tag?
I tried this:
<form:label path="account.issuer">some text</form:label> <br/>
but it dint work. Please help.
You can put a
<input type="hidden" name="account.title" value="${editAccountForm.account.title}" />
The name attribute must be the same as the one generated by a spring form:input.
but that is the same to displaying it normally ${editAccountForm.account.title}. After that, populate the value in a hidden field. that will update the value in the model

Retrieving inputvalue from JSF-repeat

I have recently come across a question involving JSF and Javabeans.
To get a value from an input you need a declared property with getter and setter in the bean. Now I am trying to generate a list of entities to edit a value (in this code example called number), but how can i retrieve these values (as the ID of h:inputText is empty at this point)?
I have tried to name this ID "name_#{bean.id}", but there is no way to set this value in the bean.
Any help would be appreciated!
index.xhtml
<ui:repeat var="entity" value="#{bean.getEntities()}">
<p>
Value:
<h:inputText id="" value="#{entity.number}" />
</p>
</ui:repeat>
Bean.java
public List<Entity> getEntities() {
return entities;
}
You don't need the client id of the inputText to get the values to the server. As stated in Daniel's comment changes are saved if you submit the surrounding form.
You don't even need to set the id parameter. JSF does it for you.
But your value attribute is not correct. It needs to be
value="#{bean.entities}"

How to set the default value for textarea in struts2

I would like to know how the default value can be set in textarea , the scenario would be after a page refresh , or fail of validation bedore save operation.
<s:textarea name="desc" value="" theme="xhtml" required="true" cssClass="text textarea small"/>
value="<%= "Default" %>" , this code is not working out.
Perhaps I'm missing something, but IMO this is the same as for any field: the value attribute of a Struts2 tag looks up the respective property in your stack. In the typical scenario, when you type, say, <s:textarea value="comment" ..> Struts2 will use the MyAction.getComment() and MyAction.setComment() to read/write the textarea value. Then, you just have to assign a default value for the attribute in your action -which, BTW, is conceptually the right way.
public class MyAction extends ActionSupport {
public final static String DEFAULT_COMMENT = "Default value...";
private String comment = DEFAULT_COMMENT;
//... getters setters follow
}
Well one of the way would be use Javascript function to load Default value on pageload event... Though I am not sure why your tag is not working
Try to initialize the object linked to the field in the previous action method.
In your java File:
X object = new X() ;
object.setDesc("");
request.setAttribute("theFormObject",object);
In your JSP:
<s:textarea name="theFormObject" property="desc" ... />

Categories