I have a form object
public class TestForm {
private long id;
private List<Date> dates;
// getters and setters for the above
}
And my controller has the following..
#RequestMapping(value = "/assignDummy", method = RequestMethod.POST)
public #ResponseBody
String assignDates(TestForm frm) {
System.out.println("frm:"+frm.getId()+", date:"+frm.getDates());
return "Everything is fine";
}
My form..
<form name="abc" method="post" action="assignDummy.htm">
<input type="text" name="id" value="1000">
<input type="text" name="dates[0]" value="4500000">
<input type="submit">
</form>
I get the following error..
Failed to convert property value of type 'java.lang.String' to
required type 'java.util.Date' for property 'dates[0]'; nested
exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type java.lang.String to type java.util.Date for value
'4500000'; nested exception is java.lang.IllegalArgumentException"
Any help is appreciated.
Thanks in advance
You are trying to put a String into Date without converting it, so it crashes.
You have to use a custom property editor in order to convert the input String into a Date.
Try to add in your controller
#InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true);
binder.registerCustomEditor(Date.class, editor);
}
Related
I have a jsp form with an input box, a domain object with get/set methods, and a controller. When I submit the form I get null values in the controller. The "set" method is never being called in the domain object when i submit the form but the object itself is being called.
Order.jsp
<portlet:defineObjects />
<portlet:actionURL portletMode="view" var="createNewOrderURL">
<portlet:param name="action" value="createNewOrder" />
</portlet:actionURL>
<div>
<form:form name="form" method="post" commandName="refOrder" action="${createNewOrderURL}" id="createOrder">
TestName : ${refOrder.name}<br/> <!-- here I get the correct value to display -->
<form:input path="referenceNum" />
<input type="submit" value="Submit" />
</form:form>
</div>
Order.java
public class Order {
private String name = "Name1";
private String referenceNum;
public Order(){
System.out.println("Inside Order.java");
System.out.println(getReferenceNum());
}
public Order(String name, String referenceNum) {
this.name = name;
this.referenceNum = referenceNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReferenceNum() {
return referenceNum;
}
public void setReferenceNum(String referenceNum) {
this.referenceNum = referenceNum;
}
SalesOrderController.java
#RenderMapping
public String defaultRender(RenderRequest request, RenderResponse response, Model model) throws SQLException, NamingException{
model.addAttribute("refOrder",new Order());
return "SalesOrderEntry";
}
#ActionMapping(params={"action=createNewOrder"})
public void addNewOrder(ActionRequest request, ActionResponse response, #ModelAttribute("refOrder") Order order)throws NamingException, SQLException{
System.out.println("Inside addNewOrder method");
System.out.println("New Order is --> "+order.toString());
System.out.println("RefNum - "+order.getReferenceNum());
System.out.println("request.getParameter is "+request.getParameter("referenceNum"));
}
I get null for all the print statements in the controller. Have been trying to fix this for two days now and I can't find what's wrong. Would really appreciate if someone can help me get this to work.
Do you have the following in your src/main/webapp/WEB-INF/liferay-portlet.xml descriptor?
<requires-namespaced-parameters>false</requires-namespaced-parameters>
Also, you might want to take a look at the PortletMVC4Spring project, which is the successor to Spring Portlet MVC. The GitHub project contains archetypes that work in Apache Pluto and Liferay Portal. The requires-namespaced-parameters config option is conveniently set in the archetypes.
I am trying to bing List of Lists in spring MVC
is it possible?
Do we need to write any custom binding methods?
Please help me.
JSTL Code :
<input id="labelDTOS0.labelItemDTOS0.newValue"
name="labelDTOS[0].labelItemDTOS[0].newValue" type="text" value=""/>
DTOs:
public class LabelDTO {
public long id;
public String name;
public List<LabelItemDTO> labelItemDTOS;
}
public class LabelItemDTO {
public String value;
public String placeHolder;
public String newValue;
}
Exception :
Invalid property 'labelDTOS[0].labelItemDTOS[0]' of bean class
[com.goitdev.datarender.command.domain.CreateTemplateCommand]: Illegal
attempt to get property 'labelItemDTOS' threw exception; nested exception
is org.springframework.beans.NullValueInNestedPathException: Invalid
property 'labelDTOS[0].labelItemDTOS' of bean class
[com.goitdev.datarender.command.domain.CreateTemplateCommand]: Could not
instantiate property type [com.goitdev.datarender.dto.domain.LabelItemDTO]
to auto-grow nested property path; nested exception is
java.lang.NoSuchMethodException:
com.goitdev.datarender.dto.domain.LabelItemDTO.<init>()
If you try to perform GET mapping and print out value of your DTO, you can try this:
In your controller method:
...
model.addAttribute("labelDTO", new LabelDTO());
...
In JSP
...
<c:forEach items="${labelDTO.labelItemDTOS}" var="labelItemDTO">
<td>${labelItemDTO.value}<td>
<td>${labelItemDTO.placeHolder}<td>
<td>${labelItemDTO.newValue}<td>
</c:forEach>
...
Or for POST see this question possible the same issue.
can anyone help me. I cant understand, why #RequestParameter or request.getParameter() not working.
My controller:
#Controller
public class CheatController extends WebMvcConfigurerAdapter {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(#RequestParam("gg") String gg, Model model) {
return "hello";
}
}
And my view:
<html>
<body>
<form action="#" th:action="#{/hello}" method="get">
<input type="text" id="gg" name="gg" placeholder="Your data"/>
<input type="submit"/>
</form>
<span th:if="${gg != null}" th:text="${gg}">Static summary</span>
</body>
</html>
Seems like you have an error in the #RequestParam
Try replacing this line public String hello(#RequestParam("gg") String gg, Model model) by:
public String hello(#RequestParam(required = false, defaultValue = "") String gg, Model model)
What we're setting in the line above is that gg is not required and if your param gg comes empty or null the defaultValue will be "". You can remove this options but is a good way to test that the Controller is working, and if you know for sure that you're going to receive always a gg param you can remove it.
You should be using POST instead of GET on your form:
<form action="#" th:action="#{/hello}" method="get">
You can also simplify your controller code to:
#Controller
public class CheatController {
#GetMapping("/hello")
public String hello(#RequestParam("gg") String gg,
Model model) {
...
return "hello";
}
}
I cant understand how it influenced on getting and sending params, but it helped me(i commented that peace of code and it started working). Can anyone explain why it happened?
#Configuration
public class DefaultView extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers( ViewControllerRegistry registry ) {
//registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/all").setViewName("all");
registry.setOrder( Ordered.HIGHEST_PRECEDENCE );
super.addViewControllers( registry );
}
}
How to bind a map property of the model and send the selected value back to the controller?
i am able to populate the drop down but on submitting the form getting error 400 - bad request.
MyModel.java:
public class MyModel{
private Map<MySubModel, String> subModel = new HashMap<MySubModel, String>();
private SubModel subModelSearched;
}
SubModel.java:
public class SubModel{
public String id;
public String name;
}
JSP:
<form:form action="/mySearch" modelAttribute="myModel" method="post">
<form:select path="subModelSearched">
<form:options items="${myModel.subModel}/>
</form:select>
.....
</form:form>
I want to use the form:options provided by Spring to provide choices.
My JSP implemented with Spring 2.5 is
<td><form:select path="billingType">
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="billingType" itemLabel="billingTypeName" />
</form:select>
</td>
My AccountCommand.java is
private int billingType;
private String billingTypeName;
public int getBillingType() {
return billingType;
}
public void setBillingType(int billingType) {
this.billingType = billingType;
}
private Map<String, Integer> billingTypeChoice = new HashMap<String, Integer>() {
{
put("Monthly", 1);
put("Block", 2);
put("Per Use", 3);
}
};
public Map<String, Integer> getbillingTypeChoice() {
return billingTypeChoice;
}
public void setbillingTypeChoice(Map<String, Integer> billingTypeChoice) {
this.billingTypeChoice = billingTypeChoice;
}
public String getBillingTypeName() {
return billingTypeName;
}
public void setBillingTypeName(String billingTypeName) {
this.billingTypeName = billingTypeName;
}
My Eclipse console is:
15:55:23,140 ERROR org.springframework.web.servlet.tags.form.OptionsTag:84 - Invalid property 'billingType' of bean class [java.lang.String]: Bean property 'billingType' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.NotReadablePropertyException: Invalid property 'billingType' of bean class [java.lang.String]: Bean property 'billingType' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:540)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:532)
at org.springframework.web.servlet.tags.form.OptionWriter.renderFromMap(OptionWriter.java:164)
...
When you do:
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="billingType" itemLabel="billingTypeName" />
you are saying go to addAccountCommand.billingTypeChoice which is a Map and do a getBillingType() for the value and a getBillingTypeName() for the label. As these methods are not defined on the map you get the error. You should use getKey() and getValue() on the map.As you have it defined now it should be:
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="key" itemLabel="value" />
because you have defined the Map in a very strange way. It's usually Map because I suppose the key is the Integer and the value the String.
Hope it helps.
I just tried it on Spring 2.5. Just code below should work.
<form:select path="billingType">
<form:options items="${addAccountCommand.billingTypeChoice}" />
</form:select>