I have the enum set
public enum MyEnum {
A("AND"), //
I("INTER");
}
I have the spring form:select in my jsp page which populates the AND and INTER As I want , but while submitting the form it throws the exception , can't map String AND to enum type Status.
My Controller :
model.addAttribute("list",MyEnum.values);
**
But it works when I modify my enum and add one more value like ,
OR("ÖR") , now If I select OR from UI , it gets saved properly in the
database.But it won't work for A or I.
**
Note : Down voting to this question simply means that you have not read question carefully.I have already searched on stackoverflow. :)
thanks all for answering but I found the way , how to deal with this.
<form:options items="${mList}" itemLabel="name" />
Use spring form options in following way don't specify the itemValue field , spring will do it for you. :)
I suppose when spring converts your enum to select option it uses toString method on enum and you overrided it so it returns "AND" for A and "INTER" for I. When it converts selected option back to enum it probably uses valueOf but I'm not sure.
In any case you should try to implemend your own formatter for this field that will convert enum to String and back to enum correctly. Check this article: http://bthurley.wordpress.com/2012/10/25/enum-translations-in-spring-mvc/
Related
i got a simple programm that begins on an input form where the user fills in 3 numbers. The form action refers to a controller servlet where i store the data in the Bean class with the setter methods I have defined.
number.setNumber1(Double.parseDouble(request.getParameter("number1")));
Till now I stored the Number object in the request with
request.setAttribute("numbers", number);
and forwarded it to the output page where i could get it with ${numbers.biggestNumber ( getter-Method that simply determines the biggest Number) }. A tutorial i am using says I could also get the data directly from the Bean by using this piece of code:
<jsp:useBean id="num" scope="session" class="model.Numbers"/>
<c:out value="${num.biggestNumber}/>
but somehow the Bean uses another object of the Numbers-class. I see the advantage of this technique, because I dont have to put the Numbers object into the request. Can someone tell me how I can use the same Numbers object I stored the data before?
I already read that I shouldnt use "jsp:setProperty..." to store the data on the input page, but if i cant get the information i wrote manually to the Bean, I have to ask myself why I should use the JSP JavaBeans annotation at all.
I used the search function but could not find an answer suitable to my question, or maybe I am just not experienced enough to get them in a more advanced context... Any help would be welcome
if you are using this one,
request.setAttribute("numbers", number);
And using requestDispacher redirecting then at target page you can do likewise,
into JSP file :
<jsp:useBean id="numbers" scope="request" class="model.Numbers"/>
<c:out value="${numbers.biggestNumber}/>
Here,
you did with wrong scope=session, means you are putting value into 'request' scope and try to pull it from session is wrong.
also maintain name of attribute 'same' while putting/getting from scope. here, name="numbers" maintain while putting/getting
Okay my mistake was that I thought JavaBeans-Jsp-Tags would save time and code. Indeed I had to create a HttpSession-Object that stores the ID of the used JavaBean
HttpSession sess = request.getSession(true);
sess.setAttribute("number", number);
Now the JavaBean-Tag in my Output.jsp knows which object to use (the one created in the Input.jsp). As far as I do understand now, the only advantage of the JavaBean-Jsp-Tag above normal Parameters added to the Request is that I can use the Bean-Class in the whole Session and am not dependend on the Request-Object.
I´m developing a plugin for jira which contains a custom field type.
The field is a select and has to be filled with options. I supply these options via the Jira method getVelocityParameters().
#Override
public Map<String, Object> getVelocityParameters(Issue issue,
CustomField field, FieldLayoutItem fieldLayoutItem) {
Map<String, Object> map = super.getVelocityParameters(issue, field, fieldLayoutItem);
map.put("customOptions", getCustomOptions());
return map;
}
getCustomOptions() returns a Hashtable with the Options i need.
To access and display these options i used a #foreach loop in the template:
#foreach($customOption in $customOptions)
<option id="$customOption.Id" value="$customOption.Value">
$customOption.Label
</option>
#end
Instead of showing the returned Objects it simply just display the text itself, only the "$customOption.Id" is displayed correctly.
And writing only "$customOption" shows the whole reference to the object.
So i CAN access the object and its id but not the other properties.
Id is an int, while label and value are Strings.
i searched for solutions and tried different things to solve this problem, e.g.:
$!customOption.Label, ${!customOption.Label}, ${customOption.Label}, $customOption.getLabel()
I can´t find the problem here, because the id is working properly.
Sry for the broken english.
Because you use Map. So try the following:
#foreach($customOption in $customOptions)
#if ($customOption)
#foreach ($co in $customOption.keySet())
$customOption[$co]
#end
#end
#end
Velocity will display the source if there is no value for your issue.
E.g. if you want to obtain a customfield value, you should check the value and if its not set you can either load the default value or just ignore it.
I think you should look into the fact that Velocity will only display a field value if its class has a public get method for that field.
Say customOption is an object of class X, then class X must have a public get() method that returns the label.
It does not matter if the label field is a public field of class X, public get() method is necessary.
You can have a look at this for reference:
Part1:
I am trying to search a way to capture and format user input on an automated way. I have a lot of fields and formatting everything with following method becomes bulky:
<s:param name="value" value="thenumber == null ? '' : getText('{0,number,#,##0.00}',{thenumber})" />
Is there an efficient way to automate this?
Part2:
I want to capture and be able to process invalid user input before it gets passed to the correct setter, in this case setTheNumber(Double theNumber). Preferrably with tags from the page itself. Because inserting '10.00aaab' will throw me an error.
Creating a temporary String field for every number I need to set is an invalid option as I would need to create around ~170 setters for this with exception handling and string parsing.
Short: I want to hook own code between my Http post message and my java class setter.
Previously I used #TypeConversion annotation on the setters and getters but I can't use this anymore as my java class with all values is in another project that can't have dependencies to xwork2 packages.
Thanks in advance,
I'm using this tag in a custom view, to generate a CRUD form for my object:
<div class="configForm">
#{crud.form /}
</div>
This generate a CRUD form inside my own view. Is there a way to customize the generated form by removing a few fields that I don't want to be editable?
I spotted this line inside form.html:
#{list items:_fields ?: currentType.fields*.name, as:'fieldName'}
If I'm reading this right, then there is a _fields parameter that might let me opt-in to fields (I prefer opt-out, but I'll take opt-in). How do I use this _fields parameter?
You can filter fields like:
#{crud.form fields:['name', 'email', 'password']/}
which will show only the fields name, email and password
Regards
Ronald
I wrote a filtered version of CRUD's ObjectType. The controller can select which fields to filter, like this:
public static void show(long id) {
MyModel object = MyModel.findById(id);
CRUD.ObjectType type = new FilteredObjectType(MyModel.class,
"filteredField1",
"filteredField2");
render(type, object);
}
You can follow Play documentation. Then you can easily write your own ExtCRUD class which ignores all fields which are declared in a static variable of your Controller. How ever FilteredObjectType is for special cases the easier solution.
I have a spring bound form (modelAttribute) which displays the user information.
The user's telephone number is displayed in a formatted manner but a requirement is that the number is saved to the database without any signs.
So in the getter method of my user object I format the telephone number according to the rules and in the setter I put the code to remove the special signs.
The formatting part works fine, but setter part where I remove the signs does not seem to occur.
In my constructor I also did:
setTelephoneNumber(TelephoneNumber);
So the constructor also invokes the setter.
I'm using Spring 3.0.4 and Spring-mvc.
Any input on this issue and how to resolve it would be appreciated.
edit:
controller section:
model.addAttribute("user", user);
JSP (shortened it a bit but this is the gist. submitUrl is due to a portal environment:
<form:form action="${submitUrl}" modelAttribute="user">
<form:input path="telephoneNumber"/>
</form>
Model telephoneNumber setter:
if(!StringUtils.isBlank(telephoneNumber)){
this.telephoneNumber = telephoneNumber.replaceAll("[^0-9]", "");
} else{
this.telephoneNumber= "";
}
And I think so because the value lands in the database with the formatting I used. (spacing)
Even if it is not the correct answer to your question:
I strongly recommend to do the formating in an other way then by setter getter
Spring 3.0 provideds something they called "type conversion"
spring blog with example
spring reference "Validation, Data Binding, and Type Conversion"
Using this would be much more cleaner.
Back to your question:
Spring path binding: is it bound directly to the variable or does it invoke the constructor/setters?
As fare I understand the Java Doc and some code snippets, Spring uses BeanWrapper (BeanWrapperImpl) to set the values of Beans (#see Reference: 5.4 Bean manipulation and the BeanWrapper). And BeanWrapperImpl behaves like the reference said:
uses setter and getter to access "simple" values.
It is exactly like the reference said in section "5.4.1 Setting and getting basic and nested properties": For an expression "name":
Indicates the property name
corresponding to the methods getName()
or isName() and setName(..)
So at least this answer your question, so I assume that the cause for your problem is some thing else.