Extra character(,) is added while using Textbox in spring - spring

I am new to JSP and Spring. I want to insert a userID(U0005) using textbox in spring form but it is storing(,U0005). From where the "," is inserted?
The code I have written is:
/* In Register.jsp: */
<c:url var="addAction" value="/libUsr/add"></c:url>
<form:form action="${addAction}" commandName="libUsr">
<table>
<tr>
<td>
<form:label path="id">
<spring:message text="ID" />
</form:label>
</td>
<td><form:input path="id" required="true" /></td>
</tr>
</table>
</form:form>
/*
In UserController.java:
*/
#RequestMapping(value= "/libUsr/add", method = RequestMethod.POST)
public String addLibUsr(#ModelAttribute("libUsr") LibUsr libUsr){
libUsrDAO.saveOrUpdate(libUsr);
return "redirect:/register";
}
/*
In DAOImpl:
Saving the data through DAOs
*/
#Transactional
public void saveOrUpdate(LibUsr libusr) {
sessionFactory.getCurrentSession().saveOrUpdate(libusr);
}

Your problem is with this tag:
<form:label path="id">
As you are adding the attribute path to one label, which wont store any value, spring try to get the value and returns empty,yourID.
Change the path attribute of your label to:
<form:label for="id">
<spring:message text="ID" />
</form:label>
If you want to show the value of the id into that label use:
<label>${yourObject.id}</label>
or
<label th:value="${yourObject.id}"></label>
Path attribute is just for input, select, checkbox...not static values as label, span...

Related

Table row data submit in struts 1.2

I have a table with each rows contain checkbox (Like gmail inbox). I want to select few checkboxes and submit data in one go to action class.
Please let me know how can i submit form using checkboxes.
First of all you must ensure that your form bean and your form action class are already working otherwise even if you prepare your jsp file you will not be able to check its correctness (Struts 1.2 drawback)
Here goes your jsp code
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html:html>
<html:form action="/formAction" method="GET">
<table>
<tr>
<td>Data 1</td>
<td>
<html:checkbox property="chk" value="value1"></html:checkbox>dln</td>
</tr>
<tr>
<td>Data 2</td>
<td>
<html:checkbox property="chk" value="value2"></html:checkbox>dln</td>
</tr>
<tr>
<td>Data 3</td>
<td>
<html:checkbox property="chk" value="value3"></html:checkbox>dln</td>
</tr>
<tr>
<td>Data 4</td>
<td>
<html:checkbox property="chk" value="value4"></html:checkbox>dln</td>
</tr>
<tr>
<td>
<html:submit value="Register"></html:submit>
</td>
</tr>
</table>
</html:form>
Form Bean
public class MyFormBean extends ActionForm {
private String chk;
[...]//other fields
public String getChk() {
return chk;
}
public void setChkString chk) {
this.chk= chk;
}
[....] // other getters and setters }
Action class's execute method
[...]
MyFormBean myForm = (MyFormBean) form;
String chkVal = myForm.getChk();
[...]
Form Bean Entry in struts-config.xml
[...]
<form-bean name="myBean" type="pkg.MyFormBean"/>
[...]
Form Action Entry in struts-config.xml
[...]
<action path="/formAction" type="pkg.formAction" name="myBean" scope="request">
[...]

How to get data from two classes that are many-to-many, in the same edit form?

I am working with two classes that have a relationship many-to-many. I insert data in both tables, the relationship is usually registered in JoinTable. My question is, how to display the data from both tables in the same form?
Here is my controller:
#Controller
public class RecipeController {
#Autowired
private ReceitaService receitaService;
#RequestMapping(value = "/novaReceita.do", method = RequestMethod.POST)
public String createRecipes(#ModelAttribute("Receita") Receita receita, BindingResult resultReceita,
#ModelAttribute("Tag") Tag tag, BindingResult resultTag, #RequestParam String action, Map<String, Object> map) {
receita.getTag().add(tag);
receitaService.addReceita(receita);
map.put("receita", receita);
map.put("receitaList", receitaService.getAllReceita());
return "listRecipes";
}
Here is where I am having problems because I can only show data for a single table.
public String editForm(#PathVariable("id") int id, ModelMap map) {
map.addAttribute("receita", receitaService.getReceita(id));
return "updateRecipes";
}
Finally, the JSP page to display the data entered:
<c:url var="url" value="/receita/${receita.id}"/>
<form:form action="${url}" method="GET" commandName="receita">
<table width=80% >
<tr>
<td><strong>ID </strong></td>
<td><form:input path="id" disabled="true" class="input-small"/></td>
</tr>
<tr>
<td><strong>Title </strong></td>
<td><form:input path="titulo" class="input-xlarge"/></td>
</tr>
<tr>
<td valign=top><strong>Desc probl</strong></td>
<td><form:textarea path="desc_prob" class="input-xlarge" rows="3" /></td>
</tr>
<tr>
<td valign=top><strong>Desc soluc</strong></td>
<td><form:textarea path="desc_soluc" class="input-xlarge" rows="6" /></td>
</tr>
<tr>
<td><strong>Tag</strong></td>
<td> <form:input path="tag" disabled="true" class="input-small"/></td>
</tr>
</table>
</form:form>
What is missing to be able to show the data from both tables?
Thank's.
For single column/property you can use <form:input/>For Many to One you can use <form:select/>For One to Many you can use <form:select multiple='true'>In your case, you have to select a single ID first for the one side of the relationship which is receita and then display all the tags in that receita, so i think it counts as One to Many at that point. So in your case, try to use this tag:<form:select path="tag" multiple="true"/>

Spring Framework 3.0.5 - spring form data binding - comma delimiter

I have a group editation form:
<form:form method="POST" action="" commandName="group">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" disabled="disabled" /></td>
<td><form:errors path="name" class="error" /></td>
</tr>
<tr>
<td><form:label path="description">Description</form:label></td>
<td><form:input path="description" /></td>
<td><form:errors path="description" class="error" /></td>
</tr>
<tr>
<td><form:label path="gidNumber">GID</form:label></td>
<td><form:input path="gidNumber" /></td>
<td><form:errors path="gidNumber" class="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
<form:hidden path="members" />
</form:form>
This form is binding object of class Group. Problem is with binding the member attribute to the hidden field. The deffinition of the memeber attribute in Group class is below. The group class is also being used by Spring LDAP ODM manager, (thus the annotations there).
#Attribute(name="member", syntax="1.3.6.1.4.1.1466.115.121.1.12")
private List<String> members = new ArrayList<String>();
The content of this array list is usually something like:
1: uid=user1,ou=users,dc=example,dc=com
2: uid=lilcuttie2,ou=users,dc=example,dc=com
3: uid=naprostejmagor,ou=users,dc=example,dc=com
4: uid=crazyuser,ou=users,dc=brazzers,dc=com
...
When the content is binded to hidden field it is concatenated to the comma delimited and then when it is beeing splited again to the arraylist entries, of course each entry is broken to four separate entries.
I've tried to use custom converter for ArrayList, but it messed up some otheer stuf like loading resource messages for locales..
Do you have any suggestions how to deal with it in any non-ugly way - so that I dont have to wrap my DAO classes nor modify them.
Thanks,
/Jakub
Have a small wrapper type over your members element, say:
public class MembersWrapper{
private List<String> members;
.....
}
Now, you can provide a custom converter to convert this MembersWrapper instance to a String and back - the conversion can be to say colon delimited across each element of the list, something that you can parse back later:
public class MemberWrapperToStringConverter implements Converter<MemberWrapper, String>{
...
}
public class StringToMemberWrapperConverter implements Converter<String, MemberWrapper>{
....
}
and register these converters with Spring MVC:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="..MemberWrapperToStringConverter "/>
<bean class="..StringToMemberWrapperConverter "/>
</set>
</property>
</bean>
An alternative will be providing a custom property editor through #InitBinder annotation:
#InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Wrapper.class, new PropertyEditorSupport(){
#Override public String getAsText() {
//....
}
#Override public void setAsText(String text) throws IllegalArgumentException {
//....
}
});
}
The logic in these converters should be straightforward, convert the list to some string form, that you can parse back to list easily, say between list elements just add a colon or a semi-colon.
As I am not clear about your problem I have made some assumptions and providing this answer on basis of that.
Well you can use to loop your entries in the members list and for each entry in the list you can create a separate hidden field for that.
Hope this helps you.
Cheers.
You don't need to put the formtag for hidden field now when you are using just use the simple input tag for hidden fields but in the name property you need to specify as following.
<input type="hidden" name="members${status.index}"/>
in this way it will bind the same value back to the controller.
I think this should help you.
Cheers.

Passing object from JSP to spring controller

I have the below table in a .JSP page:
<form:form method="post" action="update.dtt" id="contactForms" modelAttribute="contactForms" >
<c:forEach items="${pList}" var="cf">
<tr>
<td align="center"><c:out value="${cf.fname}" /></td>
<td align="center"><c:out value="${cf.lname}" /></td>
<td align="center"><c:out value="${cf.cprovider}" /></td>
<td align="center"><c:out value="${cf.id}" /></td>
<td align="center"><c:out value="${cf.phone}" /></td>
<td><input type="submit" value="Update Contact"/></td>
</tr>
</c:forEach>
</form:form>
I'm iterating over the list (this is list of objects) and adding an Update Contact button for each record in the list. How can I pass on the particular instance (object) to the controller when the Update button is clicked?
The controller I have is as below. However I'm getting null.
#RequestMapping(value = "/user/update.dtt", method = RequestMethod.POST)
public String updateView(#ModelAttribute("contactForms") Banks bank, HttpServletRequest request, HttpServletResponse response, Model model) {
System.out.println("*First Name*" + bank.getFname());
//......
return "detailBank"; //name of jsp file
}
You'll need to have a hidden form field for each information you want to send to the server.
Since you have one button for each row, you should also have one form for each row. So the <form:form> and </form:form> lines should be inside the <c:forEach>, and not outside.
You need reference the instance by position, accessing to the element of the list by position.
list[i].name
list[i].surname
where list is the element in your pojo.

Why do I get IllegalArgumentException Cannot convert value of type String to required type Product, in Spring?

I receive the exception
Failed to convert property value of
type [java.lang.String] to required
type [beans.Product] for property
product; nested exception is
java.lang.IllegalArgumentException:
Cannot convert value of type
[java.lang.String] to required type
[beans.Product] for property product:
no matching editors or conversion
strategy found
in the Errors errors object even before my DetailProductValidator starts validating through the validate method.
I don't understand why Spring does that. I don't have any input field that is mapped directly to the product property/object. I just use the product object's properties in the jsp. For example, I use:
<form:options items="${dpBackObj.product.colorMap}"/>
<!-- or -->
${dpBackObj.product.priceInDollars}
but I never use:
<form:input path="product"/>
Can anyone please explain why this happens? And maybe inform me of a simple solution?
The bean configuration for the controller is:
<!-- DETAIL PRODUCT FORM CONTROLLER -->
<bean id="productDetailFormController" name="/detail.htm /addToCart.htm"
class="detailProduct.DetailProductFormController">
<property name="sessionForm" value="true" />
<property name="commandName" value="dpBackObj" />
<property name="commandClass" value="detailProduct.DetailProductBackingObject" />
<property name="validator">
<bean class="detailProduct.DetailProductValidator" />
</property>
<property name="formView" value="detail" />
<property name="successView" value="redirect:/viewCart.htm" />
<property name="cartService" ref="cartServiceImpl"/>
</bean>
The backing object for the DetailProductFormController is:
public class DetailProductBackingObject {
private String quantityOverflowError;
private Product product;
private int quantity;
private ShoppingCart shoppingCart;
private long sizeId;
private long colorId;
public DetailProductBackingObject() {
this.product = new Product();
this.sizeId = -1;
this.colorId = -1;
}
//getters and setters
}
If you need some other info, I will provide. I am using Spring 2.5.5.
Kind Regards,
Despot
EDIT1 (due to request from axtavt):
<form:form method="post" commandName="dpBackObj">
<table width="730" border="0" cellspacing="0" cellpadding="0">
<c:if test="${!empty dpBackObj.quantityOverflowError}">
<tr>
<td>
<c:out value="${dpBackObj.quantityOverflowError}"/>
</td>
</tr>
</c:if>
<spring:bind path="dpBackObj.*">
<c:if test="${not empty status.errorMessages}">
<div class="val-summary text-error" id="errorDivId">
<div style="" class="val-summary text-error" id="errorDivId">
<fmt:message key="detail.error.header"/>
<ul>
<c:forEach items="${status.errorMessages}" var="error">
<li><c:out value="${error}"/></li>
</c:forEach>
</ul>
</div>
</div>
</c:if>
</spring:bind>
<tr>
<td width="310" align="left" valign="top">
<img src="${imagesPath}/${dpBackObj.product.largeImageUrl}" alt="${dpBackObj.product.description}" />
</td>
<td width="420" align="left" valign="top">
<div id="tls_detPName">
<c:out value="${dpBackObj.product.name}"></c:out>
</div>
<div >
<strong class="numeric">${dpBackObj.product.priceInDollars}</strong>
</div>
<div id="tls_detPDescLong">
${dpBackObj.product.largeDescription}
<br />
</div>
<div >
<table cellpadding="2" border="0">
<tr>
<td align="right">
<label for="p_sizes" class="label"><fmt:message key="viewCart.Size"/></label>
</td>
<td>
<form:select path="sizeId" >
<form:option value="-1" label="x"/>
<form:options items="${dpBackObj.product.sizeMap}"/>
</form:select>
</td>
</tr>
<tr>
<td align="right">
<label for="p_colors" class="label"><fmt:message key="viewCart.Color"/></label>
</td>
<td>
<form:select path="colorId" >
<form:option value="-1" label="y"/>
<form:options items="${dpBackObj.product.colorMap}"/>
</form:select>
</td>
</tr>
</table>
</div>
<div id="tls_addToCart">
<div >
<label for="quantityId" class="label"><fmt:message key="viewCart.Quantity"/>:</label>
<form:input path="quantity" onkeypress="return checkForNumber(this, event)" maxlength="10" size="3" id="quantityId" cssClass="textbox-center"/>
<input type="image" name="addToCartButtonName" src="${imagesPath}/addToCartBtn.jpg" />
</div>
</div>
</td>
</tr>
</table>
</form:form>
EDIT2 (due to JacobM's request):
This is my Validator:
public class DetailProductValidator implements Validator {
public boolean supports(Class clazz) {
return DetailProductBackingObject.class.equals(clazz);
}
public void validate(Object obj, Errors errors) {
DetailProductBackingObject detailProductBackingObject = (DetailProductBackingObject) obj;
if (detailProductBackingObject.getSizeId() == -1) {
errors.rejectValue("sizeId", "error.detail.jsp.choose.size", null, "Input size.");
}
}
}
When I reach the line DetailProductBackingObject detailProductBackingObject = I already have the error.
The converting of the request parameters to the backing object properties happens in http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html . This is what Spring says about the conversion:
Populating using request parameters
and PropertyEditors: Upon receiving a
request, any BaseCommandController
will attempt to fill the command
object using the request parameters.
This is done using the typical and
well-known JavaBeans property
notation. When a request parameter
named 'firstName' exists, the
framework will attempt to call
setFirstName([value]) passing the
value of the parameter. Nested
properties are of course supported.
For instance a parameter named
'address.city' will result in a
getAddress().setCity([value]) call on
the command class.
It's important to realise that you are
not limited to String arguments in
your JavaBeans. Using the
PropertyEditor-notion as supplied by
the java.beans package, you will be
able to transform Strings to Objects
and the other way around. For instance
setLocale(Locale loc) is perfectly
possible for a request parameter named
locale having a value of en, as long
as you register the appropriate
PropertyEditor in the Controller (see
initBinder() for more information on
that matter.
Validators: After the controller has
successfully populated the command
object with parameters from the
request, it will use any configured
validators to validate the object.
Validation results will be put in a
Errors object which can be used in a
View to render any input problems.
Since I can't see anything wrong with the form, the only possible reason I can imagine is that you have a parameter named product in the URL of your form page.
If so, you can change your URLs or use DataBinder.setDisallowedFields() to disable the attempt to bind that parameter.

Resources