Apply Spring formatter to non-form text in JSP - spring

In Spring MVC 3, I have a customer Formatter that converts my entity objects to text and parses the text for my entity objects. It's registered with the conversionService bean. This link shows how it works: http://springinpractice.com/2012/01/07/making-formselect-work-nicely-using-spring-3-formatters/
I'm wondering if there's any way to apply the formatter to text not inside of forms. Specifically, I'd like my object displays to have a web link to their foreign key entities with the same text that's used in the forms. I've gotten the forms to display successfully, but I haven't been able to apply it to the text on the JSP page. Instead, it uses toString.
I've played around with <spring:bind>, <spring:message>, and <spring:eval>, but they don't seem to apply to the formatter. <spring:eval> attempts to use the DateTimeFormatter.

Hopefully, this helps someone else looking for this. It turns out it was <spring:eval>, which makes sense, since somehow it has to be linked to Spring. The issue was syntactical. The statement below causes the entity to be processed by a Spring converter.
<spring:eval expression="myEntityObject" htmlEscape="false"/>
No JSP tags are needed, like: ${ok}
This uses the Spring expression language.

Related

How to get the selected value of combo in Spring MVC 3?

I am using the Spring MVC <form:select> tag in my JSP and have populated the values in it.
How can I retrieve the selected value from the select box and use it for further operations?
You can obtain them in the controller to which you submit. For example:
#RequestMapping("/submit")
public String handle(#RequestParam Foo selectedFoo) {
}
This assumes you have a converter from String to Foo. Otherwise you can simply use String as argument.
If you want to get the whole form submitted to a javabean, make a bean with fields named exactly the same as the fields in your html form, and pass it as argument to the handling method.
I think this tutorial will help you:
http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/
Spring MVC 3 example:
http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc-form-example.html
Just combine the logic from these 2 examples and you will understand hoe to do it right.

How can I bind fieldName_1, fieldName_2 to a list in spring mvc

I'm trying to convert a struts 1 application to Spring MVC 3.0. We have an form with quite a few parameters, most of which where automatically binded in struts. However there where a number of fields in the format fieldName_# where # is a number that we manually bound by looping through the request.
I'm looking for a tidier way to do this in Spring mvc, but don't know where to start.
Ideally we should have them as fieldName[#] and it would be easier, but we cannot change this and have to keep the fieldName_# format. Also the number of these fields that are sent in the request are unknown.
One way to achieve this is by wrapping the servletRequest and implementing the getParameter (and associated methods) such a way that parameters with name fieldName_# are returned as fieldName[#]. A servletFilter would be one option to wrap the request.

Data binding of type of Date Class. "2010/01/02" binds correctly, but "2010-01-02" does not

I found that, by default when I using Spring MVC, "2010/01/02" binds correctly, but "2010-01-02" does not.
I know Spring has some useful binding mechanisms like initBinder. However, in this question I want to know where is the rule defined. Does anyone know that for example RFC documents of HTTP or Spring references.
It's just coincidence.
Spring MVC can implicitly convert input parameters to model objects via their single-argument constructor that takes String (if model objects have such constructors). Date has this constructor, though it's deprecated, so that this behaviour is determined by behaviour of that constructor.

Is it possible to set a domain object as a hidden field in spring forum bean?

greetings all
i have a domain class named car
and i have an object from car retrieved from the DB
and i want to save this object in the form as a hidden field
in order when submitting to get this object
but when i tried
<form:hidden path=carObj />
it didn't work, any ideas why, and how to do such behaviour ?
Well carObj is a java object, and an HTML hidden field can only hold text, so how would Spring store one inside the other? If you want to use hidden fields, you're going to have to break down the object into individual fields.
It sounds like what you need is to tell Spring to store carObj in the session, so that's visible when the form is posted. You can use the #SessionAttributes annotation for this (see docs).
Spring's form tag library does indeed support hidden fields as you described.
See the Spring Reference for more information.

struts2 validation invoking xml

i have a login-validation.xml which define some basic field validation rules.
however that's not enough for me.
i need to do some more database lookup and i consider this as part of my validation logic.
how can i do both xml validation and my database lookup in one go?
i suppose i will write something like
public void validate() {
1) struts2-validation.xml validation();
2) myDatabaseLookup() and addFieldError() or addActionError();
}
my problem is, what is the api i can use for (1)?
or, how can i look at the code of this xml validation filter class? in fact i would also make the definitions in validation.xml available to javascript usage... i guess i would need to do some translation from xml to javascript logic, but first of all, how can i access the validation.xml api in java code?
Your best choice is to create a validator... Take a look here for some information -
Custom Validator
There are a few things to keep in mind... I don't know if the ObjectFactory will instantiate and inject your validator, so you might not have all the features of dependency injection. If your custom validator isn't injected, file a bug, I'll take a look at it.
After you create your validator and register it in your app, you can add it to the validation.xml file.
(side note, I know that I am pointing to the XWork docs, but Struts2 uses XWork internally for most of it's validation capabilities)

Resources